Exemplo n.º 1
0
            private static void Close()
            {
                if (listeningSocket != null)
                {
                    listeningSocket.Shutdown(SocketShutdown.Both);
                    listeningSocket.Close();
                    listeningSocket = null;
                }
                SecondWindow secondWindow = new SecondWindow();

                secondWindow.ChatText_Box.Text += "Server stänge sig själv!";
            }
Exemplo n.º 2
0
            public static void Listen()
            {
                try
                {
                    IPEndPoint localIP = new IPEndPoint(IPAddress.Parse("0.0.0.0"), localPort);
                    listeningSocket.Bind(localIP);

                    while (true)
                    {
                        StringBuilder builder  = new StringBuilder();
                        int           bytes    = 0;
                        byte[]        data     = new byte[256];
                        EndPoint      remoteIp = new IPEndPoint(IPAddress.Any, 0);
                        do
                        {
                            bytes = listeningSocket.ReceiveFrom(data, ref remoteIp);
                            builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                        }while (listeningSocket.Available > 0);
                        IPEndPoint remoteFullIp = remoteIp as IPEndPoint;

                        SecondWindow secondWindow = new SecondWindow();
                        string       UIM          = secondWindow.SendText_Box.Text;

                        UIM += builder.ToString() + "\r\n";
                        bool addClient = true;
                        for (int i = 0; i < clients.Count; i++)
                        {
                            if (clients[i].Address.ToString() == remoteFullIp.Address.ToString())
                            {
                                addClient = false;
                            }
                        }
                        if (addClient == true)
                        {
                            clients.Add(remoteFullIp);
                        }
                        BroadcastMessage(builder.ToString(), remoteFullIp.Address.ToString());
                    }
                }
                catch (Exception)
                {
                    SecondWindow secondWindow = new SecondWindow();
                    secondWindow.ChatText_Box.Text += secondWindow.TTS;
                }
                finally
                {
                    Close();
                }
            }
Exemplo n.º 3
0
        public void Host_button_Click(object sender, RoutedEventArgs e)
        {
            SaveBox.Text = "host";

            if (port_connect.Text.Length == 0)
            {
                MessageBoxResult result = MessageBox.Show("Du MÅSTE fulla in 'Port' ryta!",
                                                          "Fel");
            }
            else
            {
                ListenPort = Convert.ToInt32(port_connect.Text);
                if (ListenPort < 0)
                {
                    MessageBoxResult result = MessageBox.Show("Fel port! Port måste vara 0 eller mer!",
                                                              "Fel");
                }
                else
                {
                    if (Nickname_box.Text.Length != 0)
                    {
                        if (Ip_connect.Text.Length != 0)
                        {
                            if (port_connect.Text.Length != 0)
                            {
                                SecondWindow popup = new SecondWindow();
                                Close();
                                popup.ShowDialog();
                            }
                            else
                            {
                                MessageBoxResult result = MessageBox.Show("Du MÅSTE fulla in 'Port' ryta!",
                                                                          "Fel");
                            }
                        }
                        else
                        {
                            MessageBoxResult result = MessageBox.Show("Du MÅSTE fulla in 'ip address' ryta!");
                        }
                    }
                    else
                    {
                        MessageBoxResult result = MessageBox.Show("Du måste fulla in 'nickname' ryta!",
                                                                  "Fel");
                    }
                }
            }
        }
Exemplo n.º 4
0
            public static void Startserver(SecondWindow secondWindow)
            {
                try
                {
                    secondWindow.ChatText_Box.Text += "Server startas";

                    listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    Task listeningTask = new Task(Listen);
                    listeningTask.Start();
                    listeningTask.Wait();
                }
                catch (Exception)
                {
                    secondWindow.ChatText_Box.Text += secondWindow.TTS;
                }
                finally
                {
                    Close();
                }
            }
Exemplo n.º 5
0
            private static void Listen()
            {
                try
                {
                    IPEndPoint localIP = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
                    listeningSocket.Bind(localIP);

                    while (true)
                    {
                        StringBuilder builder = new StringBuilder();
                        int           bytes   = 0;
                        byte[]        data    = new byte[256];

                        EndPoint remoteIp = new IPEndPoint(IPAddress.Any, 0);

                        do
                        {
                            bytes = listeningSocket.ReceiveFrom(data, ref remoteIp);
                            builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                        }while (listeningSocket.Available > 0);

                        IPEndPoint remoteFullIp = remoteIp as IPEndPoint;

                        Console.WriteLine("{0}:{1} - {2}", remoteFullIp.Address.ToString(), remoteFullIp.Port, builder.ToString());
                    }
                }
                catch (Exception)
                {
                    SecondWindow secondWindow = new SecondWindow();
                    secondWindow.ChatText_Box.Text += secondWindow.TTS;
                }
                finally
                {
                    Close();
                }
            }