Esempio n. 1
0
        /// <summary>
        /// 处理连接请求
        /// </summary>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            semaphore.WaitOne();
            ClientPeer client = clientPeerPool.Dequeue();

            client.clientSocket = e.AcceptSocket;
            Console.WriteLine(client.clientSocket.RemoteEndPoint + "客户端连接成功");
            //接收消息TODO
            StartReceive(client);

            e.AcceptSocket = null;
            StartAccept(e);
        }
Esempio n. 2
0
        /// <summary>
        /// 处理连接请求
        /// </summary>
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            // 阻止当前线程,直到当前 WaitHandle 收到信号
            semaphore.WaitOne();
            // 获取连接的客户端套接字
            // ClientPeer client = new ClientPeer(); // 每连接一个就 new 一个会损耗性能,所以用客户端对象连接池来解决
            ClientPeer client = clientPeerPool.Dequeue();

            client.ClientSocket = e.AcceptSocket;
            Console.WriteLine(client.ClientSocket.RemoteEndPoint + "客户端连接成功");
            // 接收消息
            StartRecive(client);

            e.AcceptSocket = null;
            StartAccept(e); // 循环接收客户端的连接
        }