protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); if (asyncTcpClient.Connected) { asyncTcpClient.Disconnect(); } }
/// <summary> /// 停止 /// </summary> public async void Stop() { if (Client != null) { await Task.Delay(2 * 1000); Client.Disconnect(); Task.WaitAll(TaskManager.ToArray()); Client.Dispose(); Client = null; GC.Collect(); } }
private async Task MessageRecieved(AsyncTcpClient client, int count) { byte[] bytes = client.ByteBuffer.Dequeue(count); string message = Encoding.UTF8.GetString(bytes, 0, bytes.Length); Console.WriteLine("Server client: received: " + message); bytes = Encoding.UTF8.GetBytes("You said: " + message); await client.Send(new ArraySegment <byte>(bytes, 0, bytes.Length)); if (message == "bye") { // Let the server close the connection client.Disconnect(); } }
private void disconnectTcp() { // is there a tcp connection to the vna? if (asyncTcpClient.connected == true) { // yes... // disconnect from server asyncTcpClient.Disconnect(); // update vna panel ui updateControls(asyncTcpClient.connected); // log the event appendToLogTextBox("Disconnected from TCP/IP Socket"); } }
/// <summary> /// 断开连接 /// </summary> public void OnDisconnect() { if (m_TcpClient == null) { return; } m_TcpClient.OnMessage -= OnTcpMessage; m_TcpClient.Disconnect(); m_TcpClient = null; if (m_UdpClient == null) { return; } m_UdpClient.OnMessage -= OnUdpMessage; m_UdpClient.Disconnect(); m_UdpClient = null; }
public async Task SendDataTask(byte[] bytes) { // 用户要发送的数据 if (!RemoteTcpClient.IsClosing) { if (bytes.Length <= 0) { // Close the client connection RemoteTcpClient.Disconnect(); return; } await RemoteTcpClient.Send(new ArraySegment <byte>(bytes, 0, bytes.Length)); // Wait for server response or closed connection await RemoteTcpClient.ByteBuffer.WaitAsync(); } else { if (MessageEvent != null) { MessageEvent("发送数据失败: 远程主机已经关闭此连接", null); } } }