示例#1
0
 /*a character '$' is used as a terminator for every client-server message,
  * also the message always starts with either "I:" or "M:", which specifies whether
  * it's a text message from the client, or a background information for the app*/
 private void SendMessage(string msg)
 {
     try
     {
         serverStream = clientSocket.GetStream();
         byte[] outStream = Encoding.Unicode.GetBytes("M:" + msg + "$");
         serverStream.Write(outStream, 0, outStream.Length);
         serverStream.Flush();
     }
     //handled the case when server is down
     catch
     {
         serverDead = true;
         PopForm p = new PopForm("Server down");
         p.ShowDialog();
         this.Hide();
         p.Closed += (s, args) => this.Close();
     }
 }
示例#2
0
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count > 0)
            {
                NetworkStream serverStream = clientSocket.GetStream();
                //the chosen room is send to the server
                byte[] outStream = Encoding.Unicode.GetBytes("I:" + listBox1.SelectedItem.ToString() + "$");
                serverStream.Write(outStream, 0, outStream.Length);

                string answer = "";
                serverStream = clientSocket.GetStream();
                byte[] inStream = new byte[10025];
                //answer from the server is read
                serverStream.Read(inStream, 0, inStream.Length);
                answer = Encoding.Unicode.GetString(inStream);
                if (answer.StartsWith("I:"))
                {
                    answer = answer.Substring(0, answer.IndexOf("$"));
                    answer = answer.Substring(2, answer.Length - 2);
                    //handled the case when the room got removed while choosing
                    if (answer == "RoomGone")

                    {
                        this.Hide();
                        var p = new PopForm("This room dissapeared :/");
                        p.Closed += (s, args) => this.Show();
                        p.Show();
                    }
                    //if everything is ok, the chat window is showed
                    else
                    {
                        this.Hide();
                        var f = new Form1(ip, username, clientSocket, listBox1.SelectedItem.ToString());
                        f.Show();
                        closedByStupidUser = false;
                    }
                }
            }
        }
示例#3
0
        private void button1_Click(object sender, EventArgs e)
        {
            usernameUsed = false;
            connected    = false;
            string in1 = textBox1.Text;
            string in2 = textBox2.Text;

            //ip address check and username check are both performed
            if (IPAddress.TryParse(in1, out ip))
            {
                ip        = IPAddress.Parse(in1);
                correctIP = true;
            }
            else
            {
                label1.Text = "Please enter a valid IPv4 address";
                correctIP   = false;
                label3.Text = "";
            }

            if (r.IsMatch(in2) && in2.Length > 2 && in2.Length < 13)
            {
                userName        = in2;
                correctUsername = true;
            }
            else
            {
                userName        = in2; ////
                label2.Text     = "Letters and numbers only, 3-12 chars";
                correctUsername = false;
                label3.Text     = "";
            }
            //if both are correct, client will try to send a chosen username, which is
            //anticipated by the server
            if (correctIP && correctUsername)
            {
                TcpClient     clientsocket = new TcpClient();
                NetworkStream serverStream = default(NetworkStream);

                try
                {
                    //port 8888 is chosen for the app
                    clientsocket.Connect(ip, 8888);
                    connected    = true;
                    serverStream = clientsocket.GetStream();
                    byte[] outStream = Encoding.Unicode.GetBytes("I:" + userName + "$");
                    serverStream.Write(outStream, 0, outStream.Length);
                    string answer = null;
                    //waiting for an answer from the server
                    while (true)
                    {
                        answer = "";

                        serverStream = clientsocket.GetStream();
                        byte[] inStream = new byte[10025];
                        serverStream.Read(inStream, 0, inStream.Length);
                        answer = Encoding.Unicode.GetString(inStream);
                        if (answer.StartsWith("I:"))
                        {
                            answer = answer.Substring(0, answer.IndexOf("$"));
                            answer = answer.Substring(2, answer.Length - 2);
                            //distinguishing whether the username is ok
                            if (answer == "UsernameExists")
                            {
                                usernameUsed = true; break;
                            }
                            if (answer == "UrOK")
                            {
                                break;
                            }
                        }
                        else
                        {
                            this.Hide();
                            var p = new PopForm("DunnoWhatHappened");
                            p.Closed += (s, args) => this.Close();
                            p.Show();
                        }
                    }
                }
                catch
                {
                    connected = false;
                }
                if (connected && !usernameUsed)
                {
                    /*  this.Hide();
                     * var f = new Form1(ip, userName, clientsocket);
                     * f.Closed += (s, args) => this.Close();
                     * f.Show();*/////
                    this.Hide();
                    var f = new Form3(ip, userName, clientsocket);
                    f.Closed += (s, args) => this.Close();
                    f.Show();
                }
                //if the connection attempt failed for some reason, e.g. server not running on a specified ip
                else if (!connected)
                {
                    label3.Text = "Could not connect";
                }
                else if (usernameUsed)
                {
                    this.Hide();
                    var p = new PopForm("Username exists");
                    p.Closed += (s, args) => this.Show();
                    p.Show();
                }
            }
        }