void server_OnClientDisconnected(object sender, SocketLib.TcpClientDisconnectedEventArgs e) { try { foreach (string k in schoolNums.Keys) { if (schoolNums[k] == e.Uid) { lock (schoolNums) { schoolNums.Remove(k); SeatManage.SeatManageComm.WriteLog.Write(schoolLinkLog, string.Format("学校{0}已断开连接,Ip地址:{0}", k, e.Uid)); if (OnSchoolDisConnectionHandler != null) { OnSchoolDisConnectionHandler(k, e.Uid); } } break; } } SocketLib.SocketListener s = sender as SocketLib.SocketListener; Console.WriteLine("{0:M} {1:t}:客户端断开连接,当前连接数{2}", DateTime.Now, DateTime.Now, s.NumConnections); } catch (Exception ex) { SeatManage.SeatManageComm.WriteLog.Write(string.Format("断开连接处理遇到异常:{0}", ex.Message)); } }
public void Start() { try { server = new SocketLib.SocketListener(20000, 16384);//最大并发量35000,缓冲区16kb server.GetKeepAliveLink = GetSchoolInfo; server.OnClientConnected += new EventHandler <SocketLib.TcpClientConnectedEventArgs>(server_OnClientConnected); server.OnClientDisconnected += new EventHandler <SocketLib.TcpClientDisconnectedEventArgs>(server_OnClientDisconnected); server.OnMsgReceived += new SocketLib.SocketListener.ReceiveMsgHandler(server_OnMsgReceived); //server.OnSended += new SocketLib.SocketListener.SendCompletedHandler(server_OnSended); server.Init(); } catch (Exception ex) { SeatManage.SeatManageComm.WriteLog.Write(string.Format("socket服务初始化失败:{0},异常来自:{1}", ex.Message, ex.Source)); } server.Start(10010); }
public void Stop() { server.Stop(); schoolNums.Clear(); server = null; }
/// <summary> /// 客户端连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void server_OnClientConnected(object sender, SocketLib.TcpClientConnectedEventArgs e) { SocketLib.SocketListener s = sender as SocketLib.SocketListener; Console.WriteLine("{0:M} {1:t}:有新的连接,当前连接数{2},终结点:{3}", DateTime.Now, DateTime.Now, s.NumConnections, e.Uid); }