private void button_login_Click(object sender, RoutedEventArgs e) { TcpClient tcpClient; IPAddress ServerIP; string msg = string.Empty; try { string[] ip = textBox_ip.Text.Split(':'); tcpClient = new TcpClient(); ServerIP = IPAddress.Parse(ip[0]); tcpClient.Connect(ServerIP, int.Parse(ip[1])); //建立与服务器的连接 NetworkStream networkStream = tcpClient.GetStream(); if (networkStream.CanWrite) { IMClassLibrary.LoginDataPackage loginDataPackage = new IMClassLibrary.LoginDataPackage("127.0.0.1:" + MyPort.ToString(), "Server_Login", textBox_id.Text, sha256(passwordBox.Password)); //初始化登录数据包 Byte[] sendBytes = loginDataPackage.DataPackageToBytes(); //登录数据包转化为字节数组 networkStream.Write(sendBytes, 0, sendBytes.Length); } msg = ListenThreadMethod(); } catch { MessageBox.Show("与服务器连接失败!"); } if (msg == "登录成功") { P2PClient client = new P2PClient(textBox_id.Text, tcpListener, MyPort, textBox_ip.Text.Split(':')[1], passwordBox_Copy.Password); //传入用户名&登录端口 client.Show(); Close(); } else { MessageBox.Show("登录失败!"); } }