How to determine that connection is broken

P

puzzlecracker

Say I this client:

try{
Socket socket= new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
/*Assume there is a connection on the other end*/
while (socket.Connected)
{

/*Do some processing*/
}
}catch (SocketException se){
Console.WriteLine(ex.Message);
} catch (Exception ex){
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("something bad happen");
}

Now, assume that connection is broken on the other end( kill -9
server). It takes several minutes for the client to determine that
something is wrong. Socket.Connected returns true even though
connection is actually BROKEN.

What is the fastest way to determine that connection doesn't exist,
once the other end breaks the link?

I would appreciate an example of your solution...

Thanks
 
P

Pavel Minaev

Say I this client:

try{
      Socket socket= new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
     /*Assume there is a connection on the other end*/
      while (socket.Connected)
      {

          /*Do some processing*/
      }
 }catch (SocketException se){
       Console.WriteLine(ex.Message);
 } catch (Exception ex){
       Console.WriteLine(ex.Message);
 }
 finally
 {
   Console.WriteLine("something bad happen");
 }

Now, assume that connection is broken on the other end( kill -9
server). It takes several minutes for the client to determine that
something is wrong. Socket.Connected returns true even though
connection is actually BROKEN.

That's just the way TCP works. There's nothing you can do about it.
What is the fastest way to determine that connection doesn't exist,
once the other end breaks the link?

Assume that if there's no reply from the other side in some reasonable
time (depending on your needs and environment, this could be 1 second
or 1 minute), connection is broken. In other words, time-out.

Also, in general, you'll get the broken connection reported faster if
you try to write anything into that socket.
 

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