title: C# UDP An existing connection was forcibly closed by the remote host
body: <p>I am creating a server for a game that handles multiple clients over <code>UDP</code> using the asynchronous methods, and am specifically working on clean disconnect logic. When a client hard crashes (their program is closed without proper disconnect logic) the <code>readCallback</code> on the server throws the SocketException </p>
  
  <blockquote>
    <p>An existing connection was forcibly closed by the remote host</p>
  </blockquote>
  
  <p>which makes sense, however when the read is triggered the next time on the loop in <code>read</code> it crashes despite the exception being handled in the callback.</p>
  
  <pre><code>    private void connectedState()
      {
          while (connected)
          {
              //reset the trigger to non-signaled
              readDone.Reset();
  
              read(socket);
  
              //block on reading data
              readDone.WaitOne();
          }
      }
  
  
      private void read(Socket sock)
      {
          // Creates an IpEndPoint to capture the identity of the sending host.
          IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
          EndPoint senderRemote = sender;
  
          // Create the state object.
          StateObject state = new StateObject();
          state.workSocket = sock;
  
          //crashes after an exception is caught within the callback
          sock.BeginReceiveFrom(state.buffer, 0, StateObject.MESSAGE_SIZE, SocketFlags.None, ref senderRemote, new AsyncCallback(readCallback), state);
      }
  
  
      private void readCallback(IAsyncResult ar)
      {
          StateObject state = (StateObject)ar.AsyncState;
          Socket sock = state.workSocket;
  
          EndPoint senderRemote = new IPEndPoint(IPAddress.Any, 0);
  
          try
          {
              // Read data from the client socket. 
              int bytesRead = sock.EndReceiveFrom(ar, ref senderRemote);
  
              if (bytesRead &lt;= 0)
              {
                  //handle disconnect logic
              }
              else
              {
                  //handle the message received
              }
          }
          catch (SocketException se)
          {
              Console.WriteLine(se.ToString());
          }
  
          // Signal the read thread to continue
          readDone.Set();
      }
  </code></pre>
  
  <p>Two exceptions are thrown, one of which I believe is being caught:</p>
  
  <hr>
  
  <p>Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
  System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
     at System.Net.Sockets.Socket.EndReceiveFrom(IAsyncResult asyncResult, EndPoint&amp; endPoint)
     at CardCatacombs.Utilities.Networking.UDPNetworkConnection.readCallback(IAsyncResult ar) in C:\Users\kayas\Desktop\Practicum\Source\CardCatacombs\CardCatacombs\Utilities\Networking\UDPNetworkConnection.cs:line 424</p>
  
  <p>Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
  System.Net.Sockets.SocketException (0x80004005): An existing connection was forcibly closed by the remote host
     at System.Net.Sockets.Socket.DoBeginReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint endPointSnapshot, SocketAddress socketAddress, OverlappedAsyncResult asyncResult)</p>
  
  <hr>
  
  <p>I would like to be able to cleanly handle a client crash and continue running since there are other clients connected to the server.</p>