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(s ocket.AF_INET, socket.SOCK_STR EAM)
try:
sock.connect()
except socket.error:
print "Failed to connect"
return 0
return sock
# function to receive data
def recv_for_sock(s ock):
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(s ock)
... # 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_s ock" function, it can catch the timeout exception at first
time. After then, the program will looping/hang within the
"recv_for_s ock" 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. 1 3808 ma**********@ex cite.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(s ock): 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_s ock" function, it can catch the timeout exception at first time. After then, the program will looping/hang within the "recv_for_s ock" 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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>
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.settimeout(5.0)
|
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 e.g. some router on the way has restarted or if it restarts just in
the same time as the data is beeing send.
?
|
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:
Loop:----------------
Socket.Receive //receive http data chunks
Socket.Send //'/r/n' data to check if connection is active
------------------------
The problem is that when connection is closed by the http server, the
send socket function...
|
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 like:
TcpListener listenter = new TcpListener(IPAddress.Any, 8000);
listenter.Start();
|
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 mean the
program stops getting information but doesn't raise any error.
| |
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 this, but
it somehow doesn't work:
================================================
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPAddress ipa = Dns.GetHostEntry("www.blablabla.com").AddressList;
|
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 connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond
|
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
submit (POST), it usually comes back with a "connection has been
reset" error. Sometimes it does work though.
Furthermore - when I inspect the request string, it's always like:
|
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 the crash each time.
We finally think we have narrowed down the cause of this to a user (located
semi-remotely) who would connect into our system and disconnect
"ungracefully" (literally, by pulling his network cable). Connections here
are all...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |