//*********************************************************
        private void JoinB_Click(object sender, EventArgs e) //Join chat with given IP
        {
            int NumInit = InitTB.Text.Length;                //Initial length check

            if (NumInit == 3)
            {
                byte[] bytes = new byte[1024];
                string S     = "";
                try
                {
                    clientSocket.Connect(IPz, 8050);
                    serverStream = clientSocket.GetStream();
                    byte[] outStream = System.Text.Encoding.ASCII.GetBytes("3" + InitTB.Text + "$");
                    serverStream.Write(outStream, 0, outStream.Length);
                    serverStream.Flush();
                    Thread ctThread = new Thread(getMessage);
                    ctThread.Start();
                    Globals.running    = true;
                    JoinB.Enabled      = false;
                    InitTB.Enabled     = false;
                    SendB.Enabled      = true;
                    InputTB.Enabled    = true;
                    ChatCB.Enabled     = true;
                    ActiveTB.BackColor = Color.Green;
                    ActiveL.Text       = "Chat\nActive";
                    ExitB.Show();
                }
                catch (Exception) //Error message for no server
                {
                    System.Windows.Forms.MessageBox.Show("ERROR: CANT CONNECT");
                }
            }
            else //Error message for invalid Init
            {
                System.Windows.Forms.MessageBox.Show("Initials must be\n3 characters");
            }
        }
Пример #2
0
//********************************************************************************************************************
        private void JoinB_Click(object sender, EventArgs e)
        {
            string dataFromServer = "";

            if (InitTB.Text.ToUpper() == "CSW")
            {
                if (PassTB.Text != "cpass")
                {
                    MessageBox.Show("Password Incorrect.");
                    return;
                }
            }
            else if (InitTB.Text.ToUpper() == "PSM")
            {
                if (PassTB.Text != "ppass")
                {
                    MessageBox.Show("Password Incorrect.");
                    return;
                }
            }

            else if (InitTB.Text.ToUpper() == "DRR")
            {
                if (PassTB.Text != "dpass")
                {
                    MessageBox.Show("Password Incorrect.");
                    return;
                }
            }

            else if (InitTB.Text.ToUpper() == "JJM")
            {
                if (PassTB.Text != "jpass")
                {
                    MessageBox.Show("Password Incorrect.");
                    return;
                }
            }
            else
            {
                MessageBox.Show("User not registered.");
                return;
            }
            int NumInit = InitTB.Text.Length;

            if (NumInit == 3)
            {
                byte[] bytes = new byte[1024];
                string S     = "";
                try
                {
                    clientSocket.Connect(IPz, 54206);
                    serverStream = clientSocket.GetStream();

                    string output    = "3" + InitTB.Text + "$";
                    byte[] outStream = System.Text.Encoding.ASCII.GetBytes(output);
                    serverStream.Write(outStream, 0, outStream.Length);

                    Thread.Sleep(500);

                    serverStream.Read(bytes, 0, bytes.Length);
                    dataFromServer = System.Text.Encoding.ASCII.GetString(bytes);
                    dataFromServer = dataFromServer.Substring(0, dataFromServer.IndexOf("$"));
                    serverStream.Flush();
                    //get a stream from the string
                    var sr = new System.IO.StringReader(dataFromServer);
                    //we need a deserializer
                    var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
                    //get the object back from the stream
                    serverPubKey = (RSAParameters)xs.Deserialize(sr);

                    var csp = new RSACryptoServiceProvider(2048);

                    //how to get the private key
                    Globals.myPrivKey = csp.ExportParameters(true);

                    //and the public key ...
                    Globals.myPubKey = csp.ExportParameters(false);

                    string myPubKeyString;
                    {
                        //we need some buffer
                        var sw = new System.IO.StringWriter();
                        //we need a serializer
                        var x = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
                        //serialize the key into the stream
                        x.Serialize(sw, Globals.myPubKey);
                        //get the string from the stream
                        myPubKeyString = sw.ToString();
                    }
                    Thread.Sleep(500);

                    byte[] Stream = System.Text.Encoding.ASCII.GetBytes(myPubKeyString + "$");
                    serverStream.Write(Stream, 0, Stream.Length);

                    Thread ctThread = new Thread(getMessage);
                    ctThread.Start();
                    Globals.running    = true;
                    JoinB.Enabled      = false;
                    InitTB.Enabled     = false;
                    SendB.Enabled      = true;
                    InputTB.Enabled    = true;
                    ChatCB.Enabled     = true;
                    ActiveTB.BackColor = Color.Green;
                    ActiveL.Text       = "Chat\nActive";
                    ExitB.Show();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Initials must be\n3 characters");
            }
        }