title: c# tcpListener listen for connection status of multiple clients
body: <p>I am attempting to connect multiple clients to a <em>server</em> and monitor their connection...</p>
  
  <p>I am trying to get a better understanding of <code>TcpListener</code> and <code>TcpClient</code> while creating these programs.</p>
  
  <p>I found my server code from another stackoverflow answer as I am looking to get a connection from multiple clients, I have edited it abit:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  
  namespace socket_server
  {
      class connect
      {
          public class State
          {
              public Socket workSocket = null;
              public const int bufferSize = 1024;
              public byte[] buffer = new byte[bufferSize];
              public StringBuilder sb = new StringBuilder();
          }
  
          public class Server
          {
              public static ManualResetEvent allDone = new ManualResetEvent(false);
  
              private static IPEndPoint findMe()
              {
                  IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                  IPAddress ipAddress = ipHostInfo.AddressList[0];
                  IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 5505);
                  return localEndPoint;
              }
  
              public static void start()
              {
                  try
                  {
                      TcpListener listener = new TcpListener(findMe());
                      TcpClient client;
                      listener.Start();
  
                      Console.WriteLine("Server IP: {0}\n", findMe());
                      Console.WriteLine("Waiting for Connection...");
  
                      while (true)
                      {
                          client = listener.AcceptTcpClient();
                          ThreadPool.QueueUserWorkItem(threadProc, client);
                      }
  
                      Console.ReadKey();
                  } catch (Exception e)
                  {
                      Console.WriteLine(e.ToString());
                  }
              }
              private static void threadProc(object obj)
              {
                  try
                  {
                      var client = (TcpClient)obj;
                  } catch (Exception e)
                  {
                      Console.WriteLine(e.ToString());
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>You call this like <code>connect.Server.start()</code></p>
  
  <p>This is my current client code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  
  namespace socket_client
  {
      class connect
      {
          public class State
          {
              public Socket workSocket = null;
              public const int bufferSize = 256;
              public byte[] buffer = new byte[bufferSize];
              public StringBuilder sb = new StringBuilder();
          }
  
          public class Client
          {
              private const int port = 5505;    
              private static String response = String.Empty;
  
              private static IPEndPoint findServer()
              {
                  IPHostEntry ipHostInfo = Dns.Resolve("10.1.2.30");
                  IPAddress ipAddress = ipHostInfo.AddressList[0];
                  IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
                  return remoteEP;
              }
  
              public static void start()
              {
                  TcpClient client = new TcpClient();
  
                  Console.WriteLine("Server IP: {0}", findServer().ToString());
                  Console.WriteLine("Client IP: {0}\n", software.getIPAddress());
  
                  connect(client);
                  if (client.Connected)
                      Console.WriteLine("\nConnected to: {0}\n", findServer().ToString());
              }
  
              private static void connect(TcpClient client)
              {
                  try
                  {
                      client.Connect(findServer());
                  } catch(Exception e)
                  {
                      Console.WriteLine("Attempting to connect to: {0}", findServer().ToString());
                      connect(client);
                  }
  
              }
          }
      }
  }
  </code></pre>
  
  <p>You call this like <code>connect.Client.start();</code></p>
  
  <p>My classes such as <code>hardware</code> and <code>software</code> simply just get system information.</p>
  
  <p><strong>I would like to know how to check if a connection is dropped and respond with a <code>Console.WriteLine</code> for both Client and Server.</strong></p>
  
  <p><strong>Edit-</strong></p>
  
  <p>This is what I have <em>working</em> for checking to see if a client drops from the server:</p>
  
  <pre><code>...
  private static void threadProc(object obj)
  {
      try
      {
          var client = (TcpClient)obj;
  
          Byte[] bytes = new Byte[256];
          string data = null;
  
          try { 
              NetworkStream stream = client.GetStream();
  
              int i;
              while((i = stream.Read(bytes,0,bytes.Length)) != 0)
              {
                  data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                  Console.WriteLine("&gt;{0}: {1}", ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(), data);
              }
          } catch(Exception e)
          {
              Console.WriteLine("&gt;&gt;{0} Lost Connection...", ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
          }
      } catch (Exception e)
      {
          Console.WriteLine(e.ToString());
      }
  }
  ...
  </code></pre>