void Connected() { ipe = new IPEndPoint(IPAddress.Any, 9050); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(ipe); Thread Listen = new Thread(() => { try { while (true) { server.Listen(100); client = server.Accept(); Connection++; //Gữi public key cho client byte[] laydodai = BitConverter.GetBytes(khoapublic.Length); client.Send(laydodai); byte[] Keypublic = khoapublic; client.Send(Keypublic); //Nhận public key của client byte[] nhandodai = new byte[1024]; client.Receive(nhandodai); nhankey = new byte[BitConverter.ToInt32(nhandodai, 0)]; client.Receive(nhankey); keyclient = Convert.ToBase64String(nhankey); txtkeyclient.Text = keyclient; //Tạo khóa bí mật diff.LayKhoaBiMat(nhankey); khoabimat = diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txtSecretkey.Text = keysecret; ns = new NetworkStream(client); LstMS.Items.Add("Co:" + Connection + " client ket noi toi."); string welcome = "Welcome to my test server"; byte[] data = new byte[1024]; data = Encoding.ASCII.GetBytes(welcome); ns.Write(data, 0, data.Length); Thread receive = new Thread(Receive); receive.IsBackground = true; receive.Start(client); } } catch { ipe = new IPEndPoint(IPAddress.Any, 9050); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } } ); CreatePublicKey(); Listen.IsBackground = true; Listen.Start(); }
void Connect() { clientList = new List <Socket>(); //IP của server (127.0.0.1) IP = new IPEndPoint(IPAddress.Any, 9999); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //đợi ip server.Bind(IP); Thread listen = new Thread(() => { try { while (true) { server.Listen(100); client = server.Accept(); clientList.Add(client); // lấy độ dài khóa public của server byte[] laydodai = BitConverter.GetBytes(khoapublic.Length); // gửi độ dài khóa public cho client client.Send(laydodai); // lấy khóa public byte[] Keypublic = khoapublic; // gửi khóa public cho client client.Send(Keypublic); // Nhận độ dài của khóa public từ client byte[] nhandodai = new byte[1024]; client.Receive(nhandodai); // Nhận khóa public của client nhankey = new byte[BitConverter.ToInt32(nhandodai, 0)]; client.Receive(nhankey); // add vào textbox keyclient = Convert.ToBase64String(nhankey); txtkeyclient.Text = keyclient; //Lấy khóa public từ client để tạo khóa chung diff.LayKhoaBiMat(nhankey); khoabimat = diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txbSecretkey.Text = keysecret; Thread receive = new Thread(Receive); receive.IsBackground = true; receive.Start(client); } } catch { MessageBox.Show("Có lỗi trao đổi Key"); } }); TaoKey(); listen.IsBackground = true; listen.Start(); }
void Connect() { clientList = new List <Socket>(); IP = new IPEndPoint(IPAddress.Any, 9999); server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); server.Bind(IP); Thread listen = new Thread(() => { try { while (true) { server.Listen(100); client = server.Accept(); clientList.Add(client); byte[] laydodai = BitConverter.GetBytes(khoapublic.Length); client.Send(laydodai); byte[] Keypublic = khoapublic; client.Send(Keypublic); byte[] nhandodai = new byte[1024]; client.Receive(nhandodai); nhankey = new byte[BitConverter.ToInt32(nhandodai, 0)]; client.Receive(nhankey); keyclient = Convert.ToBase64String(nhankey); textBox2.Text = keyclient; diff.LayKhoaBiMat(nhankey); khoabimat = diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); textBox4.Text = keysecret; Thread receive = new Thread(Receive); receive.IsBackground = true; receive.Start(client); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }); TaoKey(); listen.IsBackground = true; listen.Start(); }