/// <summary> /// 从接收缓冲区读取收到数据 /// </summary> /// <param name="buffer">存储接收数据的字节数组</param> /// <param name="offset">接收字节存储在数组中的偏移量</param> /// <param name="count">接收字节数</param> /// <returns></returns> public int Read(byte[] buffer, int offset, int count) { byte[] readBuf = new byte[count]; // 同步接收 int bytes = tcpClient.Receive(readBuf, count); Array.Copy(readBuf, 0, buffer, offset, count); return(bytes); }
public async void Send() { AsyncTcpClient client = new AsyncTcpClient(); client.OnDataReceived += new EventHandler <byte[]> (TestDataReceived); await client.ConnectAsync(localhost, port); Task receiveTask = client.Receive(); String hello = "hello"; byte[] helloBytes = System.Text.Encoding.Unicode.GetBytes(hello); await client.SendAsync(helloBytes); Thread.Sleep(1000); Assert.Equal("hello", receivedMessage); }
/// <summary> /// Connect this instance. /// </summary> /// <returns>The connect.</returns> internal async Task Connect() { try { Console.WriteLine("Command - Connect"); _client = new AsyncTcpClient(); _client.OnDataReceived += DataReceived; _client.OnDisconnected += Disconnected; await _client.ConnectAsync(Constants.Address, Constants.CommandPort); Task receiveTask = _client.Receive(); FirstChevron = false; } catch (TaskCanceledException ex) { if (!cts.Token.IsCancellationRequested) { Console.WriteLine("Command Task Cancel " + ex); } } }
/// <summary> /// Connect this instance. /// </summary> /// <returns>The connect.</returns> internal async Task Connect() { try { Console.WriteLine("Data - Connect"); _client = new AsyncTcpClient(); _client.OnDataReceived += DataReceived; _client.OnDisconnected += Disconnected; status = new PortStatus(); status.IsConnected = true; await _client.ConnectAsync(Constants.Address, Constants.DataPort); Task receiveTask = _client.Receive(); } catch (TaskCanceledException ex) { if (!cts.Token.IsCancellationRequested) { Console.WriteLine("Data Task Cancel " + ex); } } }