为什么OS X允许在同一TCP端口上侦听两次? [英] Why does OS X allow listening on the same TCP port twice?

查看:119
本文介绍了为什么OS X允许在同一TCP端口上侦听两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用netcat监听某些端口来调试OS X上Jenkins中的端口分配问题,这导致了一些奇怪的结果.

I was trying to debug port allocation problems in Jenkins on OS X by listening to certain ports with netcat, which led to some weird results.

在 OS X 10.8.2 上的终端中:

In a terminal on OS X 10.8.2:

$ uname -rs
Darwin 12.2.1

$ nc -l 54321

然后在第二个终端中

$ nc -l 54321

在第三个终端中, lsof 显示两个实例都绑定到相同的端口:

And in a third terminal, lsof shows that both instances have 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)


在 Linux 上:


On Linux:

第一终端:

$ uname -rs
Linux 3.2.0-34-generic

$ nc -l 54321

第二个终端:

$ nc -l 54321
nc: Address already in use


为什么OS X也不报告该地址已被使用?


Why doesn't OS X also report that the address is already in use?

推荐答案

OS X上的二进制文件正在设置SO_REUSEPORT套接字选项,该选项允许完全重复的绑定(OS X 上的setsockopt).您可以在OS X上使用dtrace对此进行验证.

The binary on OS X is setting the SO_REUSEPORT socket option, which allows completely duplicate bindings (setsockopt on OS X). You can verify this using dtrace on OS X.

Linux上的netcat二进制文件不会执行此操作,因此您会收到预期的绑定错误.同样,您可以使用strace进行验证.我相信SO_REUSEPORT已被弃用,或者甚至在较新的Linux内核上也不可用.

The netcat binary on Linux does not do this, so you get a bind error as expected. Again, you can verify that using strace. I belive SO_REUSEPORT is deprecated, or not even available on newer Linux kernels.

这篇关于为什么OS X允许在同一TCP端口上侦听两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