public bool StopServer(int nPort) { if (!m_AllServer.ContainsKey(nPort)) { return(false); } ServerInfo curInfo = m_AllServer[nPort]; if (curInfo.nstatue != 1) { return(false); } curInfo.listener.Stop(); curInfo.listener.Server.Close(); if (m_AllConnect.ContainsKey(nPort)) { //停止所有链接 foreach (var connectinfo in m_AllConnect[nPort]) { connectinfo.LocalClient.Client.Close(); StopConnectEvent?.Invoke(connectinfo); } m_AllConnect[nPort].Clear(); } curInfo.nstatue = 0; return(true); }
void RecevieCallBack(IAsyncResult ar) { ConnectInfo connectinfo = ar.AsyncState as ConnectInfo; try { if (connectinfo == null) { return; } int nlength = connectinfo.LocalClient.Client.EndReceive(ar); if (nlength <= 0) { int nPort = (connectinfo.LocalClient.Client.LocalEndPoint as IPEndPoint).Port; StopConnectEvent?.Invoke(connectinfo); if (m_AllConnect.ContainsKey(nPort)) { m_AllConnect[nPort].Remove(connectinfo); } return; } connectinfo.nrecv += (ulong)nlength; connectinfo.server.nrecv += (ulong)nlength; nlength = connectinfo.LocalClient.Client.Send(Encoding.UTF8.GetBytes("test message")); connectinfo.nsent += (ulong)nlength; connectinfo.server.nsent += (ulong)nlength; Thread.Sleep(600); connectinfo.LocalClient.Client.BeginReceive(connectinfo.data, 0, 65535, SocketFlags.None, RecevieCallBack, connectinfo); } catch (Exception ex) { StopConnectEvent?.Invoke(connectinfo); } }