Ejemplo n.º 1
0
 /// <summary>
 /// 处理发送
 /// </summary>
 /// <param name="socketAsyncEventArgs"></param>
 private void ProcessSend(SocketAsyncEventArgs socketAsyncEventArgs)
 {
     //指的是发送之后的处理,发送已经完成
     if (socketAsyncEventArgs.SocketError == SocketError.Success)
     {
         //发送成功
         // done echoing data back to the client
         AsyncUserToken token = (AsyncUserToken)socketAsyncEventArgs.UserToken;
         // read the next block of data send from the client
         bool willRaiseEvent = token.Socket.ReceiveAsync(socketAsyncEventArgs);
         if (!willRaiseEvent)
         {
             ProcessReceive(socketAsyncEventArgs);
         }
     }
     else
     {
         CloseClientSocket(socketAsyncEventArgs);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 关闭客户端socket
        /// </summary>
        /// <param name="eventArgs"></param>
        private void CloseClientSocket(SocketAsyncEventArgs eventArgs)
        {
            AsyncUserToken token = eventArgs.UserToken as AsyncUserToken;

            //Close the socket
            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            catch (Exception)
            {
            }

            token.Socket.Close();

            Interlocked.Decrement(ref m_numConnectedSockets);
            m_maxNumberAcceptedClients.Release();
            //Free the SocketAsyncEventArg so they can be reused
            m_readWritePool.Push(eventArgs);
        }