1

Does the bind() function in linux can modify its argument ? I ask this because a sin_port of 0 means that the OS will choose the port, and I would like to get this port after this call.

rafoo
  • 1,506
  • 10
  • 17
  • 2
    This will be covered in the documentation, no? It also works to ask the real question directly: "How to get the port bound with bind() when it's chosen automatically?" If the method is such-and-such-is-modified, then that's the method. And if another method is used, then modification of arguments is not relevant to the task. – user2864740 Mar 15 '20 at 23:28
  • 1
    Ok, I miss the `const` modifier in the documentation. Thanks you – rafoo Mar 15 '20 at 23:33

1 Answers1

3

No, it does not modify the argument. Instead you use getsockname after binding to find out what port you got.

You can also use getsockname after connect to get both the local port assignment and the local address (if you have more than one address) correspomnding to the route to the remote host. This works even with UDP where connect doesn't actually send any packets and only logically binds addresses.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711