Recvfrom () hangs - how to handle this when the server is shut down

I have client code that receives a response from the server using UDP and recvfrom() . This works fine when the server is turned on, but as soon as I stop the server, my client program hangs; I suspect recvfrom () is awaiting a response from the server.

If the server and client are both installed on the same system, I get an error message from recvfrom() when the server is turned off, but when the server and client are on different systems, the client hangs in recvfrom (), because it has not responded to the server since it off.

Please, can someone give me an idea of ​​how I can deal with this situation, maybe intercepting a timer signal can solve the problem. Can anyone shed some light on this?

I am using Visual studio 2005.

+4
source share
1 answer

Your call is blocked because there is currently no data for this socket. When the server was turned on, it was fast enough to send data, so the recvfrom call received it and quickly returned. When the server is turned off, no one sends data and recvfrom waits forever. It doesn't matter if the server is on or off, recvfrom does the same in both cases; you just don’t notice the delay in the first case.

You need to use non-blocking sockets. In non-blocking mode, recvfrom returns an error if there is no data, and not wait. You can then use the select call to sleep until a timeout occurs or data arrives.

+4
source

Source: https://habr.com/ru/post/1369169/


All Articles