Tcp socket connection broken

D

Droopy

Hi,

I developed an application in C# (VS 2003) using Tcp sockets.
What is the more reliable way to know when a Tcp connection is broken.
My code looks like this :

while (_mustRun)
{
if (_socket.Poll (PollTimeout, SelectMode.SelectRead))
{
if (_socket.Available == 0)
{
// connection closed or failed
break;
}

if (_socket.Available >= needed bytes)
{
_socket.Receive (...)
}

}
}

I am using "Poll" because I want to be able to stop my thread by simply
setting my "_mustRun" boolean variable instead of aborting the thread.

It seems to work (when the peer application is closed or killed) but not
when the "peer host" is brutally shutdown (by removing power cable for
example).

Thanks in advance for your help.

Droopy.
 
S

Stelrad Doulton

A vanillla TCP connection will not be aware that the other end has died
rather than just being inactive.

Unless the other/server side initiates the "closing" sequence your client
will assume that your server/connection is just idle.

The only way to determine that the connection is no longer valid is to try
and send something. Fortunatley TCP allows for this by giving you TCP
"keep-alives", this is simply and ACK packet with a sequence number of 1
less thean the correct current sequence number.

Google on "SetSocketOption keepalive"
 
D

Droopy

A vanillla TCP connection will not be aware that the other end has
died rather than just being inactive.

Unless the other/server side initiates the "closing" sequence your
client will assume that your server/connection is just idle.

The only way to determine that the connection is no longer valid is to
try and send something. Fortunatley TCP allows for this by giving you
TCP "keep-alives", this is simply and ACK packet with a sequence
number of 1 less thean the correct current sequence number.

Google on "SetSocketOption keepalive"

I already thought to use KeepAlive.
But, as I understood, I can only set it on or off with a default value of
2 hours !
It is possible to change this default value in the registry but it is not
acceptable (it is for all sockets).
I am going to think a bit more about this problem.
May be it is possible to solve it in another way.
I will come back with more precise questions if needed.

Thanks a lot for your help.
 
C

Chad Z. Hower aka Kudzu

Droopy said:
I already thought to use KeepAlive.

You dont need to - TCP already does of sorts internally. But you just need to try to read from a
socket once in a while. I have in fact a whole topic on this in my book - but Im dont think I've included
that in any of the free articles on the web.
May be it is possible to solve it in another way.
I will come back with more precise questions if needed.

Try to read from the socket - if its closed, you will receive an error.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top