How to determine that connection is broken

  • Thread starter Thread starter puzzlecracker
  • Start date Start date
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
 
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.
 
Back
Top