/// <summary> /// 接收信息并转换为字符串 /// </summary> /// <returns>返回接收的信息字符串</returns> public string ReceiveInfo(ConnTypes connType) { //TODO 完善其它连接方式的接收方法 string info = string.Empty; try { switch (connType) { case ConnTypes.TCP: info = this.TcpClient.ReceiveInfo(); break; case ConnTypes.UDP: //info = BaseUdpClient.ReceiveInfo(); break; } } catch (Exception) { switch (connType) { case ConnTypes.TCP: LastErrorMessage = this.TcpClient.LastErrorMessage; break; case ConnTypes.UDP: //LastErrorMessage = BaseUdpClient.LastErrorMessage; break; } info = string.Empty; throw; } return(info); }
/// <summary> /// 发送指令 /// </summary> /// <param name="command">指令字符串</param> /// <param name="connType"></param> public void SendCommand(string command, ConnTypes connType) { //TODO 完善其它连接方式的发送方法 try { if (connType == ConnTypes.TCP) { this.TcpClient.SendString(command); } //else if (connType == ConnTypes.UDP) // BaseUdpClient.SendCommand(command); } catch (Exception) { if (connType == ConnTypes.TCP) { LastErrorMessage = this.TcpClient.LastErrorMessage; } //else if (connType == ConnTypes.UDP) // LastErrorMessage = BaseUdpClient.LastErrorMessage; throw; //假如不需要抛出异常,注释掉 } }
/// <summary> /// 通讯连接 /// </summary> /// <param name="serverIp">待连接的主机地址(IP地址)</param> /// <param name="portStr">待连接的端口号</param> /// <param name="connType">连接类型(UDP, TCP)</param> /// <returns>假如连接成功,返回1,否则返回0</returns> public int Connect(string serverIp, string portStr, ConnTypes connType) { string _class = MethodBase.GetCurrentMethod().ReflectedType.FullName; //命名空间+类名 string _method = string.Format("Int32 {0}", new StackTrace().GetFrame(0).GetMethod().Name); //方法名称 string connTypeName = Enum.GetName(typeof(ConnTypes), (int)connType); //连接类型的名称 //假如已连接,返回错误信息 if (IsConnected) { LastErrorCode = "001"; LastErrorMessage = string.Format("已与主机 {0} 在端口 {1} 建立{2}连接,无法再次连接!", ServerIp, ServerPort.ToString(), connTypeName); FileClient.WriteFailureInfo(new string[] { LastErrorMessage, string.Format("类名称:{0},方法名称:{1}", _class, _method) }); return(0); } //假如两个参数中有一个参数为空,则生成错误信息并抛出异常 string param = string.Empty; //参数名称 if (string.IsNullOrWhiteSpace(serverIp)) { param = "string serverIp"; LastErrorCode = "002"; LastErrorMessage = string.Format("建立{0}连接的主机地址不得为空!", connTypeName); } else if (string.IsNullOrWhiteSpace(portStr)) { param = "string portStr"; LastErrorCode = "003"; LastErrorMessage = string.Format("建立{0}连接的端口不得为空!", connTypeName); } if (!string.IsNullOrWhiteSpace(LastErrorCode)) { FileClient.WriteFailureInfo(new string[] { LastErrorMessage, string.Format("类名称:{0},方法名称:{1}", _class, _method) }); throw new ArgumentException(LastErrorMessage, param); //假如不需要抛出异常,注释此行 //return 0; } serverIp = serverIp.Equals("localhost", StringComparison.OrdinalIgnoreCase) ? "127.0.0.1" : serverIp; //判断localhost int port = int.Parse(portStr); //端口转换为数值类型 int result = 1; //判断连接类型并进行相应的连接,保存主机地址、端口与连接状态;假如报错,获取错误信息 if (connType == ConnTypes.TCP) { try { this.TcpClient = new DerivedTcpClient(serverIp, port); this.ServerIp = this.TcpClient.ServerIp; this.ServerPort = this.TcpClient.ServerPort; this.IsConnected = this.TcpClient.IsConnected; } catch (Exception) { this.LastErrorMessage = this.TcpClient.LastErrorMessage; throw; } } //TODO 编写其它连接方式的连接方法 else { //try //{ // result = BaseUdpClient.Connect(serverIp, port); // ServerIp = BaseUdpClient.ServerIp; // ServerPort = BaseUdpClient.ServerPort; // IsConnected = BaseUdpClient.IsConnected; //} //catch (Exception) { LastErrorMessage = BaseUdpClient.LastErrorMessage; throw; } } ConnType = connType; return(result); }
public void utypes(ConnTypes types) { return; }