472,958 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Detecting shutdown of remote socket endpoint.

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?
Jul 18 '05 #1
1 7374
Tim Gosselin <go******@norwich.edu> 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
Jul 18 '05 #2

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

Similar topics

0
by: Hansi | last post by:
How can i start a winforms application on remote workstation, no matter who or if there is somebody logged in? My problems: 1) I need to shutdown computers in the evening that have not been...
1
by: Graham | last post by:
Can anyone tell me how to detect when a client socket connection has failed (ie remote equipment power failure) using VB.net. Thanks in advance
5
by: Rich | last post by:
I have a simple, single threaded program in C++ that runs as Idle priority. The program creates some data which needs dumping to file if the computer is shutdown or logged out. At the moment...
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...
5
by: cashdeskmac | last post by:
I want to detect when the users computer is going to close down so that I can write all unfinished data to a file before closing down. Does anyone know how this is done?
0
by: Jerry Camel | last post by:
Using SessionEnding, I can determine if the users session is ending via logoff or shutdown, but it doesn't differentiate between shutdown and restart... Shutdown and Restart both show the same...
6
by: Terry Olsen | last post by:
I have my socket program working...I can send & receive data, I can terminate the connection, etc. But I can't figure out how to tell if the remote client has disconnected. I tried using...
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: PAzevedo | last post by:
Hi there. I called Socket.Select() on a group of sockets, and got one that was writable. On that socket i called Send() and sent a buffer of size smaller then SendBufferSize that way Send()...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.