예제 #1
0
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = e.UserToken as AsyncUserToken;

            // close the socket associated with the client
            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception) { }
            token.Socket.Close();

            // decrement the counter keeping track of the total number of clients connected to the server
            Interlocked.Decrement(ref m_numConnectedSockets);

            m_socketManager.Clean(e);
            // Free the SocketAsyncEventArg so they can be reused by another client
            m_readWritePool.Push(e);

            m_maxNumberAcceptedClients.Release();
            Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", m_numConnectedSockets);
        }
예제 #2
0
 // This method is invoked when an asynchronous send operation completes.
 // The method issues another receive on the socket to read any additional
 // data sent from the client
 //
 // <param name="e"></param>
 private void ProcessSend(SocketAsyncEventArgs e)
 {
     if (e.SocketError == SocketError.Success)
     {
         /**如果数据过大,需要多次发送,这里判断还有没有需要发送的数据
          */
         // done echoing data back to the client
         AsyncUserToken token = (AsyncUserToken)e.UserToken;
         if (m_socketManager.Write(e))
         {
             Send(e);
         }
         else
         {
             //设置buffer
             // read the next block of data send from the client
             Receive(e);
         }
     }
     else
     {
         CloseClientSocket(e);
     }
 }