public void CloseSocket(ChatSocket sc) { try { if (clients.Count == 0) { return; } for (int i = 0; i < clients.Count; i++) { SocketStatus s = clients[i]; if (s.scocKet.Connected) { if (s.Ip == sc.RemoteEndPoint.Address.ToString() && s.Port == sc.RemoteEndPoint.Port.ToString()) { clients.Remove(s); } } } } catch (Exception ex) { Log.WriteLog("wms连接不稳定"); } // FormMain.globalFrm.gdvWmsConn.RefreshData(); // sc.Close(); }
/// <summary> /// 2)监听客户端连接 /// </summary> public void ListenClientConnect(IAsyncResult aResult) { try { lock (clients) { SocketStatus classSocket = new SocketStatus(); if (serverSocket == null) { return; } Socket lientSocket = serverSocket.EndAccept(aResult); if (serverSocket == null) { return; } /// 数据格式 string ipClient = ((IPEndPoint)lientSocket.RemoteEndPoint).Address.ToString(); string protClient = ((IPEndPoint)lientSocket.RemoteEndPoint).Port.ToString(); Log.WriteLog("客户端连接成功!" + ipClient + ": " + protClient); //FrmMain.globalFrm.tb_message.AppendText("客户端连接成功!" + ipClient + ": " + protClient + "\r\n"); ClientManager cm = new ClientManager(ref lientSocket); lientSocket.Poll(100, SelectMode.SelectError); classSocket.heartbeatnumber = 0; classSocket.scocKet = lientSocket; classSocket.statusSocket = "0"; if (clients.Count > 0) { if (clients[0].scocKet.Connected) { clients[0].scocKet.Close(); } } clients.Add(classSocket); cm.CommandReceived += OnClientManagerCommandReceived; serverSocket.BeginAccept(new AsyncCallback(ListenClientConnect), null); } } catch (Exception exc) { if (!SockUtils.HandleSocketError(exc)) { Log.WriteLog(exc.InnerException + exc.Source + exc.Message + exc.TargetSite); } return; } }
public bool SendWmsData(byte[] data) { lock (data) { try { for (int i = 0; i < WmsCommon.Instance().clients.Count;) { if (i < WmsCommon.Instance().clients.Count) { SocketStatus wmssocket = WmsCommon.Instance().clients[i]; if (wmssocket != null) { if (wmssocket.scocKet.Connected) { wmssocket.scocKet.Send(data); // Log.WriteLog(DateTime.Now + "WCS --> WMS:" + data); i++; } else { wmssocket.scocKet.Close(); WmsCommon.Instance().clients.Remove(wmssocket); } } } } return(true); } catch (Exception ex) { Log.WriteLog("发送消息异常" + ex.Message.ToString()); return(false); } // return false; } }