title: How to test for a broken connection of TCPClient after being connected?
body: <p>I've been fighting with one problem for a whole 3 days I can't find any solution, please help :)<br>
  I work with Visual Studio 2010 and C# language.</p>
  
  <p>I have a device working like a server, that sends some data in a very irregular periods of time (not possible to define any read timeout).<br>
  I wrote a TCP client to connect to that server and read data. It works OK, however when something is wrong with the network and server becomes unavailable (e.g. when I plug out the network cable from my computer), it takes about 10 seconds for application to "notice" there is no connection to the server and throw an exception. (I don't know why exactly 10 seconds? Where it's defined? Can I change it?)<br>
  I want to react faster - let say after one second after connection broken.<br>
  Googling for answer however doesn't provide me any working solution.  </p>
  
  <p>The test code is below, I try to make it on 2 threads: one is reading data, the second one is looking for connection status and should alarm me when it's broken. It's not working neither for TCPClient nor Socket class. I've tried to read / write some data with <code>tcpClient.SendTimeout = 100;</code> and <code>stream.WriteTimeout = 100;</code> but it doesn't seem to work.</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net.Sockets;
  using System.Threading;
  
  namespace TCPClient
  {
      class Program
      {
          static volatile bool _continue = true;
          static TcpClient tcpClient;
          static NetworkStream stream;
  
          static void Main(string[] args)
          {
              try
              {
                  //client thread - listen from server
                  Thread tcpListenThread = new Thread(TcpListenThread);
                  tcpListenThread.Start();
  
                  //connection checking thread
                  Thread keepAliveThread = new Thread(KeepAliveThread);
                  keepAliveThread.Start();
  
                  while (_continue)
                  {
                      if (Console.ReadLine() == "q")
                      {
                          _continue = false;
                      }
                  }
  
                  keepAliveThread.Join(2000);
                  if (keepAliveThread.IsAlive)
                  {
                      Console.WriteLine("Thread keepAlive has been aborted...");
                      keepAliveThread.Abort();
                  }
  
                  tcpListenThread.Join(2000);
                  if (tcpListenThread.IsAlive)
                  {
                      Console.WriteLine("Listen thread has been aborted...");
                      tcpListenThread.Abort();
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine("\n" + ex.Message);
              }
  
              Console.WriteLine("\nHit any key to quit...");
              Console.Read();
          }
  
          private static void TcpListenThread()
          {
              string server = "172.20.30.40";
              int port = 3000;
  
              try
              {
                  using (tcpClient = new TcpClient())
                  {
                      tcpClient.Connect(server, port);
  
                      if (tcpClient.Connected)
                          Console.WriteLine("Successfully connected to server");
  
                      using (stream = tcpClient.GetStream())
                      {
  
                          while (_continue)
                          {
                              Byte[] data = new Byte[1024];
                              Int32 bytes = stream.Read(data, 0, data.Length);
                              string responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
  
                              Console.WriteLine("Received: {0}, responseData);
                          }
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Listen thread exception! " + ex.Message);
              }
          }
  
  
          private static void KeepAliveThread()
          {
              while (_continue)
              {
                  if (tcpClient != null)
                  {
                      try
                      {
                          //...what to put here to check or throw exception when server is not available??...
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine("Disconnected...");
                      }
                  }
  
                  Thread.Sleep(1000);  //test for connection every 1000ms
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong><br>
  @carsten's answer: although it looks promising, this solution do not work...<br>
  I made the simplest test application for that:  </p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net.Sockets;
  using System.Threading;
  
  namespace TCPClientTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              try
              {
                  string server = "172.20.30.40";
                  int port = 3000;
  
                  using (TcpClient tcpClient = new TcpClient())
                  {
                      tcpClient.Connect(server, port);
  
                      int i = 0;
                      while (true)
                      {
                          // This is how you can determine whether a socket is still connected.
                          bool blockingState = tcpClient.Client.Blocking;
                          try
                          {
                              byte[] tmp = new byte[1];
  
                              tcpClient.Client.Blocking = false;
                              tcpClient.Client.Send(tmp, 0, 0);
                              Console.WriteLine("Connected!");
                          }
                          catch (SocketException e)
                          {
                              // 10035 == WSAEWOULDBLOCK
                              if (e.NativeErrorCode.Equals(10035))
                                  Console.WriteLine("Still Connected, but the Send would block");
                              else
                              {
                                  Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
                              }
                          }
                          finally
                          {
                              tcpClient.Client.Blocking = blockingState;
                          }
  
                          Console.WriteLine("Connected: {0} ({1})", tcpClient.Client.Connected, i++);
  
                          Thread.Sleep(1000);
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Global exception: {0}", ex.Message);
              }
          }
      }
  }
  </code></pre>
  
  <p>The results are following, it displays:  </p>
  
  <p><strong>Connected!<br>
  Connected: True</strong></p>
  
  <p>plus my order number every one second. When I disconnect network cable, it takes 8 seconds to start printing:  </p>
  
  <p><strong>Disonnected: error code 10054!<br>
  Connected: False</strong>  </p>
  
  <p>so by 8 seconds I'm not aware that the connection is lost. It looks like pinging is the best option here, yet I'll test another solutions.</p>