Example #1
0
 //开始接受数据》.
 private void Receive()
 {
     try
     {
         StateObjectForRecvData state = new StateObjectForRecvData();
         state.workSocket = m_ClientSocket;
         m_ar_Recv        = m_ClientSocket.BeginReceive(state.buffer, 0, StateObjectForRecvData.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
     }
     catch (Exception e)
     {
         DidDisconnect(e);
     }
 }
Example #2
0
 //接受数据回调.
 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         ar.AsyncWaitHandle.Close();
         m_ar_Recv = null;
         StateObjectForRecvData state = (StateObjectForRecvData)ar.AsyncState;
         Socket client    = state.workSocket;
         int    bytesRead = client.EndReceive(ar);
         if (bytesRead > 0)
         {
             lock (m_ComunicationMem)
             {
                 m_ComunicationMem.Write(state.buffer, 0, bytesRead);
             }
         }
         m_ar_Recv = client.BeginReceive(state.buffer, 0, StateObjectForRecvData.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
     }
     catch (Exception e)
     {
         DidDisconnect(e);
     }
 }