Vega4
Vega4

Reputation: 989

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

Handling multiple UDP sockets listening on the same endpoint

None of which ever got resolved.

[Situation]:

On Microsoft Windows, I bind two UDP server sockets in the same process. This particular (not to say peculiar) operating system supports only SO_REUSEADDR, it does not support SO_REUSEPORT; and on top of that, SO_REUSEADDR acts like 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; 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; 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.

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.

The binding operations, in such a configuration for the two UDP sockets, do succeed.

Problem:

Now, I'm having trouble getting these sockets to receive data as expected. Namely, I expect the two sockets to receive data. 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.

Upvotes: 0

Views: 540

Answers (1)

Evgeny
Evgeny

Reputation: 1072

If you want two ports are received same udp packet you should set option/flag broadcast for transmit socket. 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.

Upvotes: -1

Related Questions