title: C# Async TCP Server overkill?
body: <p>This is really an implementation question so I feel it's best to start with my specific case. </p>
  
  <p>I've got a C# server that listens for TCP connections asynchronously from mobile clients. When a mobile client connects a new thread is started, the client sends a little (&lt;100 bytes usually) text message and receives one of similar size back. After the server responds, it closes the connection and ends the thread. </p>
  
  <p>Current basic usage is a user logs in, checks on stuff for sometimes up to 5 minutes, <strong><em>sending little messages and thus creating new threads on the server in rapid succession</em></strong>, and they disconnect only to reconnect a few hours later. Also, every user has their own server they run on their PC, and as such most servers will only ever have one client connected at any given time, in RARE cases two.</p>
  
  <p>Right now I'm running into the following error, <strong>An existing connection was forcibly closed by the remote host</strong>, and it has got me thinking, <em>am I doing this wrong?</em></p>
  
  <p>So my question(s):</p>
  
  <ol>
  <li>Is my current setup appropriate here?</li>
  <li>If so, should I be ending the thread after a little message is sent or keeping it alive and closing after a given period of idle?</li>
  <li>On the off chance that I'm doing everything correctly, <strong>highly unlikely</strong>, should I be avoiding the error by simply retrying a few times before giving up? </li>
  <li>Fourth and finally, that error completely kills the server (the server is spawned by another process and any untrapped exception kills it), if we've made it this far, and my implementation is OK, how can I avoid that?</li>
  </ol>
  
  <h1>EDIT:</h1>
  
  <p>In response to some of the questions here:</p>
  
  <ul>
  <li>The exception is occurring before I receive all the data, but only in instances where a user has sent multiple messages in quick succession.</li>
  <li>From what I recall max backlog is 5 unless a user is running Windows Server, however I have not set mine and I don't know what the default is, I'll try explicitly setting it to 5.</li>
  </ul>
  
  <h2>Async Server Code:</h2>
  
  <pre><code>    public void StartListening()
      {
          //Data buffer for incoming data.
          byte[] bytes = new Byte[1024];
  
          //Establish the local endpoint for the socket.
          IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
          IPAddress ipAddress = ipHostInfo.AddressList[0];
          IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Port);
  
          //Create a TCP/IP socket.
          Socket listener = new Socket(AddressFamily.InterNetwork,
              SocketType.Stream, ProtocolType.Tcp);
          listener.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.DontLinger,1);
          listener.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,1);
  
          //Bind the socket to the local endpoint and listen for
          //incoming connections.
          try
          {
              listener.Bind(localEndPoint);
              listener.Listen(100);
  
              while (listening)
              {
                  //Set the event to nonsignaled state.
                  allDone.Reset();    
  
                  //Start an asychronous socket to listen for connections.
                  Print("Waiting for a connection...");
                  listener.BeginAccept(
                  new AsyncCallback(AcceptCallback),
                      listener);
  
                  //Wait until a connection is made before continuing.
                  allDone.WaitOne();
              }
          }
          catch (Exception e)
          {
              Print(e.ToString());    
          }
  
          listener.Close();
      }
  
      public void AcceptCallback(IAsyncResult arg)
      {
          //Signal the main thread to continue.
          allDone.Set();
  
  
          try
          {
              //Get the socket that handles the client request.
              Socket listener = (Socket) arg.AsyncState;
              Socket handler = listener.EndAccept(arg);
  
  
              //Create the state object.
              StateObject state = new StateObject();
              state.workSocket = handler;
  
              handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                  new AsyncCallback(ReadCallback), state);
          }
          catch (ObjectDisposedException ex)
          {
              Print("Server terminated from another thread.");    
          }
      }
  
      public void ReadCallback(IAsyncResult arg)
      {
          String content = String.Empty;
  
          //Retrieve the state object and the handler socket
          //from the asynchronous state object.
          StateObject state = (StateObject) arg.AsyncState;
          Socket handler = state.workSocket;
  
          //Read data from the client socket.
          int bytesRead = 0;
          try 
          {
              bytesRead = handler.EndReceive(arg);
          }
          catch (ObjectDisposedException ex)
          {
              Print("Process was terminated from another thread.");   
          }
  
          if (bytesRead &gt; 0)
          {
              //There might be more data, so store the data received so far.
              state.sb.Append(Encoding.ASCII.GetString(
                  state.buffer,0,bytesRead));
  
              //Check for end-of-file tag. If it is not there, read
              //more data.
              content = state.sb.ToString();
              if (content.IndexOf("&lt;EOF&gt;") &gt; -1)
              {
                  content = content.Remove(content.Length-6);
                  //All the data has been read from the
                  //client. Display it on the console.
                  Print("Read " + content.Length + " bytes from socket. \n Data : " + content);
                  Respond(handler, content);
              }
              else
              {
                  //Not all data received. Get more.
                  handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                      new AsyncCallback(ReadCallback), state);
              }
          }
      }
  
      private void Send(Socket handler, String data)
      {
          //Convert the string data to byte data using ASCII encoding.
          byte[] byteData = Encoding.ASCII.GetBytes(data);
  
          //Begin sending the data to the remote device.
          handler.BeginSend(byteData,0,byteData.Length,0,
              new AsyncCallback(SendCallback),handler);
      }
  
      private void SendCallback(IAsyncResult arg)
      {
          try
          {
              //Retrieve the socket from the state object.
              Socket handler = (Socket) arg.AsyncState;
  
              //Complete sending the data to the remote device.
              int bytesSent = handler.EndSend(arg);
              Print("Sent " + bytesSent + " bytes to client.");
  
              handler.Shutdown(SocketShutdown.Both);
              //need to make this not linger around
              handler.LingerState = new LingerOption(true,1);
              handler.Close();
          }
          catch (Exception e)
          {
              Print(e.ToString());    
          }
      }
  </code></pre>