/// <summary> /// JYTCPClient类构造函数 /// </summary> /// <param name="ipAddress">连接的远端ip位置</param> /// <param name="port">连接的远端端口号</param> /// <param name="dataType">TCP缓存存放的资料类型</param> /// <param name="bufferSize">TCP缓存的大小</param> public JYTCPClient(string ipAddress, int port, ChannelDataType dataType = ChannelDataType.DataStream, int bufferSize = 131072) { _ipAddress = ipAddress; _port = port; _dataType = dataType; _client = new JYAsyncTcpClient(_ipAddress, _port); if (_client == null) { throw new Exception("客户端建立失败!"); } _bufferSize = bufferSize;//默认100KB switch (_dataType) { case ChannelDataType.DataStream: _buffer = new CircularBuffer <byte>((int)_bufferSize); _client.DatagramReceived += _client_DatagramReceived; break; case ChannelDataType.String: _buffer = new StringBuffer((int)_bufferSize); _client.PlaintextReceived += _client_PlaintextReceived; break; default: break; } _client.ServerDisconnected += _client_ServerDisconnected; _client.Retries = 10; _client.RetryInterval = 1; }
/// <summary> /// 服务器构造函数 /// </summary> /// <param name="listenPort">监听的端口号</param> /// <param name="dataType">缓存区存放资料的类型</param> /// <param name="bufferSize">缓存区大小</param> public JYTCPServer(int listenPort, ChannelDataType dataType = ChannelDataType.DataStream, int bufferSize = 131072) { LocalIP = Dns.GetHostAddresses(Dns.GetHostName()).Where(x => x.AddressFamily == AddressFamily.InterNetwork).First(); _server = new JYAsyncTcpServer(LocalIP, listenPort); _server.ReceiveBufferSize = (uint)bufferSize; _clientsInfo = new List <ClientInformation>(); _dataType = dataType; if (_server == null) { throw new Exception("监听端口创建失败!"); } }