public override void OnAcceptConnection(ConnectionState state) { _receivedStr = ""; if (!state.Write(Encoding.UTF8.GetBytes("Hello World!\r\n"), 0, 14)) { state.EndConnection(); //if write fails... then close connection } }
private void b_send_Click(object sender, EventArgs e) { String _receivedStr = txtTrama.Text + "\n"; if (rb_IP.Checked) { if (Server._connections.Count == 0) { MessageBox.Show("No tiene conexiones activas."); } else { TcpLib.ConnectionState cs = (TcpLib.ConnectionState)Server._connections[Server._connections.Count - 1]; cs.Write(Encoding.UTF8.GetBytes(_receivedStr), 0, _receivedStr.Length); _receivedStr = ""; } } if (rbSerial.Checked) { SerialPort port = new SerialPort("COM5"); port.BaudRate = 1200; port.Parity = Parity.None; port.StopBits = StopBits.One; port.DataBits = 8; port.Handshake = Handshake.None; port.RtsEnable = true; port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); port.Open(); // Write a string port.Write(_receivedStr); // Write a set of bytes //port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3); port.Close(); } }