private void Client_Disconnected(CLient sender) { this.Invoke(() => { for (int i = 0; i < lstClients.Items.Count; i++) { var client = lstClients.Items[i].Tag as CLient; if (client.Ip == sender.Ip) { txtReceive.Text += lstClients.Items[i].SubItems[1].Text + " !!! Has Left the Chat !!!\r\n"; BroadcastData("RefreshChat|" + txtReceive.Text); lstClients.Items.RemoveAt(i); } } }); }
private void Listener_SocketAccepted(Socket socket) { var client = new CLient(socket); client.Received += Client_Received; client.Disonnected += Client_Disconnected; this.Invoke(() => { string ipAdress = client.Ip.ToString().Split(':')[0]; var item = new ListViewItem(ipAdress); item.SubItems.Add(" "); item.SubItems.Add(" "); item.Tag = client; lstClients.Items.Add(item); clients.Add(socket); }); }
private void Client_Received(CLient sender, byte[] data) { this.Invoke(() => { for (int i = 0; i < lstClients.Items.Count; i++) { var client = lstClients.Items[i].Tag as CLient; if (client == null || client.Ip != sender.Ip) { continue; } var command = Encoding.ASCII.GetString(data).Split('|'); switch (command[0]) { case "Connect": txtReceive.Text += command[1] + " *** Joined The Chat *** \r\n"; lstClients.Items[i].SubItems[1].Text = command[1]; lstClients.Items[i].SubItems[2].Text = command[2]; string users = string.Empty; for (int j = 0; j < lstClients.Items.Count; j++) { users += lstClients.Items[j].SubItems[1].Text + "|"; } BroadcastData("Users|" + users.TrimEnd('|')); BroadcastData("RefreshChat|" + txtReceive.Text); break; case "Message": txtReceive.Text += command[1] + " : " + command[2] + "\r\n"; BroadcastData("RefreshChat|" + txtReceive.Text); break; case "pMessage": this.Invoke( () => { pChat.txtReceivedMsg.Text += command[1] + " : " + command[2] + "\r\n"; }); break; case "pChat": break; } } }); }