Why does OS X allow you to listen on the same TCP port twice?

I tried to debug port allocation issues in Jenkins on OS X by listening to specific ports with netcat, which led to some weird results.


In a terminal on OS X 10.8.2:

$ uname -rs Darwin 12.2.1 $ nc -l 54321 

Then in the second terminal:

$ nc -l 54321

And in the third terminal, lsof shows that both instances are bound to the same port:

 $ lsof -i | grep 54321 nc 70706 chris 3u IPv4 0x55618c024692f4d1 0t0 TCP *:54321 (LISTEN) nc 70769 chris 3u IPv4 0x55618c0232cb8661 0t0 TCP *:54321 (LISTEN) 

On Linux:

First terminal:

 $ uname -rs Linux 3.2.0-34-generic $ nc -l 54321 

The second terminal:

 $ nc -l 54321 nc: Address already in use 

Why is OS X also not reporting that the address is already in use?

+7
source share
1 answer

In OS X binary code, the SO_REUSEPORT socket option is installed, which allows duplication of bindings ( setsockopt in OS X ). You can verify this using dtrace in OS X.

Netcat binary on Linux does not do this, so you will get a binding error as expected. Again, you can verify that using strace. I believe SO_REUSEPORT is deprecated or even not available on newer Linux kernels.

+6
source

All Articles