public void Run() { try { ServiceLog.Info(string.Format("正在建立 Modbus TCP 数据采集连接[{0} {1}:{2}]", ServerID, server_ip, server_port)); clientSocket = ConnectToServer(); ServiceLog.Info(string.Format("建立 Modbus TCP 数据采集连接[{0} {1}:{2}]成功...", ServerID, server_ip, server_port)); HostContainer.AddModbusTcpClientHost(this); } catch (Exception ex) { ServiceLog.Error(string.Format("建立 Modbus TCP 数据采集连接[{0} {1}:{2}]发生错误:{3}", ServerID, server_ip, server_port, ex.Message)); return; } }
private Socket AcceptConnection() { if (clientSocket == null || !clientSocket.Connected) { try { clientSocket = serverSocket.Accept(); ServiceLog.Info(string.Format("数据采集服务[{0} {1}:{2}]接收到连接[{3}]", ServerID, server_ip, server_port, clientSocket.RemoteEndPoint)); HostContainer.AddTcpServerHost(this); clientSocketEndPointInfo = clientSocket.RemoteEndPoint.ToString(); return(clientSocket); } catch (Exception ex) { clientSocket = null; ServiceLog.Error(string.Format("数据采集服务[{0} {1}:{2}]接收连接发生错误:{3}", ServerID, server_ip, server_port, ex.Message)); return(null); } } else { return(clientSocket); } }