Exemplo n.º 1
0
        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");
                }
            }
        }
Exemplo n.º 2
0
        private void ConnectAndLogin(IPAddress ip, int port, string username, string password)
        {
            try
            {
                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.LoginResponse loginResult = Login(new string[2] {
                        username, password
                    });
                    //Try to log in and return the server response

                    if (loginResult.status == "OK")
                    {
                        MessageBox.Show("Succesfully connected!");
                        Global.username = usernameTextBox.Text;
                        this.Hide();
                        ChatScreen chatScreen = new ChatScreen();
                        chatScreen.WriteLoginMessage(loginResult.loginMessage);
                        chatScreen.Show();
                        //Hide this window and then open the chat screen
                    }
                    //In the following "if sections" no further action will be made, other that warning the user
                    else if (loginResult.status == "USER NOT FOUND")
                    {
                        MessageBox.Show(loginResult.loginMessage);
                    }
                    else if (loginResult.status == "BANNED")
                    {
                        MessageBox.Show("You are banned: " + loginResult.loginMessage);
                    }
                }
                else
                {
                    MessageBox.Show("Couldn't connect to server", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            catch (Exception e) { MessageBox.Show(e.Message); }
        }