Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Detecting shutdown of remote socket endpoint.

Reply
Thread Tools

Detecting shutdown of remote socket endpoint.

 
 
Tim Gosselin
Guest
Posts: n/a
 
      01-11-2005
I am writing a tcp tunnel but cannot find a way of detecting when a socket
shuts down its read end without writing to the socket. For testing the
write end of the remote endpoint I just do a:

if not sock.recv(buffsize)

I cannot write to the socket and check if send returns 0 though, because
that involves sending data out of the which may not have come in yet.

When I try polling the socket with:
r, w, e=select([],[sock],[], 0)
w returns with the socket still writable, even if the other end was
closed.

#For example:
s=socket()
s.bind(('localhost', 5000))
s.listen(5)
s2, addr=s.accept()

#then create a socket to connect,
s=socket()
s.connect(('localhost', 5000))

#then I close the server side,
s2.close() #or s2.shutdown(0)

#and poll s to see if it's still writable,
r, w, e=select([],[s],[], 0)
#this returns that the socket is writable! and i can even write to it,
s.send('test') #succeeds
#but the second write finally throws an exception.

Is there any other way to check if a the remote endpoint on a socket has
shutdown its read end? Is it difficult to implement a sock.is_shutdown(1)
with some additional c code?
 
Reply With Quote
 
 
 
 
Michael Hobbs
Guest
Posts: n/a
 
      01-12-2005
Tim Gosselin <(E-Mail Removed)> wrote:
> I am writing a tcp tunnel but cannot find a way of detecting when a socket
> shuts down its read end without writing to the socket. For testing the
> write end of the remote endpoint I just do a:
>
> if not sock.recv(buffsize)
>
> I cannot write to the socket and check if send returns 0 though, because
> that involves sending data out of the which may not have come in yet.
>
> When I try polling the socket with:
> r, w, e=select([],[sock],[], 0)
> w returns with the socket still writable, even if the other end was
> closed.


Even at the C level, I believe there were some differences between
Unix and Windows sockets in regard to this, so this advice may be
dependent on your platform.

At any rate, on both Unix and Windows, a closed socket will continue
to report itself as writable. To detect the closed socket, I believe
that one system reports the socket as being in error whereas the
other system reports the socket as being readable (where read() will
then immediately return 0 because the socket is closed).

So in summary, instead of:
r, w, e=select([],[sock],[], 0)
try this:
r, w, e=select([sock],[],[sock], 0)
If r or e is non-empty, then the socket has been closed (or there's
some other error).

HTH,
- Mike
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
Socket / ServerSocket & Remote Disconnection Detecting Jason Teagle Java 12 10-05-2003 10:23 AM



Advertisments