473,387 Members | 1,549 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,387 software developers and data experts.

How to tell when a socket is closed on the other end?

Hi there.
I'm setting up test suite for a project of mine.
>From test suite, acting as a client, I'd like to know, in certain
situations, if the socket is closed on the other end or not.
I noticed that I can "detect" such state if a call to socket.read()
returns 0 but it seems a little poor to me. :-\
Is there a reliable way to test such socket 'state'?

Jul 25 '07 #1
6 15526
In article <11**********************@19g2000hsx.googlegroups. com>,
billiejoex <gn****@gmail.comwrote:
Hi there.
I'm setting up test suite for a project of mine.
From test suite, acting as a client, I'd like to know, in certain
situations, if the socket is closed on the other end or not.
I noticed that I can "detect" such state if a call to socket.read()
returns 0 but it seems a little poor to me. :-\
Is there a reliable way to test such socket 'state'?
This isn't really a Python question, it's a Berkeley Socket API question.
You don't say, but I assume you're talking about a TCP (i.e. SOCKSTREAM)
connection?

The answer is you can use the select() system call to detect "exceptional
conditions" on a socket. Python's select module provides this
functionality, but to understand how to use it, you need to study the
underlying API.

On the other hand, socket.read() returning 0 works too. What do you find
"poor" about that? What do you want to know about the connection being
closed that you don't find out by getting 0 back from read()?
Jul 25 '07 #2
Roy Smith wrote:
In article <11**********************@19g2000hsx.googlegroups. com>,
billiejoex <gn****@gmail.comwrote:
>Hi there.
I'm setting up test suite for a project of mine.
>From test suite, acting as a client, I'd like to know, in certain
situations, if the socket is closed on the other end or not.
I noticed that I can "detect" such state if a call to socket.read()
returns 0 but it seems a little poor to me. :-\
Is there a reliable way to test such socket 'state'?

This isn't really a Python question, it's a Berkeley Socket API question.
You don't say, but I assume you're talking about a TCP (i.e. SOCKSTREAM)
connection?

The answer is you can use the select() system call to detect "exceptional
conditions" on a socket. Python's select module provides this
functionality, but to understand how to use it, you need to study the
underlying API.

On the other hand, socket.read() returning 0 works too. What do you find
"poor" about that? What do you want to know about the connection being
closed that you don't find out by getting 0 back from read()?
Sockets don't have a read method unless you have used makefile() or have
made them into ssl sockets. They don't return 0, they return empty
strings. If they have a timeout, or the reads were somehow interrupted,
they can return empty strings without the other end having closed the
connection.

There are 3 methods of determining if the remote machine has
closed/shutdown a socket. You offer one, select.select([], [], [sock],
timeout) . The others are to pass that same socket to check whether one
can read or write to the socket. If a socket is readable by select, yet
a recv() produces zero bytes, then the other end has at least signaled
that it is no longer sending (they have performed shutdown(1) or
close()). If a socket is writable by select, yet doesn't accept any
bytes in send(), then the other end has at least signaled that it is no
longer receiving (they have performed shutdown(0) or close()).

In many cases, people only care if *both* directions are open, which is
why asyncore closes the connection if a send or receive do not produce
or consume bytes.

- Josiah
Jul 25 '07 #3
On 25 Lug, 16:37, Roy Smith <r...@panix.comwrote:
This isn't really a Python question, it's a Berkeley Socket API question.
You don't say, but I assume you're talking about a TCP (i.e. SOCKSTREAM)
connection?
Yes.
The answer is you can use the select() system call to detect "exceptional
conditions" on a socket. Python's select module provides this
functionality, but to understand how to use it, you need to study the
underlying API.

On the other hand, socket.read() returning 0 works too. What do you find
"poor" about that? What do you want to know about the connection being
closed that you don't find out by getting 0 back from read()?
'poor' because it's 'tricky', since that send/write() and recv/read()
should be used for other tasks...
As far as I can tell this works on Linux and Windows, but I don't know
on other platforms.

Jul 25 '07 #4
Roy Smith wrote:
In article <11**********************@19g2000hsx.googlegroups. com>,
billiejoex <gn****@gmail.comwrote:
>Hi there.
I'm setting up test suite for a project of mine.
>From test suite, acting as a client, I'd like to know, in certain
situations, if the socket is closed on the other end or not.
I noticed that I can "detect" such state if a call to socket.read()
returns 0 but it seems a little poor to me. :-\
Is there a reliable way to test such socket 'state'?

This isn't really a Python question, it's a Berkeley Socket API question.
You don't say, but I assume you're talking about a TCP (i.e. SOCKSTREAM)
connection?

The answer is you can use the select() system call to detect "exceptional
conditions" on a socket. Python's select module provides this
functionality, but to understand how to use it, you need to study the
underlying API.
Thanks for the interesting information and suggestion of using select(). You are correct that this is actually mostly a socket API question but pertains to Python since the code is all Python's socket and asyncore modules. It might help to step back and explain the original problem. The goal of this portion of the test suite we are writing for the project is to determine if a remote server is behaving properly by closing a socket from the server side based on a client-side command.

