Exemple #1
0
        /// <summary>
        /// 客户端信息接收回调函数
        /// </summary>
        /// <param name="iar"></param>
        /// <date>2017-07-17</date>
        /// <author>冯旗</author>
        private void ReadCallBack(IAsyncResult iar)
        {
            DataReadWrite dataReadWrite = null;

            try
            {
                dataReadWrite = (DataReadWrite)iar.AsyncState;
                // 结束读取
                int recv = dataReadWrite.ns.EndRead(iar);

                OnDataArrive(Encoding.UTF8.GetString(dataReadWrite.read, 0, recv));
                //OnDataArrive(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);
            }
        }
Exemple #2
0
        /// <summary>
        /// 接收客户端连接回调函数
        /// </summary>
        /// <param name="iar"></param>
        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;
            }
        }
Exemple #3
0
        /// <summary>
        /// 关闭服务器
        /// </summary>
        /// <date>2017-07-17</date>
        /// <author>冯旗</author>
        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();
        }
Exemple #4
0
        /// <summary>
        /// 数据发送成功回调函数
        /// </summary>
        /// <param name="iar"></param>
        /// <date>2017-07-17</date>
        /// <author>冯旗</author>
        private void SendCallBack(IAsyncResult iar)
        {
            DataReadWrite dataReadWrite = (DataReadWrite)iar.AsyncState;

            try
            {
                dataReadWrite.ns.EndWrite(iar);
            }
            catch (Exception e)
            {
                Console.WriteLine("[NET SERVER]: " + e.Message);
            }
        }
Exemple #5
0
 /// <summary>
 /// 发送消息向客户端
 /// </summary>
 /// <param name="dataReadWrite">发送信息对象</param>
 /// <param name="str">待发消息</param>
 /// <date>2017-07-17</date>
 /// <author>冯旗</author>
 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);
     }
 }
Exemple #6
0
        public void SendBytes(string strSendACK)
        {
            for (int i = 0; i < clientList.Count; i++)
            {
                DataReadWrite e = clientList[i] as DataReadWrite;

                try
                {
                    e.write = System.Text.Encoding.UTF8.GetBytes(strSendACK);
                    e.ns.BeginWrite(e.write, 0, e.write.Length, new AsyncCallback(SendCallBack), e);
                    e.ns.Flush();
                }
                catch (Exception ee)
                {
                    Console.WriteLine("[NET SERVER]: " + ee.Message);
                }
            }
        }