public TcpListeningWindow(ConfigureListener.Config configuration, EditorFrame editorFrame) { InitializeComponent(); Closed += OnClosed; _editor = editorFrame; _configuration = configuration; _edtResult.Options.ShowBoxForControlCharacters = true; _edtResult.Options.ShowEndOfLine = true; _edtResult.Options.ShowSpaces = true; _edtResult.Options.ShowTabs = true; }
private void threadMethod(object threadStart) { ConfigureListener.Config config = (ConfigureListener.Config)threadStart; try { lock (_listenerLock) { _listener = new System.Net.Sockets.TcpListener(config.Nic, config.Port); _listener.Start(); } } catch (Exception se) { setStatus("Error: " + se.Message); stopListener(); } byte[] bytes = new byte[256]; while (true) { try { if (_listener == null) { return; } if (!_listener.Pending()) { setStatus("Waiting for a connection... on " + _listener.LocalEndpoint); Thread.Sleep(500); continue; } using (_client = _listener.AcceptTcpClient()) using (NetworkStream stream = _client.GetStream()) { setStatus(string.Format("Connected, {1} -> {0}", _client.Client.LocalEndPoint, _client.Client.RemoteEndPoint)); int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { string data = config.Encoding.GetString(bytes, 0, i); Dispatcher.Invoke(new Action(() => showData(data))); // data = data.ToUpper(); // byte[] msg = Encoding.ASCII.GetBytes(data); // stream.Write(msg, 0, msg.Length); // Console.WriteLine("Sent: {0}", data); } } } catch (Exception se) { setStatus("Error: " + se.Message); } } // finally // { // stopListener(); // } }