473,398 Members | 2,525 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,398 software developers and data experts.

Detecting socket connection failure

Hi All,

I've tried to RTFM this and am having no luck. First off, I am using
Mac OSX 10.4.7 with python 2.4.2 from fink. I am trying to connect to
a server that should be rejecting connections and I was surprised when
it did not throw an exception or do something otherwise equally nasty.
It just connects and never returns any data. First, the proof that
something is there and rejecting the connection (or is it that this
thing actually accepts the connection and then drops it?)...

telnet localhost 31414
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
Now if I fire up ipython and try to connect...

In [1]: import socket, select

In [2]: remote = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

In [3]: remote.connect(('localhost',31414))

In [4]: remote.recv(200)
Out[4]: ''

In [5]: r,w,e=select.select([remote],[remote],[remote],1)

In [6]: print r,w,e
[<socket._socketobject object at 0x7e48d0>] [<socket._socketobject
object at 0x7e48d0>] []

In [7]: remote.recv(200)
Out[7]: ''

So it looks like it will for ever say that it is ready for read and
write, but really is not. How do I detect this case? The recv may
really not have any data for a long time, so a recv of no bytes is not
a way to test the connection status. This is probably a FAQ, but I
can't figure it out.

Thanks!!
-kurt

Jul 10 '06 #1
4 5200
sc*****@gmail.com writes on 10 Jul 2006 08:42:11 -0700:
I've tried to RTFM this and am having no luck. First off, I am using
Mac OSX 10.4.7 with python 2.4.2 from fink. I am trying to connect to
a server that should be rejecting connections and I was surprised when
it did not throw an exception or do something otherwise equally nasty.
It just connects and never returns any data. First, the proof that
something is there and rejecting the connection (or is it that this
thing actually accepts the connection and then drops it?)...

telnet localhost 31414
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
What you see here is that the connection was opened successfully
(the connect succeeded) and then closed again.
...
In [1]: import socket, select

In [2]: remote = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

In [3]: remote.connect(('localhost',31414))

In [4]: remote.recv(200)
Out[4]: ''
The means, that you see the same in Python:
"recv" returning an empty string indicates that the connection
was closed.
>
In [5]: r,w,e=select.select([remote],[remote],[remote],1)

In [6]: print r,w,e
[<socket._socketobject object at 0x7e48d0>] [<socket._socketobject
object at 0x7e48d0>] []
I have seen something similar recently:

I can write ("send" to be precise) to a socket closed by
the foreign partner without error
(but of course, the written data does not arrive at the remote side).
Only the second "send" raises an exception.

I expect this is a TCP bug.
--
Dieter
Jul 15 '06 #2
Hi Dieter,

Thanks for the feedback. Were you also using mac osx? I am wondering
at what level this bug is occuring.

-kurt

Dieter Maurer wrote:
sc*****@gmail.com writes on 10 Jul 2006 08:42:11 -0700:
I've tried to RTFM this and am having no luck. First off, I am using
Mac OSX 10.4.7 with python 2.4.2 from fink. I am trying to connect to
a server that should be rejecting connections and I was surprised when
it did not throw an exception or do something otherwise equally nasty.
It just connects and never returns any data. First, the proof that
something is there and rejecting the connection (or is it that this
thing actually accepts the connection and then drops it?)...

telnet localhost 31414
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

What you see here is that the connection was opened successfully
(the connect succeeded) and then closed again.
...
In [1]: import socket, select

In [2]: remote = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

In [3]: remote.connect(('localhost',31414))

In [4]: remote.recv(200)
Out[4]: ''

The means, that you see the same in Python:
"recv" returning an empty string indicates that the connection
was closed.

In [5]: r,w,e=select.select([remote],[remote],[remote],1)

In [6]: print r,w,e
[<socket._socketobject object at 0x7e48d0>] [<socket._socketobject
object at 0x7e48d0>] []

I have seen something similar recently:

I can write ("send" to be precise) to a socket closed by
the foreign partner without error
(but of course, the written data does not arrive at the remote side).
Only the second "send" raises an exception.

I expect this is a TCP bug.
--
Dieter
Jul 19 '06 #3
sc*****@gmail.com wrote:
First, the proof that
something is there and rejecting the connection (or is it that this
thing actually accepts the connection and then drops it?)...
Yes, it accepts it and then drops it, or perhaps drops it after
receiving some data? It's not a failed or rejected connection from a
socket point of view, however.
In [4]: remote.recv(200)
Out[4]: ''
Assuming I understand the socket module, given how under-documented it
is, I assume this means the socket has now been closed.
How do I detect this case? The recv may
really not have any data for a long time, so a recv of no bytes is not
a way to test the connection status.
You already received zero bytes the first time, which I believe means
the socket is closed, and you shouldn't pass it to select a second
time. You should never get zero bytes unless the socket is closed,
otherwise it would just sit there and wait until it has some bytes to
return to you. Select doesn't tell you that there's data waiting -
obviously it can't, as how would it handle the write case? - but in
fact tells you that the socket is 'ready', and operations upon it
should return immediately. And 'ready' in this case could well just
mean it's ready to tell you that it's not connected.

--
Ben Sizer

Jul 20 '06 #4
sc*****@gmail.com writes on 19 Jul 2006 08:34:00 -0700:
...
Were you also using mac osx?
No, I have observed the problem under Linux.

Dieter Maurer wrote:
....
I have seen something similar recently:

I can write ("send" to be precise) to a socket closed by
the foreign partner without error
(but of course, the written data does not arrive at the remote side).
Only the second "send" raises an exception.

I expect this is a TCP bug.
Jul 24 '06 #5

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

Similar topics

0
by: Colin Brown | last post by:
Python2.3.2: Running the following receiver and sender code in separate windows under Win2K does not work properly (missed & very delayed transmissions). Under Redhat Linux 9 (where I will be using...
6
by: Michael Kennedy [UB] | last post by:
Hi, I have a project using the TcpClient and its associated NetworkStream. Everything works well except for one condition which I haven't found any information about dealing with: How do I...
0
by: Jacob Lee | last post by:
I'm getting a rather bizarre error while using the socket module. If I start out disconnected from the net and then connect while the program or interpreter session is open, I do not always gain...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
0
by: Macca | last post by:
Hi, I am using an asynchronous socket server for my comms.I have multiple clients that connect to my server and hopefully stay connected sending data approx every 500ms. I want to make it...
5
by: Kurt | last post by:
I have a client & server app which communicates using the socket class. If I shutdown the server closing the socket the client still thinks the socket is open. Even calling send does not throw an...
9
by: timor.super | last post by:
Hi group, I've written a client/server application, using the dotnet sockets. In my server, I have a thread waiting for messages with : ret = currSocket.Receive(buffer, 1024,...
3
by: A. W. Dunstan | last post by:
I'm creating a socket as follows: m_networkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); m_networkSocket.LingerState = new LingerOption(true, 1);...
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: 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
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
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.