Ejemplo n.º 1
0
 private void Client_DisplayMsg(TCPServerClient client, string msg, DateTime time)
 {
     if (ShowListMsg != null)
     {
         ShowListMsg(client, msg, time);
     }
 }
Ejemplo n.º 2
0
 private void Client_DataReceived(TCPServerClient client, int head, byte[] buffer, string strdata = null)
 {
     if (DataReceived != null)
     {
         DataReceived(client, head, buffer, strdata);
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 客户端移除事件
 /// </summary>
 /// <param name="client"></param>
 private void Client_ClientClosed(TCPServerClient client)
 {
     try
     {
         Clients.Remove(client);
         client.Dispose();
     }
     catch (Exception)
     {
     }
 }
Ejemplo n.º 4
0
        private void AcceptCallBack(IAsyncResult ar)
        {
            Socket sokserver = ar.AsyncState as Socket;

            try
            {
                Socket sokclient = sokserver.EndAccept(ar);
                string key       = sokclient.RemoteEndPoint.ToString();
                try
                {
                    Clients.First(x => x.ID == key);
                    sokclient.Shutdown(SocketShutdown.Both);
                    sokclient.Disconnect(true);
                    sokclient.Close();
                    sokclient.Dispose();
                    sokclient = null;
                    if (ShowListMsg != null)
                    {
                        ShowListMsg(null, key + "重复请求已阻止", DateTime.Now);
                    }
                }
                catch (Exception)
                {
                    TCPServerClient client = new TCPServerClient(sokclient, TrustClientValidate);
                    client.ShowAllData   = ShowAllData;
                    client.ClientClosed += Client_ClientClosed;
                    client.DataReceived += Client_DataReceived;
                    client.DisplayMsg   += Client_DisplayMsg;
                    Clients.Add(client);
                    if (ShowListMsg != null)
                    {
                        ShowListMsg(client, "建立连接", DateTime.Now);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ObjectDisposedException)
                { //通常是关闭Socket造成的
                    return;
                }
                else
                {
                    if (ShowListMsg != null)
                    {
                        ShowListMsg(null, "处理客户端连接请求失败", DateTime.Now);
                    }
                    //LogHelper.WriteLog("TCP服务器接受客户端连接请求失败", ex);
                }
            }
            try
            {
                sokserver.BeginAccept(new AsyncCallback(AcceptCallBack), sokserver);
            }
            catch (Exception ex)
            {
                if (ShowListMsg != null)
                {
                    ShowListMsg(null, "服务器启动侦听失败", DateTime.Now);
                }
                //LogHelper.WriteLog("TCP服务器启动侦听失败", ex);
            }
        }