title: C# UDP Server Asynchronous Multiple Clients | SocketException When Client Disconnect
body: <p>I've been working on a socket server program in C# (I was inspired from this <a href="http://acrocontext.wordpress.com/2013/08/15/c-simple-udp-listener-in-asynchronous-way/" rel="nofollow">post</a>) and my problem is that when a client disconnects an exception "<strong>An existing connection was forcibly closed by the remote host</strong>" appears when the call <strong>EndReceiveFrom()</strong> and returns <strong>0</strong>, the <strong>ref clientEP</strong> becomes the client normally close. I don't understand why my <strong>DoReceiveFrom()</strong> function is called if there is nothing to read. I probably missed something. What is wrong ?</p>
  
  <p>Problem appear there :</p>
  
  <pre><code>int dataLen = this.serverSocket.EndReceiveFrom(iar, ref clientEP);
  </code></pre>
  
  <p>The full source code:</p>
  
  <pre><code>class UDPServer
  {
      private Socket serverSocket = null;
      private List&lt;EndPoint&gt; clientList = new List&lt;EndPoint&gt;();
      private List&lt;Tuple&lt;EndPoint, byte[]&gt;&gt; dataList = new List&lt;Tuple&lt;EndPoint, byte[]&gt;&gt;();
      private byte[] byteData = new byte[1024];
      private int port = 4242;
  
      public List&lt;Tuple&lt;EndPoint, byte[]&gt;&gt; DataList
      {
          private set { this.dataList = value; }
          get { return (this.dataList); }
      }
  
      public UDPServer(int port)
      {
          this.port = port;
      }
  
      public void Start()
      {
          this.serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          this.serverSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
          this.serverSocket.Bind(new IPEndPoint(IPAddress.Any, this.port));
          EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
          this.serverSocket.BeginReceiveFrom(this.byteData, 0, this.byteData.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, newClientEP);
      }
  
      private void DoReceiveFrom(IAsyncResult iar)
      {
          try
          {
              EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
              int dataLen = this.serverSocket.EndReceiveFrom(iar, ref clientEP);
              byte[] data = new byte[dataLen];
              Array.Copy(this.byteData, data, dataLen);
  
              if (!this.clientList.Any(client =&gt; client.Equals(clientEP)))
                  this.clientList.Add(clientEP);
  
              EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
              this.serverSocket.BeginReceiveFrom(this.byteData, 0, this.byteData.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, newClientEP);
  
              DataList.Add(Tuple.Create(clientEP, data));
          }
          catch (ObjectDisposedException)
          {
          }
      }
  
      public void SendTo(byte[] data, EndPoint clientEP)
      {
          try
          {
              this.serverSocket.SendTo(data, clientEP);
          }
          catch (System.Net.Sockets.SocketException)
          {
              this.clientList.Remove(clientEP);
          }
      }
  
      public void SendToAll(byte[] data)
      {
          foreach (var client in this.clientList)
          {
              this.SendTo(data, client);
          }
      }
  
      public void Stop()
      {
          this.serverSocket.Close();
          this.serverSocket = null;
  
          this.dataList.Clear();
          this.clientList.Clear();
      }
  }
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>An existing connection was forcibly closed by the remote host
  </code></pre>
  
  <hr>
  
  <p><strong>Update:</strong>
  I tried to run my client (netcat) on another pc and the exception no longer appears, even when <strong>SendTo()</strong>, which is also problematic to delete my client in my <strong>clientList</strong>.
  I still do not understand what is happening.</p>