Ejemplo n.º 1
0
        private void AcceptCb(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = (TcpListener)ar.AsyncState;
                TcpClient   client   = (TcpClient)listener.EndAcceptTcpClient(ar);
                int         index    = NewIndex();

                if (index < 0)
                {
                    client.Close();
                    Console.WriteLine("[警告]链接已满");
                }
                else
                {
                    SocketBase socketBase = clients[index];
                    socketBase.Init(client);
                    //string adr = socketBase.GetAddress();
                    //Console.WriteLine("客户端连接 [" + adr + "] conn池ID:" + index);
                    //MessageData messageData = new MessageData();
                    //string msg = " [" + plazaSession.GetAddress() + "] 加入聊天房间,当前房间人数[" + GetOnlineCount() + "]";
                    //plazaSession.client.GetStream().BeginRead(plazaSession.byteBuffer, 0, plazaSession.GetMaxRead(), new AsyncCallback(ReceiveCb), plazaSession);
                    //plazaSession.client
                    //AddMessage(conn, ref messageData, 0, 3, msg);
                    //Broadcast(conn, messageData,true);
                    //conn.socket.BeginReceive(conn.readBuffer,
                    //                         conn.bufferCount, conn.Buffremain(),
                    //                         SocketFlags.None, ReceiveCb, conn);
                }
                tcpListener.BeginAcceptTcpClient(AcceptCb, tcpListener);
            }
            catch (Exception e)
            {
                Console.WriteLine("AcceptCb失败:" + e.Message);
            }
        }