Esempio n. 1
0
        //=====================================================================================================================/
        //====================================================================================================================/

        //=====================BTNSTART=============================================================================
        private async void btnSart_ClickAsync(object sender, EventArgs e)
        {
            lblPatience.Visible = true;
            var sIpDestinataire = tbxIP1.Text + "." + tbxIP2.Text + "." + tbxIP3.Text + "." + tbxIP4.Text;

            if (sIpDestinataire == lblIPPersonnel.Text)
            {
                tbxIP4.BackColor = Color.Red;
            }
            if (tbxIP1.BackColor != Color.Red)
            {
                if (tbxIP2.BackColor != Color.Red)
                {
                    if (tbxIP3.BackColor != Color.Red)
                    {
                        if (tbxIP4.BackColor != Color.Red)
                        {
                            if (tbxIP4.BackColor != Color.PaleGreen)
                            {
                                lblPatience.Visible  = true;
                                lblEtatPing.Visible  = false;
                                lblNomPCDest.Visible = false;
                                lblEtatPing.Visible  = true;
                                var sNameDestinataire = Ip.GetHostName(sIpDestinataire);
                                var bResultPing       = await Task.Run(() => Ip.PingDest(sIpDestinataire));

                                if (bResultPing)
                                {
                                    lblEtatPing.Text      = @"Ping : OK";
                                    lblEtatPing.ForeColor = Color.Green;
                                    if (btnSart.Text == @"Check IP")
                                    {
                                        if (sNameDestinataire == "")
                                        {
                                            lblNomPCDest.Text      = "Name :" + "\r\n" + "Not found";
                                            lblNomPCDest.ForeColor = Color.Red;
                                            lblNomPCDest.Visible   = true;
                                            tbxIP4.BackColor       = Color.Red;
                                        }
                                        else
                                        {
                                            btnSart.Text           = "Start";
                                            lblNomPCDest.Visible   = true;
                                            lblNomPCDest.Text      = "Name :" + "\r\n" + sNameDestinataire;
                                            lblNomPCDest.ForeColor = Color.Black;
                                        }
                                    }
                                    else
                                    {
                                        //___________MinimiserLaFenetre________________
                                        this.Width                = 620;
                                        lblDescription.Visible    = false;
                                        btnSart.Visible           = false;
                                        lblDescription2.Visible   = true;
                                        lblIPDESTINATAIRE.Visible = false;
                                        pbxLogo1.Visible          = false;
                                        pbxLogoPetit.Visible      = true;
                                        lblIPPersonnel.Visible    = false;
                                        lblFixeCePC.Visible       = false;
                                        NomDestinataireToolStripMenuItem.Visible = true;
                                        tbxIP1.Visible = false;
                                        tbxIP2.Visible = false;
                                        tbxIP3.Visible = false;
                                        tbxIP4.Visible = false;
                                        NomDestinataireToolStripMenuItem.Text = "Recipient : " + sNameDestinataire;
                                        lblNomPCDest.Visible = false;
                                        iPPersonnelToolStripMenuItem.Text    = "My IP : " + lblIPPersonnel.Text;
                                        iPPersonnelToolStripMenuItem.Visible = true;
                                        //______________________________________________
                                        //FichierConfig---------------------------------
                                        if (XmlManipulation.GetValue("EnableLastIpConnexion") == "ON")
                                        {
                                            XmlManipulation.ModifyElementXml("LastIpConnexion", sIpDestinataire);
                                        }
                                        else
                                        {
                                            XmlManipulation.ModifyElementXml("LastIpConnexion", "");
                                        }
                                        _recipientIp = sIpDestinataire;
                                        try
                                        {
                                            _epLocal = new IPEndPoint(IPAddress.Parse(lblIPPersonnel.Text), 3056);//Use 3056 port
                                            _sck.Bind(_epLocal);

                                            _epRemote = new IPEndPoint(IPAddress.Parse(sIpDestinataire), 3056);
                                            _sck.Connect(_epRemote);

                                            var buffer = new byte[1500];
                                            _sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref _epRemote, MessageReceived, buffer);

                                            btnSart.Text     = @"Connected";
                                            btnSart.Enabled  = false;
                                            btnEnvoi.Enabled = true;
                                            tbxMessageEnvoit.Focus();
                                            EnvoiDuMessage("tuiFZCz56786casdcssdcvuivgboRTSDetre67Rz7463178", KMessage.Type.Init());
                                            if (XmlManipulation.GetValue("SaveDiscussion") == "ON")
                                            {
                                                var temp = ChatData.Import(_recipientIp);
                                                if (temp.Count() != 0)
                                                {
                                                    foreach (var element in temp)
                                                    {
                                                        lbxTchat.Items.Add(element);
                                                    }
                                                }
                                            }
                                        }
                                        catch
                                        {
                                            MessageBox.Show("An error occured. \r\nPlease restart Kubeah Chat" + "\r\n" + "\r\n", "An error occurred");
                                            Application.Exit();
                                        }
                                        //Send the key to the other client to connect
                                    }
                                }
                                else
                                {
                                    lblEtatPing.Text      = "Ping : Fail";
                                    lblEtatPing.ForeColor = Color.Red;
                                    lblNomPCDest.Visible  = false;
                                    tbxIP4.BackColor      = Color.Red;
                                }
                            }
                        }
                    }
                }
            }
            lblPatience.Visible = false;
        }