Asynchronous sockets in C #

I am confused about the correct way to use asynchronous socket methods in C #. I will refer to these two articles to explain things and ask my questions: MSDN article on asynchronous client sockets and devarticles.com article on socket programming .

My question is about the method BeginReceive(). An MSDN article uses these two functions to process received data:

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 > 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 > 1) 
       {
         response = state.sb.ToString();
       }
       // Signal that all bytes have been received.
       receiveDone.Set();
     }
   } 
   catch (Exception e) 
   {
     Console.WriteLine(e.ToString());
   }
 }

While the devarticles.com tutorial passes nullfor the last parameter of the method BeginReceiveand continues to explain that the last parameter is useful when dealing with multiple sockets. Now my questions are:

  • BeginReceive, ? ? , , , , - .

  • ? client.BeginReceive(...), client? devarticles.com : m_asynResult = m_socClient.BeginReceive (theSocPkt.dataBuffer,0,theSocPkt.dataBuffer.Length, SocketFlags.None,pfnCallBack,theSocPkt);

theSocPkt.thisSocket, m_socClient. , , ?

, , , . , BeginReceive , ?

+3
2

BeginReceive, ? ? , , , , - .

, , , . , . , .

. , - ? ... . , . .

. , , , , , , , - .

?

, . , , -:

//Socket client = this.client; // Don't do this.
Socket client = state.workSocket; 

, MSDN Client . - , .


. ,.NET , , ArgumentException. EndReceive .NET Reflector :

if ((result == null) || (result.AsyncObject != this))
{
    throw new ArgumentException(SR.GetString("net_io_invalidasyncresult"), "asyncResult");
}
+2

, ?

, - , ; :

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
    ReceiveCallback, state);

, , , , "" , ReceiveCallback, . , , .

state , ( ), .

" " , , , , ... , . , , , , " " , . ;)

+1

Source: https://habr.com/ru/post/1738761/


All Articles