private void btnLogin_Click(object sender, EventArgs e) { resetState(); if (String.IsNullOrEmpty(iUsername.Text)) { panel8.BackColor = red; username.ForeColor = red; flag = false; } if (String.IsNullOrEmpty(iPassword1.Text)) { panel4.BackColor = red; label3.ForeColor = red; flag = false; } if (flag) { socket = new Client(); int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject commandToBeSent = new Client.ModelObject(); commandToBeSent.command.code = "1"; commandToBeSent.command.id = $"{id}"; commandToBeSent.user.name = iUsername.Text; commandToBeSent.user.pass = SHA256HexHashString(iPassword1.Text); tmpPD = commandToBeSent.user.pass; Console.WriteLine(commandToBeSent.user.pass); socket.Send(commandToBeSent); RecvData(1, commandToBeSent.command.id); } }
private void btnSubmit_Click(object sender, EventArgs e) { if (currentBtn == btnEditProfile) { int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "12"; Send.command.id = $"{id}"; Send.user.ava = ClientForm.currentUser.ava; Send.user.alias = textbox1.Text; Send.user.name = ClientForm.currentUser.name; Send.user.email = textbox2.Text; register.socket.Send(Send); RecvData(12, Send.command.id); textbox1.Text = ClientForm.currentUser.alias; textbox2.Text = ClientForm.currentUser.email; } else { if (!rg.IsMatch(username.Text)) { MessageBox.Show("The length of password must be 6-16"); return; } string messageDigest = register.SHA256HexHashString(textbox1.Text); if (messageDigest != ClientForm.currentUser.pass) { MessageBox.Show("Wrong password! Please try again."); } else if (textbox1.Text == textbox2.Text) { MessageBox.Show("The new password must be different the old password!"); } else { int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "11"; Send.command.id = $"{id}"; Send.user.pass = register.SHA256HexHashString(textbox3.Text); Send.user.alias = ClientForm.currentUser.alias; Send.user.ava = ClientForm.currentUser.ava; Send.user.email = ClientForm.currentUser.email; Send.user.name = ClientForm.currentUser.name; register.socket.Send(Send); RecvData(11, Send.command.id); textbox1.Text = ""; textbox2.Text = ""; textbox3.Text = ""; } } }
public void RecvData(int flag, string commandId) { Client.ModelObject data = new Client.ModelObject(); int p = 0; while (p < 5000) { data = register.socket.GetCommandResult(commandId); if ((data != null)) { switch (flag) { case RecvCreate: if (data.command.status != "0") { MessageBox.Show("Failed to create!"); break; } listExistRoom.Add(new existRoom(data.room.id, data.room.name, false, null, data.room.members)); MessageBox.Show("Create successfully!"); JoinRoom(); break; case RecvJoin: if (data.command.status != "0") { MessageBox.Show("Failed to create!"); break; } listExistRoom.Add(new existRoom(data.room.id, data.room.name, false, null, data.room.members)); MessageBox.Show("Join successfully"); JoinRoom(); break; case Leave: if (data.command.status != "0") { MessageBox.Show("Failed to leave, please try again!"); break; } listExistRoom.RemoveAll(item => item.IDROOM.Equals(currentRoom.IDROOM)); break; } return; } p += 50; Thread.Sleep(50); } MessageBox.Show("Timeout!"); }
private void btnLogOut_Click(object sender, EventArgs e) { int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "9"; Send.command.id = $"{id}"; register.socket.Send(Send); CloseForm(); }
public static void RecvData(int flag, string commandId) { Client.ModelObject data = new Client.ModelObject(); BigInteger p = 0; while (p < 5000000000) { data = socket.GetCommandResult(commandId); if ((data != null)) { switch (flag) { case RecvLogin: if (data.command.status != "0") { MessageBox.Show("Failed to login!"); break; } MessageBox.Show("Successful!"); ClientForm clientForm = new ClientForm(); ClientForm.currentUser = data.user; ClientForm.currentUser.pass = tmpPD; if (!String.IsNullOrEmpty(ClientForm.currentUser.ava)) { clientForm.setImageClientForm(Profile.Base64ToImage(ClientForm.currentUser.ava)); } tmpPD = null; if (data.message.existroom != null) { clientForm.listRoom.AddRange(data.message.existroom); } Console.WriteLine(data); thisForm.Hide(); clientForm.BringToFront(); clientForm.Show(); break; case RecvRegister: if (data.command.status != "0") { MessageBox.Show("Failed to register!"); break; } MessageBox.Show("You've been registered successfully!"); break; } return; } p += 50; } MessageBox.Show("Timeout!"); }
private void btnCreateJoinRoom_Click(object sender, EventArgs e) { if (!String.IsNullOrWhiteSpace(textJoin.Text) && !String.IsNullOrWhiteSpace(textJoin.Text)) { if (btnJoin.Text == "Create") { int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "3"; Send.command.id = $"{id}"; Send.room.name = textJoin.Text; register.socket.Send(Send); if (subSetting.Visible == true) { subSetting.Visible = false; } textJoin.Text = ""; RecvData(3, Send.command.id); } else { for (int i = 0; i < listExistRoom.Count; i++) { if (listExistRoom[i].IDROOM == textJoin.Text) { MessageBox.Show("Room has been joined!"); return; } } int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "4"; Send.command.id = $"{id}"; Send.room.id = textJoin.Text; register.socket.Send(Send); if (subSetting.Visible == true) { subSetting.Visible = false; } textJoin.Text = ""; RecvData(4, Send.command.id); } } }
private void RecvData(int flag, string commandId) { Client.ModelObject data = new Client.ModelObject(); int p = 0; while (p < 5000) { Thread.Sleep(50); data = register.socket.GetCommandResult(commandId); if ((data != null)) { switch (flag) { case ChangeAlias: if (data.command.status != "0") { MessageBox.Show("Something went wrong, cant change profile!"); break; } ClientForm.currentUser.alias = textbox1.Text; ClientForm.currentUser.email = textbox2.Text; MessageBox.Show("Successful"); break; case ChangePassword: if (data.command.status != "0") { MessageBox.Show("Faild to change password!"); break; } ClientForm.currentUser.pass = register.SHA256HexHashString(textbox3.Text); MessageBox.Show("Successful"); break; } return; } p += 50; } MessageBox.Show("Timeout!"); }
private void sendMessage(object sender, EventArgs e) { if (!string.IsNullOrEmpty(textboxMessage.Text)) { MyBubble bubble = new MyBubble(1); bubble.Dock = DockStyle.Bottom; bubble.SendToBack(); bubble.Body = textboxMessage.Text; currentRoom.myPanel.Controls.Add(bubble); int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "5"; Send.command.id = $"{id}"; Send.message.type = "1"; Send.message.data = textboxMessage.Text; Send.room.id = currentRoom.IDROOM; register.socket.Send(Send); textboxMessage.Text = ""; } }
private void btnLeave_Click(object sender, EventArgs e) { int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "10"; Send.command.id = id.ToString(); Send.room.id = currentRoom.IDROOM; register.socket.Send(Send); btnSetting_Click(sender, e); foreach (Client.ModelObject.Room x in listRoomFromTop) { if (x.id == currentRoom.IDROOM) { listRoomFromTop.Remove(x); break; } } RecvData(10, Send.command.id); currentRoom.myPanel.Dispose(); currentRoom.myRoom.Dispose(); currentRoom = null; if (listExistRoom.Count != 0) { currentRoom = listExistRoom[0]; ShowChatPanel(currentRoom.myPanel, currentRoom.myRoom); } else { nameRoomHeader.Text = "Welcome"; roomIdHeader.Text = "000000"; } }
private void btnAddFile_Click(object sender, EventArgs e) { var fileContent = string.Empty; var filePath = string.Empty; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.Filter = "Image Files(*.jpg, *png)|*jpg;*png|Txt files(*.txt)|*.txt|Pdf files(*.pdf)|*pdf|All files (*.*)|*.*"; openFileDialog.FilterIndex = 4; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { FileInfo fi = new FileInfo(openFileDialog.FileName); long fileSize = fi.Length; if (fileSize > 10 * 1024 * 1024) { MessageBox.Show("The size is over limited size."); return; } filePath = openFileDialog.FileName; string[] partsFileName = filePath.Split('.'); if (partsFileName[partsFileName.Length - 1] == "jpg" || partsFileName[partsFileName.Length - 1] == "png") { byte[] bytesFromImage = ImageToByteArray(Image.FromFile(filePath)); string base64OfImgae = Convert.ToBase64String(bytesFromImage); Image x = Base64ToImage(base64OfImgae); Upload ul = new Upload(x); Form form1 = new Form(); form1.BackColor = System.Drawing.Color.Black; form1.Opacity = (double)0.5; form1.FormBorderStyle = FormBorderStyle.None; form1.Height = ActiveForm.Height; form1.Width = ActiveForm.Width; form1.FormBorderStyle = FormBorderStyle.None; form1.ShowIcon = false; form1.ShowInTaskbar = false; form1.BringToFront(); form1.Show(); ul.BringToFront(); ul.ShowDialog(); form1.Dispose(); if (ul.flag) { MyBubble bubble = new MyBubble(0); bubble.Dock = DockStyle.Bottom; bubble.SendToBack(); bubble.ImageOfMessage = x; currentRoom.myPanel.Controls.Add(bubble); int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "5"; Send.command.id = $"{id}"; Send.message.type = "0"; Send.room.id = currentRoom.IDROOM; Send.message.data = base64OfImgae; register.socket.Send(Send); } } else { byte[] bytesFromFile = File.ReadAllBytes(filePath); string base64OfFile = Convert.ToBase64String(bytesFromFile); MyBubble bubble = new MyBubble(2); bubble.Dock = DockStyle.Bottom; bubble.SendToBack(); bubble.ChatTextCursor = Cursors.Hand; bubble.Body = $"Đã gửi {filePath.Split('\\')[filePath.Split('\\').Length - 1]}"; currentRoom.myPanel.Controls.Add(bubble); string body = $"{bubble.Body}[;]{base64OfFile}"; int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "5"; Send.command.id = $"{id}"; Send.message.type = "2"; Send.room.id = currentRoom.IDROOM; Send.message.data = body; register.socket.Send(Send); } } } }
private void btnRegister_Click_1(object sender, EventArgs e) { bool flag = true; if (iUsername.Text.Length < 6 || !rg.IsMatch(iUsername.Text)) { flag = false; if (String.IsNullOrEmpty(iUsername.Text) || String.IsNullOrWhiteSpace(iUsername.Text)) { hint1.Text = "Username can't be null or space"; } else if (!rg.IsMatch(iUsername.Text)) { hint1.Text = "Username just has text and digits"; } else { hint1.Text = "The length of username is from 6 until 15"; } line1.BackColor = red; username.ForeColor = red; } if (iPassword1.Text != iPassword2.Text || iPassword1.Text.Length < 6) { flag = false; if (iPassword1.Text != iPassword2.Text) { hint2.Text = "The two passwords must be the same"; hint3.Text = hint2.Text; } else { hint2.Text = "The length of password must longer than 6"; hint3.Text = hint2.Text; } label3.ForeColor = red; label4.ForeColor = red; line2.BackColor = red; line3.BackColor = red; } if (bunifuCheckbox1.Checked == false) { term.ForeColor = red; flag = false; } if (String.IsNullOrWhiteSpace(iEmail.Text)) { iEmail.Text = ""; } if (flag) { MessageBox.Show("Register..."); register.socket = new Client(); int ranNumber = Program.RandomNumber(2, 2 ^ 32); Client.ModelObject Send = new Client.ModelObject(); Send.command.code = "2"; Send.command.id = $"{ranNumber}"; Send.user.name = iUsername.Text; Send.user.pass = register.SHA256HexHashString(iPassword1.Text); Send.user.email = iEmail.Text; register.socket.Send(Send); register.RecvData(2, Send.command.id); } if (!flag) { MessageBox.Show("Failed to register"); } resetState(1); }
private void labelChangePhoto_Click(object sender, EventArgs e) { OpenFileDialog op = new OpenFileDialog(); op.Title = "Select a picture"; op.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png"; if (op.ShowDialog() == DialogResult.OK) { if ((int)new System.IO.FileInfo(op.FileName).Length > 300 * 1024) { MessageBox.Show("The size of image is too large"); return; } string appPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\data\"; if (Directory.Exists(appPath) == false) { Directory.CreateDirectory(appPath); } try { var filePath = op.FileName; string iName = op.SafeFileName; string filepath = op.FileName; Bitmap x = new Bitmap(op.OpenFile()); avatar.Image = (Image)x; byte[] imageBytes = ImageToByte2((Image)x); ClientForm.currentUser.ava = System.Text.Encoding.UTF8.GetString(imageBytes); string base64String = Convert.ToBase64String(imageBytes); ClientForm.currentUser.ava = base64String; textbox1.Text = ClientForm.currentUser.alias; textbox2.Text = ClientForm.currentUser.email; int id = Program.RandomNumber(0, 2 ^ 32); Client.ModelObject commandToBeSent = new Client.ModelObject(); commandToBeSent.command.code = "12"; commandToBeSent.command.id = $"{id}"; commandToBeSent.user.alias = ClientForm.currentUser.alias; commandToBeSent.user.email = ClientForm.currentUser.email; commandToBeSent.user.name = ClientForm.currentUser.name; commandToBeSent.user.ava = base64String; register.socket.Send(commandToBeSent); RecvData(12, commandToBeSent.command.id); } catch (Exception exp) { MessageBox.Show("Unable to open file " + exp.Message); } } else { op.Dispose(); } }