public void Stop() { if (TcpServerUtils.GetState(this.m_client) == TcpState.Established) { this.m_client.Close(); this.m_client.Dispose(); } }
//method for sending data from joystick public void SetInfo(List <string> tokens) { if ((TcpServerUtils.GetState(this.m_client) != TcpState.Closed) && (TcpServerUtils.GetState(this.m_client) != TcpState.Unknown)) { string command = "set "; command += SimulatorPathsDict[tokens[0]] + " " + tokens[1] + "\r\n"; byte[] byteTime = Encoding.ASCII.GetBytes(command.ToString()); m_networkStream.Write(byteTime, 0, byteTime.Length); } }
public void SendCommand(String command) { if ((TcpServerUtils.GetState(m_client) != TcpState.Closed) && (TcpServerUtils.GetState(m_client) != TcpState.Unknown)) { if (command.Length != 0) { using (NetworkStream stream = new NetworkStream(m_client.Client, false)) using (BinaryWriter writer = new BinaryWriter(stream)) { byte[] data = Encoding.ASCII.GetBytes((command + "\r\n")); Console.WriteLine(command); writer.Write(data); writer.Flush(); } } } }
/// <summary> /// OnTimedEvent(object sender, ElapsedEventArgs e, IPEndPoint ep). /// /// </summary> /// <param name="sender"> sender as object</param> /// <param name="e"> ElapsedEventArgs </param> /// <param name="ep"> IPEndPoint </param> private void OnTimedEvent(object sender, ElapsedEventArgs e, IPEndPoint ep) { if ((TcpServerUtils.GetState(this.m_client) != TcpState.Closed) && (TcpServerUtils.GetState(this.m_client) != TcpState.Unknown)) { Console.WriteLine("Connected usccessful!"); this.m_networkStream = m_client.GetStream(); Timer timer = (Timer)sender; // Get the timer that fired the event timer.Stop(); // Stop the timer that fired the event } else { Console.WriteLine("Connecting..."); try { m_client.Connect(ep); #pragma warning disable CS0168 // The variable 'ex' is declared but never used } catch (System.Net.Sockets.SocketException ex) { }; #pragma warning restore CS0168 // The variable 'ex' is declared but never used } }
private void Predicat(object obj) { TcpClient tcpClient = null; NetworkStream netStream = null; var listener = new TcpListener(IPAddress.Any, m_port); listener.Start(); tcpClient = listener.AcceptTcpClient(); netStream = tcpClient.GetStream(); Console.WriteLine("Flight simulator is connected!"); while (true) { try { if (TcpServerUtils.GetState(tcpClient) == System.Net.NetworkInformation.TcpState.Closed) { Console.WriteLine("Client disconnected gracefully"); break; } Data = ReadStream(netStream); } catch (ObjectDisposedException) { Console.WriteLine("netstream has died"); } } tcpClient.Close(); netStream.Close(); listener.Stop(); tcpClient.Dispose(); netStream.Dispose(); }