473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

check socket alive

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
1 3789
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thomas Hervé | last post by:
My problem is not really python specific but as I do my implementation in python I hope someone here can help me. I have two programs that talk through a socket. Here is the code : <server>...
6
by: Anders Both | last post by:
If you send data using TCP and Socket, can the client then be 100% sure that if it send´s data and no exception uccur, then the data will also arrived in a a correct way on the server. What if...
2
by: Nuno Magalhaes | last post by:
How to check for a closed http socket without losing any data? (MSDN says to send something to server). Somewhere in my code (after sending the http header in plain text) I'm doing a loop with:...
3
by: Eric Marthinsen | last post by:
Hello- I'm trying to create a console app that will accept an incoming HTTP request and output the information of the request to the console. The code is pretty simple - here's what it looks...
6
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I've developed a program that uses a socket to receive information 24h a ~ day. The problem is that the socket seems to freeze. By that I...
1
by: cyberco | last post by:
Using: .Net Compact Framework 2, Windows Mobile 5 (Pocket PC) I'm trying to upload a large binary file in chuncks over a socket (as a multipart mime message). This is how I'm trying to accomplish...
3
by: =?Utf-8?B?UGFpbiBhbmQgaGVhZGFjaGU=?= | last post by:
Hi, I am trying to get a webpage using a TcpSocket instead of a standard Webrequest. Initial, it works fine but after the 2 or 3 request the tcpclient I start to get the following error: A...
3
by: Khookie | last post by:
Hi everyone I've been coding a web server recently, and wanted to figure out how to do it with mingw (code is below). It works fine when you first load a page (GET request), but when you...
4
by: Adam Clauss | last post by:
A while back I posted regarding a problem we were having with one of our applications which was randomly crashing. Monitoring memory usage revealed a spike in nonpaged pool memory just prior to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.