Exemple #1
0
        private void Connectbutton_Click(object sender, EventArgs e)
        {
            client = new TcpClient();
            IPEndPoint IpEnd = new IPEndPoint(IPAddress.Parse(ClientIPtextBox.Text), int.Parse(ClientPorttextBox.Text));

            try
            {
                client.Connect(IpEnd);

                if (client.Connected)
                {
                    Chat_textBox.AppendText("Connected to server" + "\n");
                    Chat_textBox.AppendText(Environment.NewLine);
                    STW           = new StreamWriter(client.GetStream());
                    STR           = new StreamReader(client.GetStream());
                    STW.AutoFlush = true;
                    backgroundWorker1.RunWorkerAsync();
                    backgroundWorker2.WorkerSupportsCancellation = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
Exemple #2
0
 private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
 {
     if (client.Connected)
     {
         STW.WriteLine(TextToSend);
         this.Chat_textBox.Invoke(new MethodInvoker(delegate()
         {
             Chat_textBox.AppendText("Me:" + TextToSend + "\n");
             Chat_textBox.AppendText(Environment.NewLine);
         }));
     }
     else
     {
         MessageBox.Show("Sending failed");
     }
     backgroundWorker2.CancelAsync();
 }
Exemple #3
0
 private void DecryptButton_Click(object sender, EventArgs e)
 {
     try
     {
         recieve = Decryption(Key_textBox.Text, Msg_textBox.Text, Key_textBox2.Text);
         if (!String.IsNullOrWhiteSpace(recieve))
         {
             this.Chat_textBox.Invoke(new MethodInvoker(delegate()
             {
                 Chat_textBox.AppendText("Sender:" + recieve + "\n");
                 Chat_textBox.AppendText(Environment.NewLine);
             }));
             recieve = "";
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message.ToString());
     }
 }
        private void AddMessage(Message message, Socket server)
        {
            if (currentTime.ToShortDateString() != message.GetTime.ToShortDateString())
            {
                Chat_textBox.AppendText("                          " + message.GetTime.ToShortDateString() + Environment.NewLine,
                                        Color.LightSlateGray, FontStyle.Italic);
                currentUser = null;     //скинем чтобы написал
            }

            if (currentUser != message.GetUsername || messageCount == 10)
            {
                Chat_textBox.AppendText("" + message.GetUsername, Color.DarkBlue, FontStyle.Regular);
                Chat_textBox.AppendText("    " + message.GetTime.ToShortTimeString() + Environment.NewLine,
                                        Color.LightGray, FontStyle.Regular);
                messageCount = 0;
            }


            if (message.GetMessageType == MessageType.PrivateMessage)
            {
                Chat_textBox.AppendText("   " + message.GetMessage + Environment.NewLine, Color.Firebrick, FontStyle.Bold);
            }
            else
            {
                Chat_textBox.AppendText("   " + message.GetMessage + Environment.NewLine, Color.Black, FontStyle.Regular);
            }

            currentTime = message.GetTime;
            currentUser = message.GetUsername;
            messageCount++;

            if (message.GetMessageType == MessageType.HistoryMessage)
            {
                server.Send(Encoding.UTF8.GetBytes(
                                new Message(MessageType.Message, DateTime.Now).Serialize())); //посылаем подтверждение DON'T TOUCH
            }
        }
Exemple #5
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (client.Connected)
            {
                StatusStripConnection.Text = "Connection Status : Connected";
                statusStrip1.BackColor     = Color.LightGreen;

                cipher           = STR.ReadLine();
                Msg_textBox.Text = cipher;
                if (AutoDecryptCheckBox.Checked)
                {
                    try
                    {
                        recieve = Decryption(Key_textBox.Text, cipher, Key_textBox2.Text);
                        if (!String.IsNullOrWhiteSpace(recieve))
                        {
                            this.Chat_textBox.Invoke(new MethodInvoker(delegate()
                            {
                                Chat_textBox.AppendText("Sender:" + recieve + "\n");
                                Chat_textBox.AppendText(Environment.NewLine);
                            }));
                            recieve = "";
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message.ToString());
                    }
                }
            }
            if (!client.Connected)
            {
                StatusStripConnection.Text = "Connection Status : Not Connected";

                statusStrip1.BackColor = Color.Red;
            }
        }