1

I need to do an important test. The test's condition is as bellow.

I am using winsock and I have 2 UDP sockets (Sock-A and Sock-B) which with SO_REUSEADDR option both of them have been bound to port 1000 on one PC. Both of them transmit UDP packets out to another socket (Sock-C) and Sock-C which is located in different LAN receives their messages.

Sock-C responds them with some messages, but unfortunately I can see just Sock-A (which has been opened sooner) just gets the messages, and Sock-B doesn't get anything. When I close Sock-A I can see that Sock-B starts to receiving the messages.

Any of you know what should I do to let both of Sock-A and Sock-B can receive messages from Sock-C?

Thanks~

  • Is SO_REUSEADDR available in WinSock? Are you checking the return value from setsockopt? https://msdn.microsoft.com/en-us/library/windows/desktop/ms740476(v=vs.85).aspx – Neil Apr 19 '17 at 12:09
  • Why do you use the same port - http://xyproblem.info/ ? – Karoly Horvath Apr 19 '17 at 14:09
  • Yes, It's possible to bind same port to two different sockets with SO_REUSEADDR option. – mehrdad fallahpour Apr 20 '17 at 02:50
  • The reason why I need to use same port is, I have a Voip software that has a UDP listener to receive audio packets. This application is closed and I can't change it. I need to send audio packets to this software from somewhere behind a different NAT. To handle P2P connection for those peers, I need to bind a UDP socket same as the voip software on the same host and send UDP packets to the originate of audio packets. Then NAT without port-forwarding and STUN/TURN and ... will route packets to Voip software. – mehrdad fallahpour Apr 20 '17 at 02:51
  • Idk sorry, but I can't picture what you are trying to do with the explanation provided – rinn2883 Apr 21 '17 at 08:10

1 Answers1

0

This is normal behavior. I think this can't be done with 2 sockets listening on the same port.

Why are you binding 2 sockets to the same port?

Read Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

In linux it will even try to distribute the datagrams evenly between the 2 sockets, so its random. You will need to change how you send/receive packets.

Community
  • 1
  • 1
rinn2883
  • 366
  • 1
  • 14
  • 1
    When two UDP sockets are bound to the same port and an incoming unicast packet is received on that port, it will be handed to one of the two sockets (chosen arbitrarily). If the incoming packet is a multicast UDP packet, OTOH, it will be handed to every socket bound to the port. (The latter behavior is necessary in order to allow multiple programs on the same host to listen to the same multicast group simultaneously) – Jeremy Friesner Apr 19 '17 at 14:22
  • good point. I did not include it because op did not mention using multicast. – rinn2883 Apr 19 '17 at 14:34
  • Thanks guys, So how can I deal with this problem? Since I am sending packets through the internet, can I send them in multicast mode? – mehrdad fallahpour Apr 20 '17 at 02:49
  • that depends since the VoIP is closed software there will have to be support for listening to a multicast address. For sending you don't need to join a multicast group but for receiving it needs to join a group. – rinn2883 Apr 21 '17 at 08:08