static void Main(string[] args) { try { cancellationSource = new CancellationTokenSource(); RemoteServerInfo rsi = new RemoteServerInfo { Host = IPAddress.Parse("127.0.0.1"), Port = 900 }; AsyncTcpClient client = new AsyncTcpClient(); //client.OnDataReceived += HandleRecieved; client.OnDataReceived = onData; client.OnDisconnected = onDisconnect; client.OnException = OnException; client.OnException += OnExceptionStack; //client.OnDisconnected += HandleDisconnected; client.ConnectAsync(rsi, cancellationSource.Token).ContinueWith(t => client .Recieve(cancellationSource.Token), TaskContinuationOptions.OnlyOnRanToCompletion).Wait(); Console.WriteLine("here we are"); } catch (System.Exception ex) { Console.Write(ex.Message); } Console.Read(); }
public async void ConnectDisconnect() { AsyncTcpClient client = new AsyncTcpClient(); await client.ConnectAsync(localhost, port); Assert.True(client.IsConnected); await client.CloseAsync(); }
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); }
public async void ServerDisconnect() { AsyncTcpClient client = new AsyncTcpClient(); client.OnDisconnected += new EventHandler(OnDisconnected); await client.ConnectAsync(localhost, port); server.Dispose(); Thread.Sleep(1000); String hello = "hello"; byte[] helloBytes = System.Text.Encoding.Unicode.GetBytes(hello); // we have to write twice to see the socket disconnect await client.SendAsync(helloBytes); await client.SendAsync(helloBytes); Assert.True(disconnected); }
/// <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); } } }