473,473 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 7542
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
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
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.