473,839 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.xx x'
serverport = 9520
aeris_sockobj = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
aeris_sockobj.c onnect((serverh ost,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 64529
On 2006-07-16, ne*****@xit.net <ne*****@xit.ne twrote:
serverhost = 'xxx.xxx.xxx.xx x'
serverport = 9520
aeris_sockobj = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
aeris_sockobj.c onnect((serverh ost,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.ne twrote:
serverhost = 'xxx.xxx.xxx.xx x'
serverport = 9520
aeris_sockobj = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
aeris_sockobj.c onnect((serverh ost,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.r ecv(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.ne twrote:
>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.r ecv(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.ne twrote:
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.r ecv(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.supernew s.com>,
Grant Edwards <gr****@visi.co mwrote:
>On 2006-07-16, ne*****@xit.net <ne*****@xit.ne twrote:
>serverhost = 'xxx.xxx.xxx.xx x'
serverport = 9520
aeris_sockob j = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
aeris_sockobj. connect((server host,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.ne twrote:
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.ne twrote:
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

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

Similar topics

3
4647
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 and then fires a datareceived event. Within the waitingloop there is a timeout function, but I want the the 'last-time-socket-used' variable set when the socket is finished sending. When I send by System.Net.Sockets.Socket.Send(buffer()) (<--this...
1
16482
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 cannot detect it to take appropriate action. I am using "Connected" property of the Socket class, but it seems this property does not reflect the true state of the socket connection. Here is the code snippet. Basically, I am checking the...
1
6818
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 connection was established successfully at the beginning and ends for some reason after a certain time, I have to reconnect to the socket server. So, what I need therefore is a TcpClient attribute such as "Connected" that returns true if...
2
6133
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 "output" in the client if the socket is not connected. What is the best way to determine if a socket is still active on the server side? When I close my server clients will still do their jobs and I don't want that to happen.
1
2430
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. To simulate failure, the server disconnects the socket. However, the client socket parameter shows an open socket: socket.connected is true, socket. Active is true, socket.poll(1,selectRead) is true. When checking the MS class documentation, it is...
7
16951
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 write an helper method that will tell if the socket is connected or not , but it's not working good continue to tell me that the socket is connectedeven if the other party already call shutdown(both) + close , or , even if the other party close the...
1
1505
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() method again, but it doesnt see any other connections. heres the code... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ep As New IPEndPoint(ipHome, PORT)
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10911
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, 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...
0
10589
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10298
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 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...
0
9429
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7833
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 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...
0
5683
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...
2
4066
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.