check socket alive

M

martinnitram

Dear all,
following are some piece of my code (mainly create a socket
connection to server and loop to receive data):

# function to create and return socket
def connect():
server_config = ('192.168.1.50', 3333);
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect()
except socket.error:
print "Failed to connect"
return 0
return sock

# 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

# main function
if __name__ == '__main__':
sock = connect()
if sock:
errorno = recv_for_sock(sock)
... # other stuffs
else:
print "Cannot create connection"
...

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.
 
S

Steve Horsley

Dear all,
following are some piece of my code (mainly create a socket
connection to server and loop to receive data):
# 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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top