473,407 Members | 2,314 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.

how to know if socket is still connected

lo there,
i have a simple app that connects to a socket to get info from a server

i looks like this

serverhost = 'xxx.xxx.xxx.xxx'
serverport = 9520
aeris_sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aeris_sockobj.connect((serverhost,serverport))

while 1:
do this or that with socket,
send and receive info.
yadda yadda yadda
works well, but sometimes the server drops the connection.
so, what i need is something that will let me know if the connection
is still ok, if not will reconnect.

what i thought, since it only lets you connect on a certain port one at
a time,
that i could use a try-except to connect every time, if it could not
connect (because it already is) then i would just continue on. But if
it is not connected, it would reconnect.
that is what brings me here. Seems like it would work, but is there a
better way ? this kinda seems like a dirty hack.

any opinions ?

thanks.

Jul 16 '06 #1
18 63961
On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:
serverhost = 'xxx.xxx.xxx.xxx'
serverport = 9520
aeris_sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aeris_sockobj.connect((serverhost,serverport))

while 1:
do this or that with socket,
send and receive info.
yadda yadda yadda

works well, but sometimes the server drops the connection. so,
what i need is something that will let me know if the
connection is still ok, if not will reconnect.
If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
what i thought, since it only lets you connect on a certain
port one at a time, that i could use a try-except to connect
every time, if it could not connect (because it already is)
then i would just continue on. But if it is not connected, it
would reconnect. that is what brings me here. Seems like it
would work, but is there a better way?
I don't see why the normal send() and recv() semantics aren't
sufficient.

--
Grant Edwards grante Yow! I'm an East Side
at TYPE...
visi.com
Jul 16 '06 #2

Grant Edwards wrote:
On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:
serverhost = 'xxx.xxx.xxx.xxx'
serverport = 9520
aeris_sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aeris_sockobj.connect((serverhost,serverport))

while 1:
do this or that with socket,
send and receive info.
yadda yadda yadda

works well, but sometimes the server drops the connection. so,
what i need is something that will let me know if the
connection is still ok, if not will reconnect.

If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
what i thought, since it only lets you connect on a certain
port one at a time, that i could use a try-except to connect
every time, if it could not connect (because it already is)
then i would just continue on. But if it is not connected, it
would reconnect. that is what brings me here. Seems like it
would work, but is there a better way?

I don't see why the normal send() and recv() semantics aren't
sufficient.

--
Grant Edwards grante Yow! I'm an East Side
at TYPE...
visi.com


like this ?
databack = aeris_sockobj.recv(2048)
if databack:
view_msg = 'caught request acknowlage %s bytes \n' %
len(databack)
else:
view_msg = 'fail to recieve data from aeris server\n'

then put the reconnect in the else: block ?

thanks
thanks

Jul 16 '06 #3
On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:
>If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
like this ?
databack = aeris_sockobj.recv(2048)
if databack:
view_msg = 'caught request acknowlage %s bytes \n' %
len(databack)
else:
view_msg = 'fail to recieve data from aeris server\n'

then put the reconnect in the else: block ?
Yes, if the problem is that the host closes the connection,
that should work. Modulo the broken indentation and
line-wrapping. ;)

--
Grant Edwards grante Yow! My mind is a potato
at field...
visi.com
Jul 17 '06 #4
cool enough, thanks !

-sk
Grant Edwards wrote:
On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:
If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
like this ?
databack = aeris_sockobj.recv(2048)
if databack:
view_msg = 'caught request acknowlage %s bytes \n' %
len(databack)
else:
view_msg = 'fail to recieve data from aeris server\n'

then put the reconnect in the else: block ?

Yes, if the problem is that the host closes the connection,
that should work. Modulo the broken indentation and
line-wrapping. ;)

--
Grant Edwards grante Yow! My mind is a potato
at field...
visi.com
Jul 17 '06 #5
In article <12*************@corp.supernews.com>,
Grant Edwards <gr****@visi.comwrote:
>On 2006-07-16, ne*****@xit.net <ne*****@xit.netwrote:
>serverhost = 'xxx.xxx.xxx.xxx'
serverport = 9520
aeris_sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
aeris_sockobj.connect((serverhost,serverport))

while 1:
do this or that with socket,
send and receive info.
yadda yadda yadda

works well, but sometimes the server drops the connection. so,
what i need is something that will let me know if the
connection is still ok, if not will reconnect.

If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
>what i thought, since it only lets you connect on a certain
port one at a time, that i could use a try-except to connect
every time, if it could not connect (because it already is)
then i would just continue on. But if it is not connected, it
would reconnect. that is what brings me here. Seems like it
would work, but is there a better way?

I don't see why the normal send() and recv() semantics aren't
sufficient.
Jul 17 '06 #6
On 2006-07-17, Cameron Laird <cl****@lairds.uswrote:
>>works well, but sometimes the server drops the connection. so,
what i need is something that will let me know if the
connection is still ok, if not will reconnect.

If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
>>what i thought, since it only lets you connect on a certain
port one at a time, that i could use a try-except to connect
every time, if it could not connect (because it already is)
then i would just continue on. But if it is not connected, it
would reconnect. that is what brings me here. Seems like it
would work, but is there a better way?

I don't see why the normal send() and recv() semantics aren't
sufficient.
.
.
.
Often normal send() and recv() semantics have been mistaught.
An alert alien, looking at other common APIs in isolation,
might reasonably wonder whether there is some sort of
still_ok_to_use() sort of check as part of TCP. As it happens,
of course, that doesn't fit with the rest of socket networking,
which takes the "modernist" approach of trying send() or recv(),
and reporting any exception.
On most Unices there are some obscure API features that can be
used to generate a SIGPIPE under some vaguely specified error
conditions (e.g. TCP keepalive timeout). I've only read about
them and never tried to use them, since I couldn't see anything
in the description of the features that was any benefit over
the nomral send() and recv() usage.

--
Grant Edwards grante Yow! Xerox your lunch
at and file it under "sex
visi.com offenders"!
Jul 17 '06 #7
hey there, i have a question about this solution.
if i have a
message = socket.recv()
in the script, and the socket connection drops, will the
socket.recv() just wait forever for something to come across
the internet port? or will it know if the connection is dropped?
thanks.
-sk
Grant Edwards wrote:
On 2006-07-17, Cameron Laird <cl****@lairds.uswrote:
>works well, but sometimes the server drops the connection. so,
what i need is something that will let me know if the
connection is still ok, if not will reconnect.

If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.

what i thought, since it only lets you connect on a certain
port one at a time, that i could use a try-except to connect
every time, if it could not connect (because it already is)
then i would just continue on. But if it is not connected, it
would reconnect. that is what brings me here. Seems like it
would work, but is there a better way?

I don't see why the normal send() and recv() semantics aren't
sufficient.
.
.
.
Often normal send() and recv() semantics have been mistaught.
An alert alien, looking at other common APIs in isolation,
might reasonably wonder whether there is some sort of
still_ok_to_use() sort of check as part of TCP. As it happens,
of course, that doesn't fit with the rest of socket networking,
which takes the "modernist" approach of trying send() or recv(),
and reporting any exception.

On most Unices there are some obscure API features that can be
used to generate a SIGPIPE under some vaguely specified error
conditions (e.g. TCP keepalive timeout). I've only read about
them and never tried to use them, since I couldn't see anything
in the description of the features that was any benefit over
the nomral send() and recv() usage.

--
Grant Edwards grante Yow! Xerox your lunch
at and file it under "sex
visi.com offenders"!
Jul 17 '06 #8
On 2006-07-17, ne*****@xit.net <ne*****@xit.netwrote:
hey there, i have a question about this solution.
if i have a
message = socket.recv()
in the script, and the socket connection drops, will the
socket.recv() just wait forever for something to come across
the internet port? or will it know if the connection is dropped?
As I said before, if the socket is closed by the remote host,
recv() will return "".

I don't know what you mean by "drops" and "dropped" in this
context. If you want a useful answer to your question, you'll
have to define your terms precisely.

--
Grant Edwards grante Yow! I'm CONTROLLED by
at the CIA!! EVERYONE is
visi.com controlled by the CIA!!
Jul 17 '06 #9
oh, sorry, what i mean by dropped is that the server i am connecting to
can close the connection. If that happens, i need to know about it.
i also need to know about it if the server i am connecting to just
dies.

if recv() returns "" is that the same as NONE ?
again, sorry, i am still kinda new at this.
I mean can the value be tested true or false?

thanks
-sk
Grant Edwards wrote:
On 2006-07-17, ne*****@xit.net <ne*****@xit.netwrote:
hey there, i have a question about this solution.
if i have a
message = socket.recv()
in the script, and the socket connection drops, will the
socket.recv() just wait forever for something to come across
the internet port? or will it know if the connection is dropped?

As I said before, if the socket is closed by the remote host,
recv() will return "".

I don't know what you mean by "drops" and "dropped" in this
context. If you want a useful answer to your question, you'll
have to define your terms precisely.

--
Grant Edwards grante Yow! I'm CONTROLLED by
at the CIA!! EVERYONE is
visi.com controlled by the CIA!!
Jul 17 '06 #10
On 2006-07-17, ne*****@xit.net <ne*****@xit.netwrote:
oh, sorry, what i mean by dropped is that the server i am
connecting to can close the connection.
Then recv() will return "" and send() will raise an exception.
If that happens, i need to know about it. i also need to know
about it if the server i am connecting to just dies.
Again, you have to define what you mean by "server just dies".

If the server _application_ crashes or exits, then the OS will
close the socket and recv() will return "". If somebody powers
down the server without warning, or if the server OS crashes,
or if the Ethernet cable between the Internet and the server is
cut, then the socket will not be closed, and recv() will wait
forever[1].
if recv() returns "" is that the same as NONE ?
No. It's the empty string (also spelt '').
I mean can the value be tested true or false?
Yes. The empty string is false, all non-empty strings are
true.

[1] Unless you've enabled the TCP Keepalive feature, in which
case the socket will timeout in a couple hours and recv()
will return "".

--
Grant Edwards grante Yow! Now I am depressed...
at
visi.com
Jul 17 '06 #11
If the server _application_ crashes or exits, then the OS will
close the socket and recv() will return "". If somebody powers
down the server without warning, or if the server OS crashes,
or if the Ethernet cable between the Internet and the server is
cut, then the socket will not be closed, and recv() will wait
forever[1].
Ok, yes all of the above is what i mean. Actually I am not too
concerned about a server os crash, or the cable being cut. But I have
had them close the connection on me, after which i just reconnect
(whenever i discover that its happened)
>[1] Unless you've enabled the TCP Keepalive feature, in which
case the socket will timeout in a couple hours and recv()
will return "".
if this is something that must be enabled, or is not enabled by
default, then it is not enabled.
so i should be pretty good.

thanks for all of your help, gents !
-sk


Grant Edwards wrote:
On 2006-07-17, ne*****@xit.net <ne*****@xit.netwrote:
oh, sorry, what i mean by dropped is that the server i am
connecting to can close the connection.

Then recv() will return "" and send() will raise an exception.
If that happens, i need to know about it. i also need to know
about it if the server i am connecting to just dies.

Again, you have to define what you mean by "server just dies".

If the server _application_ crashes or exits, then the OS will
close the socket and recv() will return "". If somebody powers
down the server without warning, or if the server OS crashes,
or if the Ethernet cable between the Internet and the server is
cut, then the socket will not be closed, and recv() will wait
forever[1].
if recv() returns "" is that the same as NONE ?

No. It's the empty string (also spelt '').
I mean can the value be tested true or false?

Yes. The empty string is false, all non-empty strings are
true.

[1] Unless you've enabled the TCP Keepalive feature, in which
case the socket will timeout in a couple hours and recv()
will return "".

--
Grant Edwards grante Yow! Now I am depressed...
at
visi.com
Jul 17 '06 #12
On 2006-07-17, ne*****@xit.net <ne*****@xit.netwrote:
>If the server _application_ crashes or exits, then the OS will
close the socket and recv() will return "". If somebody powers
down the server without warning, or if the server OS crashes,
or if the Ethernet cable between the Internet and the server is
cut, then the socket will not be closed, and recv() will wait
forever[1].

Ok, yes all of the above is what i mean. Actually I am not too
concerned about a server os crash, or the cable being cut. But I have
had them close the connection on me, after which i just reconnect
(whenever i discover that its happened)
>>[1] Unless you've enabled the TCP Keepalive feature, in which
case the socket will timeout in a couple hours and recv()
will return "".

if this is something that must be enabled, or is not enabled by
default, then it is not enabled.
On all OSes with which I'm familiar it's disabled by default.
You use a socket object's setsockopt method to enable it:

s.setsockopt(socket.SOL_TCP,socket.SO_KEEPALIVE,Tr ue)

--
Grant Edwards grante Yow! Wow! Look!! A stray
at meatball!! Let's interview
visi.com it!
Jul 17 '06 #13
ok, yeah, thats in my book.
thanks, and no, it isn't enabled.
thanks again for everything
-sk
Grant Edwards wrote:
On 2006-07-17, ne*****@xit.net <ne*****@xit.netwrote:
If the server _application_ crashes or exits, then the OS will
close the socket and recv() will return "". If somebody powers
down the server without warning, or if the server OS crashes,
or if the Ethernet cable between the Internet and the server is
cut, then the socket will not be closed, and recv() will wait
forever[1].
Ok, yes all of the above is what i mean. Actually I am not too
concerned about a server os crash, or the cable being cut. But I have
had them close the connection on me, after which i just reconnect
(whenever i discover that its happened)
>[1] Unless you've enabled the TCP Keepalive feature, in which
case the socket will timeout in a couple hours and recv()
will return "".
if this is something that must be enabled, or is not enabled by
default, then it is not enabled.

On all OSes with which I'm familiar it's disabled by default.
You use a socket object's setsockopt method to enable it:

s.setsockopt(socket.SOL_TCP,socket.SO_KEEPALIVE,Tr ue)

