// gửi gói tin public void Send(string ms) { try { // gửi dữ liệu ở dạng nhị phân (byte) myUser.setMessage(ms); NetworkStream netStream = clt.GetStream(); byte[] data = myUser.GetBytes(); int size = myUser.size; byte[] packsize = new byte[2]; // convert byte -> string packsize = BitConverter.GetBytes(size); netStream.Write(packsize, 0, 2); netStream.Write(data, 0, size); netStream.Flush(); } catch { MessageBox.Show("Lỗi kết nối", "Lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } }
private void loginBtn_Click(object sender, EventArgs e) { string name = usNameTxt.Text; string pass = passTxt.Text; if (name != "" && pass != "") { BllClient userSending = new BllClient(name, pass); userSending.setMessage("dang_nhap-" + keyDangNhap); myUser = userSending; myUser.receiveDir = 1; sendUserObject(userSending); } else { MessageBox.Show("User name or Password are error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void sendMessage(string ms) { try { myUser.setMessage(ms); NetworkStream netStream = clt.GetStream(); byte[] data = myUser.GetBytes(); int size = myUser.size; byte[] packsize = new byte[2]; packsize = BitConverter.GetBytes(size); netStream.Write(packsize, 0, 2); netStream.Write(data, 0, size); netStream.Flush(); } catch { MessageBox.Show("Error login", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }