Recvfrom() is hanging -- how to deal this when Server is OFF
I have client code which gets a response from the server using UDP and recvfrom()
. This is working fine when the server is ON, but once I stop the server my 开发者_JAVA百科client program is hanging; I suspect recvfrom() is waiting for the response from the server.
If the server and client are both are installed on same system them I am getting error from recvfrom()
when the server is OFF, but when the server and client are on different systems then the client hangs at recvfrom() as there is no response from the server since its OFF.
Please some one can give me idea how can I can deal with this situation, maybe a timer signal interuption can solve the issue.. can anyone throw some light on this?
I am Using Visual studio 2005.
Your call is blocking, because there is no data for this socket right now. When the server was on, it was fast enough to send data so the recvfrom
call got it and returned quickly. When the server is off, nobody's sending data and recvfrom
waits forever. It does not matter whether server is on or off, recvfrom
is doing the same thing 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
will return an error when there is no data, instead of waiting. You can then use select
call to sleep until a timeout happens or the data arrive.
精彩评论