0
votes

I have a UDP client that calls connect(), send() and recv().

The server has multiple IP addresses. If the reply from the server is not from the same IP as the query, then recv() times out. I have read elsewhere that this might be because the client is calling connect(), so it will only accept a reply from the same IP.

Is there a way to ensure the server always replies from the same IP as the query? I would like the server to listen on all interfaces.

Update: If the client does not call connect() and calls sendto() instead of send(), then recv() correctly receives the reply from the server. I would still rather fix it on the server side by sending the reply from the same IP that the query came from. There is no routing happening on the server, it's one network interface with several IPs.

2
Please write a snippet to see the code. Also I don't believe recv fails, at most it will receive nothing. Finally, I don't believe the server receives a message in one IP address and send the response from a different IP address if there is NAT in the middle. You should provide more details if you want some helprodolk
I should have been clearer. recv() is timing out.upl8
If I remember this correct, the client will be establish a communication on a port number and ip received with the udp package, when transmit into the server. The server should return to the same port number. Sometimes firewall scenarios can cause problem require something called "udp hole punching", but its not related to multiple client issue. If you have multiple clients you may have a look at thread safe coding to keep the clients apart.Independent
Firewall disabled on client and server for testing. The datagram is making it back to the client, but recv() is ignoring it because of the different source IP address. I have since confirmed that the problem does not occur if the client does not call connect() and calls sendto() instead of send().upl8

2 Answers

0
votes

This makes sense if the client calls connect to one IP address and port, it won't receive a UDP datagram sent from a different IP or port.

0
votes

If you want your server to listen on all ip's and all ports you'll need to program at the ethernet layer (raw sockets). Check out this link.

When programming in raw sockets you can check in your code for the IP address that the datagrams were addressed to and respond from the appropriate IP.