Beispiel #1
0
        /// <summary>
        /// 接收到数据,存入数据
        /// </summary>
        /// <param name="ar"></param>
        public void OnRecievedData(IAsyncResult ar)
        {
            try
            {
                SocketChatClient client = (SocketChatClient)ar.AsyncState;
                byte[]           aryRet = client.GetRecievedData(ar);


                // If no data was recieved then the connection is probably dead
                if (aryRet.Length < 1)
                {
                    Console.WriteLine("客户端 {0}, 断开连接", client.Sock.RemoteEndPoint);
                    client.Sock.Close();
                    m_aryClients.Remove(client);
                    return;
                }
                if (wr.isOk(aryRet))
                {
                    if (wr.JudgeMethod(aryRet, connABB))
                    {
                        client.Sock.Send(wr.ConfirmMessage(aryRet));
                    }
                }
                client.SetupRecieveCallback(this);
            }
            catch (Exception ex)
            {
                Console.WriteLine("OnRecievedData:" + ex.Message);
                WriteError("OnRecievedData:" + ex.Message);
                if (connABB.State != ConnectionState.Open)
                {
                    connABB.Open();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 将给定的连接添加到我们的客户列表中
        /// Note we have a new friend
        /// Send a welcome to the new client
        /// Setup a callback to recieve data
        /// </summary>
        /// <param name="sockClient">Connection to keep</param>
        //public void NewConnection( TcpListener listener )
        public void NewConnection(Socket sockClient)
        {
            // Program blocks on Accept() until a client connects.
            //SocketChatClient client = new SocketChatClient( listener.AcceptSocket() );
            SocketChatClient client = new SocketChatClient(sockClient);

            m_aryClients.Add(client);
            Console.WriteLine("客户端 {0}, 加入连接", client.Sock.RemoteEndPoint);

            // Get current date and time.
            //DateTime now = DateTime.Now;
            //String strDateLine = "Welcome " + now.ToString("G") + "\n\r";

            //// Convert to byte array and send.
            //Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes( strDateLine.ToCharArray() );
            //client.Sock.Send( byteDateLine, byteDateLine.Length, 0 );

            client.SetupRecieveCallback(this);
        }