Re: check socket alive

From: Steve Horsley (shoot_at_the.moon)
Date: 02/10/05


Date: Wed, 09 Feb 2005 23:18:54 +0000

martinnitram@excite.com wrote:
> Dear all,
> following are some piece of my code (mainly create a socket
> connection to server and loop to receive data):
<snip>
> # function to receive data
> def recv_for_sock(sock):
> sock.settimeout(25)
> while 1:
> if sock is None:
> return 1
> try:
> recv_data = sock.recv(65535)
> if recv_data:
> .... # do something
> except socket.timeout:
> print "Socket Timeout"
> time.sleep (10)
> pass
> except socket.error:
> print "Socket Error"
> time.sleep (10)
> pass
>
<snip>
> my question is, when the socket (create a main function) is
> disconnected by server (netstat status show close_wait), in
> "recv_for_sock" function, it can catch the timeout exception at first
> time. After then, the program will looping/hang within the
> "recv_for_sock" function (actually is the socket.recv function) and
> causing CPU usage to 99%. So, how to detect the socket connection is
> closed at this case? I tried to use exception/check socket is None but
> no help.
> Thank for helping.
>

There is no point in continuing round the loop once you have caught an
exception. I suggest two possibilities:

1) put a return statement in the catch blocks, and return an ppropriate
value that indicates an error.

Or better:

2) Don't catch the exception at this level (because this method doesn't
know how to deal with the situation properly). Instead, let the exception
bubble up the call stack, and catch and deal with the exception wherever
you ARE able to sensiby able to decide what on earth to do if the receive
goes wrong.

Steve



Relevant Pages

  • Re: BeginReceive return zero length buffer when run ,and work correctly when use step by step debug
    ... when I used the sync methods it throw exception related to mixed ... If I use the loop and don't break when recv zero length - I enter inside ... Do you ever try to read from the socket ... get a WSAEWOULDBLOCK error and go back to try again). ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: An existing connection was forcibly closed by the remote host...
    ... EndReceivewill throw "Can't access disposed object" exception whenever the connection is terminated. ... In MS .NET Socket implementation there is no mechanism for the socket object to signal to the main thread that it is about to be disposed. ... public Socket ClientSocket ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: "invalid handle", sockets, threads and garbage collector
    ... your IntPtr value happens to coincide with a reference. ... The exception contains "invalid handle" message. ... Socket constructor problem ... finalization means closing the handle to resource. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: reconnect tcp
    ... instance throws a fatal exception, ... to close the socket and reconnect. ... public ClientHandler(Socket client, int timeout) ... while (rxDataCounter < receiveBytes) ...
    (microsoft.public.dotnet.languages.csharp)
  • "invalid handle", sockets, threads and garbage collector
    ... The exception contains "invalid handle" message. ... Socket constructor problem ... finalization means closing the handle to resource. ...
    (microsoft.public.dotnet.framework.clr)