void Receive() { try { while (true) { byte[] data = new byte[1024 * 5000]; clientSocket.Receive(data); string message = (string)Deserialize(data); string MsgType = message.Split('|')[2]; if (MsgType == "ClientData") { string sendPoint = message.Split('|')[0]; bool checkClientExist = false; foreach (Client client in listClient) { if (client.EndPoint == sendPoint) { checkClientExist = true; break; } } if (checkClientExist == false) { Client client = new Client(message.Split('|')[0], message.Split('|')[3]); listClient.Add(client); AddClientToPnlListClient(client); AddClientToPnlMsg(client); } if (SelectedClient == null) { SelectedClient = listClient[0]; labelSelectedClient.Text = SelectedClient.Name + " - " + SelectedClient.EndPoint; foreach (Control control in pnlListClients.Controls) { if (control.Name == SelectedClient.EndPoint) { control.BackColor = Color.FromArgb(180, 180, 180); } } } } else if (MsgType == "Image") { Client client = GetClientData(message.Split('|')[0]); Image image = DecryptImage(message.Split('|')[3]); if (image != null) { DisplayImage(image, "receive", client); client.NewMsg = true; foreach (Panel panel in pnlListClients.Controls) { if (panel.Name == client.EndPoint) { if (this.InvokeRequired) { this.BeginInvoke((MethodInvoker) delegate() { panel.BackColor = Color.Red; }); } else { panel.BackColor = Color.Red; } } } } } else if (MsgType.Contains("Text") == true) { //string cypher_sign = message.Split('~')[1]; string cypher = message.Split('|')[3]; string sign = message.Split('|')[4]; string msgIndex = MsgType.Remove(0, 4); string msg = RSA_Crypto.Decrypt(Convert.FromBase64String(cypher)); Client client = GetClientData(message.Split('|')[0]); string publicKey = client.PublicKey; byte[] signature = Convert.FromBase64String(sign); MD5 md5 = MD5.Create(); string hashedMsg = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(msg))); if (RSA_Crypto.VerifyData(publicKey, msg, signature) == true) { DisplayText(msg, "receive", msgIndex, client); SendEcho(client, hashedMsg, msgIndex); } if (WindowState != FormWindowState.Minimized && SelectedClient == client) { Thread.Sleep(10); SendEcho(client, hashedMsg, msgIndex); } else { Message unreadMsg = new Message(Convert.ToInt32(msgIndex), hashedMsg, 1); client.UnreadMessages.Add(unreadMsg); client.NewMsg = true; foreach (Panel panel in pnlListClients.Controls) { if (panel.Name == client.EndPoint) { if (this.InvokeRequired) { this.BeginInvoke((MethodInvoker) delegate() { panel.BackColor = Color.Red; }); } else { panel.BackColor = Color.Red; } } } } } else if (MsgType.Contains("Echo") == true) { string cypher = message.Split('|')[3]; string sign = message.Split('|')[4]; string msgIndex = MsgType.Remove(0, 4); string msg = RSA_Crypto.Decrypt(Convert.FromBase64String(cypher)); Client client = GetClientData(message.Split('|')[0]); string publicKey = client.PublicKey; byte[] signature = Convert.FromBase64String(sign); if (RSA_Crypto.VerifyData(publicKey, msg, signature) == true) { int state = client.ChangeMsgState(msgIndex, msg); ChangeMsgState(msgIndex, state, client); } } } } catch (Exception e) { Close(); } }
void Receive() { try { bool check; while (true) { byte[] data = new byte[1024 * 5000]; client.Receive(data); string message = (string)Deserialize(data); if (message.Contains("server") == true) { message = message.Replace("server ", ""); check = false; if (message != client.LocalEndPoint.ToString()) { foreach (string str in address) { if (str == message) { check = true; break; } } if (check == false) { address.Add(message); } BindingSource bs = new BindingSource(); bs.DataSource = address; cmbIP.DataSource = bs; } } else if (message.Contains("all") == true) { string sendPoint = message.Split('|')[0]; bool checkD = false; for (int i = 0; i < index; i++) { if (IP_PK[i, 0] == sendPoint) { checkD = true; break; } } if (sendPoint != client.LocalEndPoint.ToString() && checkD == false) { IP_PK[index, 0] = message.Split('|')[0]; IP_PK[index, 1] = message.Split('|')[2]; index++; } //AddMessage(message); } else if (message.Contains("Image")) { Image image = DecryptImage(message.Split('~')[1]); if (image != null) { DisplayImage(image, "receive"); imageIndex++; } } else { string str = RSA_Crypto.Decrypt(Convert.FromBase64String(message.Split('|')[2])); DisplayText(str, "receive"); } } } catch (Exception e) { Close(); } }