コード例 #1
0
        static private void GlobalChatReceive()
        {
            Log.LogApplication.WriteLog($"[UDP Reader Worker] Старт потока глобального чата на порту {Config.GlobalChatUdpPort}");

            /*=========*/
            MyUdpClient1 = new UdpClient(Config.GlobalChatUdpPort);
            MyUdpClient1.JoinMulticastGroup(MulticastGroup, 20);
            IPEndPoint remoteIp     = null;
            string     localAddress = LocalIp;

            try
            {
                while (true)
                {
                    byte[] data = MyUdpClient1.Receive(ref remoteIp); // получаем данные

                    if (remoteIp.Address.ToString().Equals(localAddress))
                    {
                        continue;
                    }

                    string message = Encoding.Unicode.GetString(data);
                    if (message.Length <= 1)
                    {
                        continue;
                    }

                    if (ChatForm == null || !ChatForm.ShownForm)
                    {
                        LogApplication.WriteLog("[GlobalMessage] Форма не открыта, пропуск"); continue;
                    }

                    LogApplication.WriteLog($"[GlobalMessage] размер буффера {message.Length}, буффер в строку -> {message}");

                    ChatForm.Invoke((MethodInvoker) delegate
                    {
                        ChatForm.chatTextBox.AppendText(LocalMachines.GetNicknameByIP(IPAddress.Parse(remoteIp.ToString().Substring(0, remoteIp.ToString().IndexOf(':')))) + ": " + message);
                    });

                    Thread.Sleep(2);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                MyUdpClient.Close();
            }
            /*=========*/
        }
コード例 #2
0
        static public void CheckMachines()
        {
            LogApplication.WriteLog("[PingCheck] работает, проверка существования клиентов в сети каждые 5 секунд");
            Ping ping = new Ping();

            while (true)
            {
                for (int a = 0; a < LocalMachines.ListLocalMachines.Count; a++)
                {
                    try
                    {
                        PingReply reply = ping.Send(LocalMachines.ListLocalMachines[a].RemoteIp);
                        if (reply.Status != IPStatus.Success)
                        {
                            bool result = false;

                            for (int aa = 0; aa < 3; aa++)
                            {
                                reply = ping.Send(LocalMachines.ListLocalMachines[a].RemoteIp);
                                if (reply.Status == IPStatus.Success)
                                {
                                    result = true;
                                    break;
                                }
                            }

                            if (!result)
                            {
                                LogApplication.WriteLog("[PingCheck] Обнаружена машина, которая недоступна, удаление");
                                LocalMachines.RemoveMachine(LocalMachines.ListLocalMachines[a].RemoteIp);
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogApplication.WriteLog($"[PingCheck] Исключение \n{ex.Message}\nSTACK\n{ex.StackTrace}\n[PingCheck] Удаляю машину {LocalMachines.ListLocalMachines[a].ComputerNickname}:{LocalMachines.ListLocalMachines[a].RemoteIp.ToString()}");
                    }
                }

                Thread.Sleep(TimeSpan.FromSeconds(5));
            }
        }
コード例 #3
0
        static public void Detect()
        {
            LogApplication.WriteLog("[Detect] Запущен");
            IPEndPoint remoteIp     = null;
            string     localAddress = LocalIp;

            try
            {
                while (true)
                {
                    byte[] data = MyUdpClient.Receive(ref remoteIp); // получаем данные

                    {
                        bool local = false;
                        foreach (string bufff in ListIp)
                        {
                            if (remoteIp.Address.ToString().Equals(bufff))
                            {
                                local = true;
                                break;
                            }
                        }

                        if (local)
                        {
                            continue;
                        }
                    }

                    string message = Encoding.Unicode.GetString(data);
                    if (message.Length <= 1)
                    {
                        continue;
                    }

                    if (LocalMachines.Add(IPAddress.Parse(remoteIp.ToString().Substring(0, remoteIp.ToString().IndexOf(':'))), message))
                    {
                        GlavnForm.Invoke((MethodInvoker) delegate
                        {
                            new PopupNotifier()
                            {
                                TitleText   = "FileExchange",
                                ContentText = $"Новый компьютер в локальной сети {remoteIp.ToString().Substring(0, remoteIp.ToString().IndexOf(':'))}\n Никнейм {message}"
                            }.Popup();
                        });
                    }

                    Thread.Sleep(2);
                }
            }
            catch (Exception ex)
            {
                LogApplication.WriteLog($"DETECTOR    ИСКЛЮЧЕНИЕ IP:{remoteIp.ToString()}\n\nMESSAGE\n{ex.Message}\n\nSTACK\n" + ex.StackTrace + "\n\n\n");
            }
            finally
            {
                MyUdpClient.Close();
            }

            LogApplication.WriteLog("[Detector] Конец работы детектора");
        }