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

Detecting when a socket has been closed.

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
Jul 27 '06 #1
5 3065
Kurt wrote:
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.
The Receive function returns the number of bytes received. When the
connection has been closed it returns 0. Can you get it to work that way?

Max
Jul 27 '06 #2
If I am using BeginRecieve it won't fire until data is received.

"Markus Stoeger" <sp******@gmx.atwrote in message
news:OW**************@TK2MSFTNGP05.phx.gbl...
Kurt wrote:
>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.

The Receive function returns the number of bytes received. When the
connection has been closed it returns 0. Can you get it to work that way?

Max

Jul 27 '06 #3
Kurt wrote:
If I am using BeginRecieve it won't fire until data is received.
It also fires when the connection is closed. In that case the call to
EndReceive will return 0.

You should also put a try/catch around EndReceive. If it throws, you can
take it as closed too.

Max
Jul 27 '06 #4
Kurt wrote:
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

Hi,
First send will not throw exception, because there is no way for TCP
stack to know that other end has actually closed its receiving side.
Second send may throw or may not, depending on the timing. It takes some
time for packet to reach other side and return information that server
side is gone (it may take as long as 2-3 minutes). This is by design of
TCP protocol.

Few things you should note:
a) if send succeeds, that doesn't mean data is received by other side.
It means only that it is buffered by the OS
b) only when TCP stack timeouts on send (if foreign computer becomes
unreachable due to connection going down) TCP will know something is
wrong. You will not be notified until next operation (send or receive).
This may take minutes.
c) in case other end has closed socket and network is still operating
after first send TCP RST segment will be returned to TCP stack notifying
it connection has gone down. That send will succeed however, next should
fail. This takes also some time, though generally is much faster than
TCP timeout mechanism.
d) on idle connection, there is no exchange of data over socket.
Meaning: if you pull out the cable of server computer, you will never be
notified of this unless you try to send something (read will block
forever). First send will succeed, than after timeout next send/read
should fail. There is a keepalive option that can prevent this.
e) if you pull cable of client computer, you may get immediate
notification due to windows media sense feature.
f) Markus is right, async receive should complete when connection
tear-down is detected. But when exactly it is detected, is different story.
g) to close connection gracefully, both server and client must follow
some constraints. Check:
http://msdn.microsoft.com/library/de..._closure_2.asp
Regards,
Goran

Jul 27 '06 #5
That works.

Thanks!

"Markus Stoeger" wrote:
Kurt wrote:
If I am using BeginRecieve it won't fire until data is received.

It also fires when the connection is closed. In that case the call to
EndReceive will return 0.

You should also put a try/catch around EndReceive. If it throws, you can
take it as closed too.

Max
Jul 27 '06 #6

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

Similar topics

1
by: Tim Gosselin | last post by:
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...
6
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...
6
by: Bruce Vander Werf | last post by:
I am using the asynchronous send/receive methods of the Socket class. When the remote end closes the socket, the callback for receive is called and EndReceive returns 0. Socket.Connected still...
1
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
4
by: schwehr | last post by:
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...
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: O.B. | last post by:
In the following code snippet, the thread successfully makes it to the line where it waits for data to be received. Then the client closes the connection. The thread wakes up and returns from the...
5
by: tichi | last post by:
I'm writing a program that in its most simplest form acts as a proxy between a local client and a remote server. But I cannot figure out how to detect when either the connection to the client is...
11
Plater
by: Plater | last post by:
I'm having a bit of trouble figuring out how to detect if a socket is closed or not. Outside of using read()/write(). I also don't want to set the socket to non-blocking if at all possible. I wrote...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.