private void timerClientes_Tick(object sender, EventArgs e) { if (TCPServer != null) { if (TCPServer.Pending()) { TcpClient tcpClient = TCPServer.AcceptTcpClient(); cuenta++; string llega = String.Format("{0}", tcpClient.Client.RemoteEndPoint); listBox1.Items.Add(new ClaseCliente { direccion = "Cliente " + cuenta.ToString() + " (" + llega + ")", cliente = tcpClient }); } } for (int i = 0; i < listBox1.Items.Count; i++) { ClaseCliente elcliente = (ClaseCliente)listBox1.Items[i]; if (!elcliente.cliente.Connected) { listBox1.Items.RemoveAt(i); elcliente = null; } } }
private void button1_Click(object sender, EventArgs e) { if (listBox1.SelectedItem != null) { ClaseCliente elcliente = (ClaseCliente)listBox1.SelectedItem; if (elcliente.cliente.Connected) { elcliente.enviaMensaje(textBox1.Text); //Elimina lo que se encuentra en textBox1 textBox1.Text = ""; } } }