473,782 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s ocket.AF_INET,s ocket.SOCK_STRE AM)

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

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

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

In [6]: print r,w,e
[<socket._socket object object at 0x7e48d0>] [<socket._socket object
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 5228
sc*****@gmail.c om 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(s ocket.AF_INET,s ocket.SOCK_STRE AM)

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

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.se lect([remote],[remote],[remote],1)

In [6]: print r,w,e
[<socket._socket object object at 0x7e48d0>] [<socket._socket object
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.c om 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(s ocket.AF_INET,s ocket.SOCK_STRE AM)

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

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.se lect([remote],[remote],[remote],1)

In [6]: print r,w,e
[<socket._socket object object at 0x7e48d0>] [<socket._socket object
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.c om 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.c om 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
1642
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 it) it works fine (with up to 250 sending threads; I hit the thread limit at 255!) Any idea why do I not get any errors reported under Windows? Colin Brown PyNZ
6
18507
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 detected when the socket connection was closed on the server-side? That is, if my client connects to the server and everything is initialized
0
1948
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 the ability to resolve or connect to hosts by name. Here is the problematic scenario: <Unplug network cable> >>> from socket import * >>> gethostbyname('google.com') # Fails as expected Traceback (most recent call last):
5
3686
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 but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are too basic. My programming environment is Windows XP, Visual Studio .NET 2003 and C#. So here it...
1
700
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
1726
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 robust as possible and was wondering how I would efficently detect a socket failure/disconnection and recover from it. Would it be up to the client to re-establish the connection?
5
3109
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 exception. Thanks Kurt
9
5575
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, SocketFlags.None); When the client exits, I close the socket with a specific message (like "end") and the thread terminate in a proper manner, but If my client crashes, the server is still waiting for receiving data, and
3
3056
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); m_networkSocket.Blocking = true; m_networkSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true); m_networkSocket.SetSocketOption(SocketOptionLevel.Socket,
0
9643
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, 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...
0
10313
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...
1
10081
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,...
0
9946
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
8968
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
7494
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
6735
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();...
0
5378
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.