两个应用程序可以监听同一个端口吗? [英] Can two applications listen to the same port?

查看:83
本文介绍了两个应用程序可以监听同一个端口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同一台机器上的两个应用程序可以绑定到同一个端口和IP地址吗?更进一步,一个应用程序可以侦听来自某个 IP 的请求,而另一个应用程序侦听来自另一个远程 IP 的请求吗?我知道我可以让一个从两个线程(或分支)开始的应用程序具有相似的行为,但是两个没有共同点的应用程序可以做同样的事情吗?

Can two applications on the same machine bind to the same port and IP address? Taking it a step further, can one app listen to requests coming from a certain IP and the other to another remote IP? I know I can have one application that starts off two threads (or forks) to have similar behavior, but can two applications that have nothing in common do the same?

推荐答案

答案因所考虑的操作系统而异.总的来说:

The answer differs depending on what OS is being considered. In general though:

对于 TCP,没有.您一次只能让一个应用程序侦听同一个端口.现在,如果您有 2 个网卡,您可以让一个应用程序使用相同的端口号侦听第一个 IP,第二个侦听第二个 IP.

For TCP, no. You can only have one application listening on the same port at one time. Now if you had 2 network cards, you could have one application listen on the first IP and the second one on the second IP using the same port number.

对于 UDP(多播),多个应用程序可以订阅同一个端口.

For UDP (Multicasts), multiple applications can subscribe to the same port.

从 Linux 内核 3.9 及更高版本开始,使用 SO_REUSEPORT 选项添加了对侦听同一端口的多个应用程序的支持.这篇 lwn.net 文章

Since Linux Kernel 3.9 and later, support for multiple applications listening to the same port was added using the SO_REUSEPORT option. More information is available at this lwn.net article.

这篇关于两个应用程序可以监听同一个端口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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