private void ConnectAndRegister(IPAddress ip, int port, string username, string password) { Global.Net.server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Global.Net.server.Connect(new IPEndPoint(ip, port)); //Connect to server if (Global.Net.server.Connected) { Global.Messages.Registration registrationPackage = new Global.Messages.Registration(); registrationPackage.username = username; registrationPackage.password = password; Global.Messages.LoginResponse registrationResult = Register(registrationPackage); if (registrationResult.status == "OK") { MessageBox.Show("Succesfully registered and connected!"); Global.username = usernameTextBox.Text; this.Hide(); ChatScreen chatScreen = new ChatScreen(); chatScreen.WriteLoginMessage(registrationResult.loginMessage); chatScreen.Show(); //Hide this window and then open the chat screen } else if (registrationResult.status == "USER ALREDY EXISTS") { MessageBox.Show("Error : The specified user alredy exists"); } } }
private Global.Messages.LoginResponse Register(Global.Messages.Registration reg) { string registerPackage = JsonConvert.SerializeObject(reg); //Convert the object to a JSON string Global.Net.server.Send(Encoding.UTF8.GetBytes(registerPackage)); //The string to bytes and send it byte[] response = new byte[256]; Global.Net.server.Receive(response); Global.Messages.LoginResponse deserializedResponse = JsonConvert.DeserializeObject <Global.Messages.LoginResponse>(Encoding.UTF8.GetString(response)); return(deserializedResponse); }