Exemplo n.º 1
0
        private void AcceptTcpClientCallBack(IAsyncResult iar)
        {
            try
            {
                this._AllDone.Set();
                TcpListener mylistener = (TcpListener)iar.AsyncState;
                TcpClient   client     = mylistener.EndAcceptTcpClient(iar);

                Console.WriteLine("connecting:" + client.Client.RemoteEndPoint);

                DataReadWrite datareadwrite = new DataReadWrite(client);
                clientlist.Add(datareadwrite);

                SendString(datareadwrite, "CONNECTING");
                datareadwrite.ns.BeginRead(datareadwrite.read, 0, datareadwrite.read.Length, ReadCallBack, datareadwrite);
                if (AnalyeConnectedEvent != null)
                {
                    AnalyeConnectedEvent(null);
                }
                else
                {
                    Console.WriteLine("AnalyeConnectedEvent: is null!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("网络服务器:" + e.Message);
                return;
            }
        }
Exemplo n.º 2
0
        private void SendCallBack(IAsyncResult iar)
        {
            DataReadWrite datareadwrite = (DataReadWrite)iar.AsyncState;

            try
            {
                datareadwrite.ns.EndWrite(iar);
            }
            catch (Exception e)
            {
                Console.WriteLine("[NET SERVER]:" + e.Message);
            }
        }
Exemplo n.º 3
0
 //发送
 private void SendString(DataReadWrite datareadwrite, string str)
 {
     try
     {
         datareadwrite.write = Encoding.UTF8.GetBytes(str + "\r\n");
         datareadwrite.ns.BeginWrite(datareadwrite.write, 0, datareadwrite.write.Length, new AsyncCallback(SendCallBack), datareadwrite);
         datareadwrite.ns.Flush();
     }
     catch (Exception e)
     {
         Console.WriteLine("[NET SERVER]:" + e.Message);
     }
 }
Exemplo n.º 4
0
        public void CloseService()
        {
            for (int i = 0; i < clientlist.Count; i++)
            {
                DataReadWrite e = clientlist[i] as DataReadWrite;

                SendString(e, "I am closing");

                clientlist.RemoveAt(i);
            }

            _isExit = true;

            _AllDone.Set();
        }
Exemplo n.º 5
0
        private void ReadCallBack(IAsyncResult iar)
        {
            DataReadWrite datareadwrite = null;

            try
            {
                datareadwrite = (DataReadWrite)iar.AsyncState;
                int recv = datareadwrite.ns.EndRead(iar);

                DataArriveEvent(Encoding.UTF8.GetString(datareadwrite.read, 0, recv));


                if (this._isExit == false)
                {
                    datareadwrite.InitReadArray();
                    datareadwrite.ns.BeginRead(datareadwrite.read, 0, datareadwrite.read.Length, ReadCallBack, datareadwrite);
                }
            }
            catch (Exception e)
            {
                clientlist.Remove(datareadwrite);
                Console.WriteLine("[NET SERVER]:" + e.Message + "disconnecting:" + datareadwrite.client.Client.RemoteEndPoint);
            }
        }