Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from Nov 15 '12 at 7:30

This question came from our site for professional and enthusiast programmers.

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 Nov 14 '12 at 21:58
Ok, but are you sure the OS wont pick the same port as any other machine in the same network? – grimgrom Nov 14 '12 at 22:00
Are these broadcast packets? Are you talking about communicating through a NAT device? – Harry Johnston Nov 15 '12 at 3:25

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.

share|improve this answer
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? – grimgrom Nov 14 '12 at 22:26
@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. – Fozi Nov 14 '12 at 22:31
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. – grimgrom Nov 14 '12 at 23:08
@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 Nov 15 '12 at 2:12

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).

share|improve this answer
Ok I see, but how does SO_REUSEPORT work and how should it be implemented? – grimgrom Nov 15 '12 at 19:26
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. – EJP Dec 12 '13 at 23:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.