How does the OS generate a random socket port?

0

I'm wondering how the OS makes sure that the clients don't get the same "random"(binding the socket by setting the port to 0) port as any other client in the network, since I want to communicate using the UDP protocol between my server and a lot of machines that's using the same local IP it would be really difficult if the clients get each others packets.

grimgrom

Posted 2012-11-14T21:53:36.247

Reputation: 101

It's a good question, but its a broad topic, and not c++ related in its current form. You might get abetter answer on superuser. – Mike – 2012-11-14T21:58:39.120

Ok, but are you sure the OS wont pick the same port as any other machine in the same network? – None – 2012-11-14T22:00:16.193

Are these broadcast packets? Are you talking about communicating through a NAT device? – Harry Johnston – 2012-11-15T03:25:35.437

Answers

3

The OS has a list of the currently in-use ports, it usually just picks the next available one. That said, it tries to avoid low numbers.

Ports don't have to be unique in the network, only per connection. A connection consists of

  • the source IP
  • the source port
  • the destination IP
  • the destination port

If any of these is different, it's a different connection. This means that a server can have many connections coming in to the same port from different IPs or from different ports from the same IP.

Fozi

Posted 2012-11-14T21:53:36.247

Reputation: 68

I was not clear enough, sorry. I'm using the UDP protocol which means if two clients in the same network uses the same port and receive a packet from someone outside that local network both of the clients will receive the packet? Am I right? – None – 2012-11-14T22:26:35.000

@grimgrom No, if you are using a router then the target IP will be different. If you are using NAT, then the NAT router will have to assign different ports for each client. You will have to set this up somehow (explicit forwarding, UPnP, port triggering, NAT hole punching, ...). In any case the clients can use the same local port, the NAT can not. – None – 2012-11-14T22:31:44.003

Ok, but what about this case: two machines in the same network communicating with a server outside the network(For example using the public ip 40,40,40,40 and the port 4444). So, the two machines uses the same port. What would happen when the server send a packet to the ip: 40,40,40,40:4444? Who's getting the packet? Remember that the machines uses the UDP protocol. – None – 2012-11-14T23:08:31.743

@grimgrom When the packet hits the NAT router, the router assigns a random port and re-sends the packet itself, when it gets a response it uses a look-up table it built and forwards the packet on toward whoever originally asked for it. So to answer your question "The router OS handles it, not the client, nor the server" in your case, if you are talking about NAT. – Scott Chamberlain – 2012-11-15T02:12:04.030

0

how the OS makes sure that the clients don't get the same "random"(binding the socket by setting the port to 0) port as any other client in the network

It doesn't. It ensures that the local applications get a unique port in the current host. Nothing to do with clients or the network whatsover.

it would be really difficult if the clients get each others packets.

They don't unless they are in the same host and they are deliberately sharing the same port, but you would have to program them both to do so deliberately (i.e. set SO_REUSEPORT on both sockets).

user207421

Posted 2012-11-14T21:53:36.247

Reputation: 214

Ok I see, but how does SO_REUSEPORT work and how should it be implemented? – grimgrom – 2012-11-15T19:26:48.743

SO_REUSEPORT is used for multicast clients. If you set it on all affected datagram sockets they can all use the same port and will all receive multicasts to that port. – user207421 – 2013-12-12T23:57:39.447