简体   繁体   English

只有一个套接字接收数据。 两个UDP服务器sockets绑定到同一个端口,Windows上的不同地址

[英]Only one socket receives data. Two UDP server sockets bound to same port, different addresses on Windows

[Similar Questions]: [类似问题]:

Problems using UDP sockets bound to the same port on Windows使用 UDP 的问题 sockets 绑定到 Windows 上的同一端口

Handling multiple UDP sockets listening on the same endpoint处理多个 UDP sockets 侦听同一端点

None of which ever got resolved.这些都没有得到解决。

[Situation]: [情况]:

On Microsoft Windows, I bind two UDP server sockets in the same process.在 Microsoft Windows 上,我在同一进程中绑定了两个 UDP 服务器 sockets。 This particular (not to say peculiar) operating system supports only SO_REUSEADDR , it does not support SO_REUSEPORT ;这个特殊的(不是说特殊的)操作系统只支持SO_REUSEADDR ,它不支持SO_REUSEPORT and on top of that, SO_REUSEADDR acts like SO_REUSEPORT .最重要的是, SO_REUSEADDR就像SO_REUSEPORT一样。

So, considering the above, I am left with binding socket A to 0.0.0.0:1234 and socket B to say 192.168.1.1:1234 ;因此,考虑到上述情况,我只剩下将套接字 A 绑定到0.0.0.0:1234 ,将套接字 B 绑定到192.168.1.1:1234 if I'm to reuse the same port on the same machine.如果我要在同一台机器上重用同一端口。

Namely, I cannot bind both to the same IP address, since Microsoft Windows would not agree with that;也就是说,我不能将两者都绑定到同一个 IP 地址,因为 Microsoft Windows 不会同意这一点; since yet again, it does not support two sockets to be bound to exactly the same tuple of {SRC_ADDR,DST_ADDR, SRC_PORT,DST_PORT,PROTOCOL} even when SO_REUSEADDR is used on BOTH sockets.再一次,它支持两个 sockets 绑定到完全相同的{SRC_ADDR,DST_ADDR, SRC_PORT,DST_PORT,PROTOCOL}元组,即使在两个 sockets 上都使用了SO_REUSEADDR

So, I'm forced to bind to two distinct addresses, if I'm to use the same port on the same machine.因此,如果我要在同一台机器上使用同一端口,我将不得不绑定到两个不同的地址。 Even though 0.0.0.0 represents any address, but in any case Microsoft Windows agrees, and binds the two sockets in such a configuration, without any error.虽然0.0.0.0代表任意地址,但无论如何微软Windows都同意,将两个sockets绑定成这样的配置,没有报错。

The binding operations, in such a configuration for the two UDP sockets, do succeed. UDP sockets 两个这样的配置,绑定操作成功。

Problem:问题:

Now, I'm having trouble getting these sockets to receive data as expected.现在,我无法让这些 sockets 按预期接收数据。 Namely, I expect the two sockets to receive data.即,我希望两个 sockets 接收数据。 In reality, only the socket which got bound first receives data.实际上,只有最先绑定的套接字才能接收数据。

Any ideas?有任何想法吗?

Is it that the data is always received by the socket best matching the destination?数据是否总是由与目的地最匹配的套接字接收? If you wonder why I'm playing that way, it's because I'm trying to multiplex two libraries with two protocols over the same port, with the least amount of modifications.如果你想知道为什么我要那样玩,那是因为我试图在同一个端口上用两个协议多路复用两个库,修改量最少。

If you want two ports are received same udp packet you should set option/flag broadcast for transmit socket.如果您希望两个端口接收到相同的 udp 数据包,您应该为传输套接字设置选项/标志广播。 Something like:就像是:

      socket_.set_option(boost::asio::socket_base::broadcast(true));

In some cases (for some OS's) you should transmit packet to broadcast address instead of exact ip-address.在某些情况下(对于某些操作系统),您应该将数据包传输到广播地址而不是确切的 ip 地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两对 TCP 服务器/客户端共享相同的代码,但只有一个客户端接收消息 - Two pairs of TCP server/client sharing same code, but only one client receives messages 在派生套接字上侦听时,只有一个进程接收UDP数据包是一种可靠的行为吗? - Is it a reliable behavior that only one process receives a UDP packet when listening on a forked socket? 服务器连接不同的套接字端口 - Server connecting with different sockets port boost :: socket只收到一条消息 - boost::socket receives only one message 在同一端口上绑定两个多播套接字 - Binding two multicast sockets on the same port 通过2个套接字在不同IP和端口上进行通信的UDP客户端/服务器程序 - UDP client/server program that communicates over 2 sockets on different IPs and ports 无法通过Windows UDP套接字发送数据:错误代码10035 - Unable to send data over windows UDP sockets:Error Code 10035 Windows,C++:一个服务器套接字上的两个连接? - Windows, C++: two connections on one server socket? 两个临时对象的地址在同一个表达式中是否保证不同? - Are the addresses of two temporaries guaranteed to be different in the same expression? 如何从相同的SRC和DEST端口(C ++)运行UDP套接字 - How to run UDP sockets from the same SRC and DEST port (c++)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM