Beispiel #1
0
        private void connectButton_Click(object sender, System.EventArgs e)
        {
            try
            {
                ipTB.Text   = ipTB.Text.TrimEnd(' ');
                portTB.Text = portTB.Text.TrimEnd(' ');
                var ipChecker =
                    new Regex(
                        @"((?<=\s)|^)((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\s|$)");
                if (!ipChecker.IsMatch(ipTB.Text))
                {
                    throw new ArgumentException();
                }

                if (!int.TryParse(portTB.Text, out var port) && (port <= 0 || port > 65535))
                {
                    throw new ArgumentException();
                }

                _parrent.Connect(nameBox.Text, ipTB.Text, portTB.Text);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Wrong IP or port format.");
                return;
            }
            catch (EndpointNotFoundException)
            {
                MessageBox.Show("Wrong IP or port. Unable to connect.");
                return;
            }
            catch (Exception)
            {
                MessageBox.Show("Something wrong.");
                _parrent.Close();
                return;
            }

            _parrent.Show();

            Hide();
        }
Beispiel #2
0
 public void Disconnect()
 {
     if (stream != null)
     {
         stream.Close();
     }
     if (client != null)
     {
         client.Close();
     }
     Console.WriteLine(client.Connected);
     chat.Dispatcher.BeginInvoke(
         (Action)(() =>
     {
         chat.Close();
     }));
 }