By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,122 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,122 IT Pros & Developers. It's quick & easy.

check socket alive

P: n/a
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.

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ma**********@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
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.