title: Asynchronous sockets in C#
body: <p>I'm confused about the correct way of using asynchronous socket methods in C#. I will refer to these two articles to explain things and ask my questions: <a href="http://msdn.microsoft.com/en-us/library/bbx2eya8.aspx" rel="nofollow noreferrer">MSDN article on asynchronous client sockets</a> and <a href="http://www.devarticles.com/c/a/C-Sharp/Socket-Programming-in-C-sharp-Part-II/1/" rel="nofollow noreferrer">devarticles.com article on socket programming</a>.</p>
  
  <p>My question is about the <code>BeginReceive()</code> method. The MSDN article uses these two functions to handle receiving data:</p>
  
  <pre><code>private static void Receive(Socket client) 
  {
    try 
    {
      // Create the state object.
      StateObject state = new StateObject();
      state.workSocket = client;
  
      // Begin receiving the data from the remote device.
      client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
          new AsyncCallback(ReceiveCallback), state);
    } 
    catch (Exception e) 
    {
      Console.WriteLine(e.ToString());
    }
  }
  
  private static void ReceiveCallback( IAsyncResult ar ) 
  {
    try 
    {
       // Retrieve the state object and the client socket 
       // from the asynchronous state object.
       StateObject state = (StateObject) ar.AsyncState;
       Socket client = state.workSocket;
       // Read data from the remote device.
       int bytesRead = client.EndReceive(ar);
       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));
           //  Get the rest of the data.
          client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
              new AsyncCallback(ReceiveCallback), state);
       } 
       else 
       {
          // All the data has arrived; put it in response.
         if (state.sb.Length &gt; 1) 
         {
           response = state.sb.ToString();
         }
         // Signal that all bytes have been received.
         receiveDone.Set();
       }
     } 
     catch (Exception e) 
     {
       Console.WriteLine(e.ToString());
     }
   }
  </code></pre>
  
  <p>While the devarticles.com tutorial passes <code>null</code> for the last parameter of the <code>BeginReceive</code> method, and goes on to explain that the last parameter is useful when we're dealing with multiple sockets. Now my questions are:</p>
  
  <ol>
  <li><p>What is the point of passing a state to the <code>BeginReceive</code> method if we're only working with a single socket? Is it to avoid using a class field? It seems like there's little point in doing it, but maybe I'm missing something.</p></li>
  <li><p>How can the state parameter help when dealing with multiple sockets? If I'm calling <code>client.BeginReceive(...)</code>, won't all the data be read from the <code>client</code> socket? The devarticles.com tutorial makes it sound like in this call: 
  <code>m_asynResult = m_socClient.BeginReceive (theSocPkt.dataBuffer,0,theSocPkt.dataBuffer.Length, SocketFlags.None,pfnCallBack,theSocPkt);</code></p></li>
  </ol>
  
  <p>Data will be read from the <code>theSocPkt.thisSocket</code> socket, instead of from the <code>m_socClient</code> socket. In their example the two are one and the same, but what happens if that is not the case?</p>
  
  <p>I just don't really see where that last argument is useful or at least how it helps with multiple sockets. If I have multiple sockets, I still need to call <code>BeginReceive</code> on each of them, right?</p>