Really what's needed is a way to make sure the socket gets closed, and preferably determine if it was closed from the remote end as expected. Do you know if this is possible to determine from the client side reliably/accurately? Would select()'s exceptional condition flag actually indicate whether or not the root cause of the condition was a socket closed by the remote peer? I've read through the select's manpage and I can't seem to find a reference that indicates what the possible values are for the I/O descriptor sets returned by select. Is there another man page, or a place in the header file for select I can look?

Thanks for your help,

-Jay
Jul 26 '07 #5
Jay Loden wrote:
Roy Smith wrote:
>In article <11**********************@19g2000hsx.googlegroups. com>,
billiejoex <gn****@gmail.comwrote:
>>Hi there.
I'm setting up test suite for a project of mine.
From test suite, acting as a client, I'd like to know, in certain
situations, if the socket is closed on the other end or not.
I noticed that I can "detect" such state if a call to socket.read()
returns 0 but it seems a little poor to me. :-\
Is there a reliable way to test such socket 'state'?
This isn't really a Python question, it's a Berkeley Socket API question.
You don't say, but I assume you're talking about a TCP (i.e. SOCKSTREAM)
connection?

The answer is you can use the select() system call to detect "exceptional
conditions" on a socket. Python's select module provides this
functionality, but to understand how to use it, you need to study the
underlying API.

Thanks for the interesting information and suggestion of using select(). You are correct that this is actually mostly a socket API question but pertains to Python since the code is all Python's socket and asyncore modules. It might help to step back and explain the original problem. The goal of this portion of the test suite we are writing for the project is to determine if a remote server is behaving properly by closing a socket from the server side based on a client-side command.

Really what's needed is a way to make sure the socket gets closed, and preferably determine if it was closed from the remote end as expected. Do you know if this is possible to determine from the client side reliably/accurately? Would select()'s exceptional condition flag actually indicate whether or not the root cause of the condition was a socket closed by the remote peer? I've read through the select's manpage and I can't seem to find a reference that indicates what the possible values are for the I/O descriptor sets returned by select. Is there another man page, or a place in the header file for select I can look?
Use select to determine if the socket is readable and writable. If it
is, yet you can't send or receive to/from it, then it is closed.

- Josiah
Jul 27 '07 #6
Jay Loden <py****@jayloden.comwrote:
The goal of this
portion of the test suite we are writing for the project is to determine if a
remote server is behaving properly by closing a socket from the server side
based on a client-side command.

Really what's needed is a way to make sure the socket gets closed, and
preferably determine if it was closed from the remote end as expected.
This really is way out of scope for a Python newsgroup, but what the heck.
There is no way to tell through the Socket API *why* the connection was
shut down, because this information isn't transmitted by the TCP protocol.
All you know is that the connection did indeed get shut down.

It could be because the user code at the remote end called close(). Or, it
could be because the process exited (normally or abnormally) and the kernel
closed the connection as part of the cleanup. All the TCP stack at this
end knows is it got a packet with the FIN bit set.

If you really want to know if the other end completed normally, you need to
design your user-level protocol to include some "end of session"
indication. For example:
bash-3.2$ telnet mx.panix.com smtp
Trying 166.84.1.72...
Connected to mx.panix.com.
Escape character is '^]'.
220 mail1.panix.com ESMTP Postfix
helo foo
250 mail1.panix.com
quit
221 Bye
Connection closed by foreign host.
The SMTP user-level protocol sent "221 Bye", then, my telnet client saw the
actual TCP connection close, and printed the "Connection closed by foreign
host" message. I know the remote end closed down the connection normally
because I saw the "221" message in response to my "quit" command.
Do you
know if this is possible to determine from the client side
reliably/accurately? Would select()'s exceptional condition flag actually
indicate whether or not the root cause of the condition was a socket closed
by the remote peer? I've read through the select's manpage and I can't seem
to find a reference that indicates what the possible values are for the I/O
descriptor sets returned by select. Is there another man page, or a place in
the header file for select I can look?
You need to read up about how TCP/IP works. A good place to start might be
the Wikipedia article on "Transmission Control Protocol". The canonical
textbook on the subject would be:

Richard Stevens
UNIX Network Programming, Volume 1, Second Edition:
Networking APIs: Sockets and XTI
Prentice Hall
1998
ISBN 0-13-490012-X
Jul 27 '07 #7

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

Similar topics

4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
0
by: Johann Blake | last post by:
I am using the TcpClient to connect to a web site. I then open a NetworkStream and read the contents that are being sent back. The problem is that I have no idea when the remote host is finished...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
1
by: rooster575 | last post by:
I would like to force a socket closed if it is currently open. For arguments sake, say a function failed to close a socket and another function would like to use this same socket. I get the...
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...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program (myProblemProgram) that has a socket connected to...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
2
by: White Spirit | last post by:
I have a function within an application where a client connected to a server continuously sends data. The code on the server side is of the following form: Socket socket = receiveSocket;...
2
by: jlparise | last post by:
Hey everyone, I am really rusty on my Java socket programming and am having some confusing problems. I don't need the code debugged(even though it doesn't work). I'm not going to make you dig...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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,...

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.