async void sendMessage(string message) { byte[] buf; if (status != clientStatus.ENCRYPTED) { return; } string cipher = string.Empty; for (int i = 0; i < message.Length; i += 8) { DESCrypt crypt = new DESCrypt(); cipher += crypt.Encrypt(message.Substring(i, message.Length - i >= 8 ? 8 : message.Length - i), key); } buf = System.Text.Encoding.ASCII.GetBytes(cipher); if (stream.CanWrite) { await stream.WriteAsync(buf, 0, buf.Length); // System.Console.WriteLine("Phy Send: {0}",cipher); } }
async void sendMessageToClient(string message, clientInfo info) { byte[] buf; TcpClient client = info.Client; if (!client.Connected) { return; } NetworkStream stream = client.GetStream(); string cipher = string.Empty; for (int i = 0; i < message.Length; i += 8) { DESCrypt crypt = new DESCrypt(); cipher += crypt.Encrypt(message.Substring(i, message.Length - i >= 8 ? 8 : message.Length - i), info.Key); } buf = System.Text.Encoding.ASCII.GetBytes(cipher); if (stream.CanWrite) { await stream.WriteAsync(buf, 0, buf.Length); } // stream.Close(); }
async void receiveEvent() { byte[] buf = new byte[MAX_BUF_SIZE]; //接收密钥 while (true) { if (stream.CanRead) { int count = stream.Read(buf, 0, MAX_BUF_SIZE); string recv = System.Text.Encoding.ASCII.GetString(buf); //密钥以明文形式发送 if (recv.Contains("KEY:")) { System.Console.WriteLine(recv); key = Convert.FromBase64String(recv.Substring(4, 12)); // System.Console.WriteLine("Key: {0}",Convert.ToBase64String(key)); status = clientStatus.ENCRYPTED; break; } else { stream.Write(System.Text.Encoding.ASCII.GetBytes("SEND KEY"), 0, System.Text.Encoding.ASCII.GetByteCount("SEND KEY")); } } } //接收消息循环 //在服务器端关闭时会报错退出 try { while (true) { string message = string.Empty; // byte[] buf=new byte[MAX_BUF_SIZE]; if (status != clientStatus.ENCRYPTED) { return; } while (true) { int count = await stream.ReadAsync(buf, 0, MAX_BUF_SIZE); message += System.Text.Encoding.ASCII.GetString(buf, 0, count); if (count < MAX_BUF_SIZE) { break; } } // System.Console.WriteLine("Phys Recv: {0}",message); for (int i = 0; i < message.Length; i += 24) { string part = message.Substring(i, message.Length - i >= 24 ? 24 : message.Length - i); DESCrypt crypt = new DESCrypt(); part = crypt.Decrypt(part, key); System.Console.Write(part); } System.Console.WriteLine(); message.Remove(0); } } catch (Exception ex) { System.Console.WriteLine(ex); stream.Close(); client.Close(); Environment.Exit(-1); } }
async void clientEvent(clientInfo info) { byte[] buf = new byte[1024]; TcpClient client = info.Client; NetworkStream stream = client.GetStream(); //发送密钥 if (stream.CanWrite) { stream.Write(info.Key, 0, info.Key.Length); } while (true) { if (status != serverStatus.LISTENING) { return; } else if (!client.Connected) { // clientList.Remove(client); stream.Close(); this.removeClient(info); return; } string recv = string.Empty; try { while (true) { if (!client.Connected) { stream.Close(); this.removeClient(info); // clientList.Remove(client); return; } if (stream.CanRead) { int count = await stream.ReadAsync(buf, 0, MAX_BUF_SIZE); recv += System.Text.Encoding.ASCII.GetString(buf, 0, count); if (count == 0) { stream.Close(); // clientList.Remove(client); this.removeClient(info); return; } else if (count < MAX_BUF_SIZE) { break; } } else { System.Console.WriteLine("Uncatched stream closed"); } } } catch (Exception e) { System.Console.WriteLine(e); stream.Close(); client.Close(); return; } if (recv.Length <= 0) { return; } //处理客户端命令 if (recv == "SEND KEY") { if (stream.CanWrite) { string sendKey = "KEY:" + Convert.ToBase64String(info.Key); await stream.WriteAsync(System.Text.Encoding.ASCII.GetBytes(sendKey), 0, System.Text.Encoding.ASCII.GetByteCount(sendKey)); } continue; } //非客户端命令,解密消息 string message = string.Empty; for (int i = 0; i < recv.Length; i += 24) { string part = recv.Substring(i, recv.Length - i >= 24 ? 24 : recv.Length - i); DESCrypt crypt = new DESCrypt(); message += crypt.Decrypt(part, info.Key); // System.Console.Write(part); } System.Console.WriteLine("New message from:{0}", info.UserName); System.Console.WriteLine("\tContent:{0}", message); string reply = String.Empty; if (!ServerCommands.CheckServerCommands(info, message, out reply)) { string messageToClient = string.Format("{0}:{1}", info.UserName, message); foreach (var i in clientList) { if (i != info && i.Client.Connected) { this.sendMessageToClient(messageToClient, i); } } } else { this.sendMessageToClient(reply, info); } } }