--
Grant Edwards grante Yow! Wow! Look!! A stray
at meatball!! Let's interview
visi.com it!
Jul 17 '06 #14
ne*****@xit.net írta:
ok, yeah, thats in my book.
thanks, and no, it isn't enabled.
thanks again for everything
-sk
Another hint: use select.select() on the socket before reading. It will
tell you if recv() will block or not. This way you can implement your
own async timeout and do something else in your program while waiting
for the connection to be available.

Best,

Laszlo

Jul 18 '06 #15
Grant Edwards <gr****@visi.comwrites:
[...]
Often normal send() and recv() semantics have been mistaught.
An alert alien, looking at other common APIs in isolation,
might reasonably wonder whether there is some sort of
still_ok_to_use() sort of check as part of TCP. As it happens,
of course, that doesn't fit with the rest of socket networking,
which takes the "modernist" approach of trying send() or recv(),
and reporting any exception.

On most Unices there are some obscure API features that can be
used to generate a SIGPIPE under some vaguely specified error
conditions (e.g. TCP keepalive timeout). I've only read about
them and never tried to use them, since I couldn't see anything
in the description of the features that was any benefit over
the nomral send() and recv() usage.
Sorry for butting in, Grant, but this reminds me of this bug:

http://python.org/sf/1411097
and specifically, of my question in the tracker:

| One problem: I don't understand the need for
| HTTPConnection._safe_read(), rather than checking for an
| EINTR resulting from the recv() call (or WSAEINTR on
| Windows). Can anybody explain that?
Note the comment in the third paragraph in the method's docstring,
below ("Note that we cannot...", and the "if not chunk" test). Do you
know of any reason why it should do that (fail to check for EINTR)?
Looks wrong to me.

(I do understand that it's only signals that arrive before *any* data
is read that cause EINTR, but that's the specific case mentioned in
the 3rd para, and a check for that is what's conspicuously absent from
the method implementation.)

def _safe_read(self, amt):
"""Read the number of bytes requested, compensating for partial reads.

Normally, we have a blocking socket, but a read() can be interrupted
by a signal (resulting in a partial read).

Note that we cannot distinguish between EOF and an interrupt when zero
bytes have been read. IncompleteRead() will be raised in this
situation.

This function should be used when <amtbytes "should" be present for
reading. If the bytes are truly not available (due to EOF), then the
IncompleteRead exception can be used to detect the problem.
"""
s = []
while amt 0:
chunk = self.fp.read(min(amt, MAXAMOUNT))
if not chunk:
raise IncompleteRead(s)
s.append(chunk)
amt -= len(chunk)
return ''.join(s)
John
Jul 18 '06 #16

Grant Edwards wrote:
If the server has closed the connection, then a recv() on the
socket will return an empty string "",
after returning all the data the remote side had sent, of course.
and a send() on the
socket will raise an exception.
Send() might, and in many cases should, raise an exception
after the remote side has closed the connection, but the behavior
is unreliable.
--
--Bryan

Jul 20 '06 #17
In message <12*************@corp.supernews.com>, Grant Edwards wrote:
If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.
Would that still apply when trying to send an empty string?
Jul 20 '06 #18
Lawrence D'Oliveiro wrote:
In message <12*************@corp.supernews.com>, Grant Edwards wrote:

>>If the server has closed the connection, then a recv() on the
socket will return an empty string "", and a send() on the
socket will raise an exception.


Would that still apply when trying to send an empty string?
Yes: sending an empty string demands no action on the server's part, as
the receiver has already received it ... adding an empty string to
whatever the sender currently has buffered does not require any state
change on the sender's part.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jul 20 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, I use the System.Net.Sockets to send/receive data (no tcpclient/tcplistener), I made a receivethread in my wrapper, the receivethread loops/sleeps while waiting for data...
1
by: Dr. J | last post by:
I have an application that opens a socket and connects to another application listening over a port. The problem I am encountering is that when the listening application is closed my application...
1
by: D. Wichert | last post by:
Hi folks, I use a TcpClient object to connect to a local (asynchron) socket server. Once the connection is established, I ask every second if there is a new entry in the socket queue. If the...
2
by: Nuno Magalhaes | last post by:
socket.Connected only gives the last access result but not the current status of the socket. What is the best way to determine if a socket is still connected? Because I want to cancel all my...
1
by: Techsol | last post by:
Hi, I have synchronous communications between a server and client. To save bandwith the connection must persist. So the socket must stay open and only be re-opened in case of communications failure....
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
1
by: =?Utf-8?B?aXdkdTE1?= | last post by:
Hi, i have a socket that i use to listen for a connection. there can be any number of connections. when testing my socket, it sees the first connection, then no others. i call the BeginAccept()...
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
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
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
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.