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

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

问题描述

同一台机器上的两个应用程序是否可以绑定到同一个端口IP地址并侦听传入连接?在 套接字选项SO_REUSEADDR
设置在 在使用bind()之前监听套接字。 但所有传入的套接字连接请求始终指向其中一个应用程序,而不是两者。 

解决方案

On 7/22 / 2018 3:16 PM,Neptune123写道:


同一台机器上的两个应用程序是否可以绑定到同一端口和IP地址并侦听传入连接?在使用bind()之前,在侦听套接字上设置套接字选项SO_REUSEADDR。但是所有传入的套接字连接请求
总是被定向到其中一个应用程序,而不是两者。


绑定到两个套接字的两个套接字的行为相同的端口是不确定的:


https://docs.microsoft.com/en-us/windows/desktop/WinSock/using-so-reuseaddr-and-so-exclusiveaddruse


但是,"此非确定性行为的例外是多播套接字。如果两个套接字绑定到同一个接口和端口并且是同一个多播组的成员,则数据将被传递到两个套接字,而不是任意选择
。"


<我自己并不熟悉多播套接字。也许你需要研究一下。



Can two applications on the same machine bind to the same port and IP address and listen for incoming connections? The  socket option SO_REUSEADDR is set on the  listen socket before using bind().  But all the incoming socket connection requests is always directed to one of the applications, not both. 

解决方案

On 7/22/2018 3:16 PM, Neptune123 wrote:

Can two applications on the same machine bind to the same port and IP address and listen for incoming connections? The  socket option SO_REUSEADDR is set on the  listen socket before using bind().  But all the incoming socket connection requests is always directed to one of the applications, not both.

The behavior of two sockets bound to the same port is indeterminate:

https://docs.microsoft.com/en-us/windows/desktop/WinSock/using-so-reuseaddr-and-so-exclusiveaddruse

However, "The exception to this non-deterministic behavior is multicast sockets. If two sockets are bound to the same interface and port and are members of the same multicast group, data will be delivered to both sockets, rather than an arbitrarily chosen one."

I'm not familiar with multicast sockets myself. Perhaps something for you to look into.


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

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