Exemple #1
0
        public override string ToString()
        {
            string name = "Unknown/Generic PI";

            switch (BoardType)
            {
            case RaspberryPiBoards.Model_2:
                name = "Raspberry Pi 2";
                break;

            case RaspberryPiBoards.Model_A:
                name = "Raspberry Pi, Model A";
                break;

            case RaspberryPiBoards.Model_B:
                name = "Raspberry Pi, Model B";
                break;

            case RaspberryPiBoards.Model_APlus:
                name = "Raspberry Pi, Model A+";
                break;

            case RaspberryPiBoards.Model_BPlus:
                name = "Raspberry Pi, Model B+";
                break;

            case RaspberryPiBoards.Bannana:
                name = "Bannana Pi, original";
                break;
            }

            string ip = (IpAdress != null) ? IpAdress.ToString() : "(N/A)";

            return(string.Format("{0} @ {1}", name, ip));
        }
        /// <summary>
        /// Methode die in einem eigenen Thread läuft
        /// Wartet auf Anfrage aus dem Netzwerk
        /// Sendet bei erfolgreicher Anfrage die IPAdresse in das Netzwerk
        /// </summary>
        private void WaitForClientRequest_UDP()
        {
            const String seperator = "-------------------------------------------------------------------------";

            try
            {
                while (!CloseApplication)
                {
                    UdpClient = new UdpClient();
                    Byte[] myBuffer = new Byte[4096];

                    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 1336);
                    UdpClient.Client.Bind(remoteEndPoint);
                    ServerVM.WriteToLog($"UdpClient wurde geöffnet: Port: {1336}");
                    Console.WriteLine(DateTime.Now + " >>>>>>>>>>> " + "Waiting for BroadcastMessages..." + "<<<<<<<<<<");
                    ServerVM.WriteToLog(" >>>>>>>>>>> " + "Waiting for BroadcastMessages..." + "<<<<<<<<<<");
                    myBuffer = UdpClient.Receive(ref remoteEndPoint);
                    Console.WriteLine(Environment.NewLine + seperator);
                    Console.WriteLine(DateTime.Now + " Eine Anfrage kam von: " + remoteEndPoint);
                    ServerVM.WriteToLog(" Eine Anfrage kam von: " + remoteEndPoint);

                    ASCIIEncoding encoder        = new ASCIIEncoding();
                    String        messageFromUdp = encoder.GetString(myBuffer);

                    messageFromUdp.Replace("\0", "");
                    messageFromUdp.Trim();

                    Console.WriteLine(DateTime.Now + " Empfanges Schlüsselwort: " + messageFromUdp.Trim() + " von: " + IpAdress.ToString());
                    ServerVM.WriteToLog(" Empfanges Schlüsselwort: " + messageFromUdp.Trim() + " von: " + IpAdress.ToString());
                    Byte[] bufferToClient = new Byte[0];
                    if (messageFromUdp.Contains("<Server>")) //Server-Anfrage
                    {
                        Thread.Sleep(1000);
                        bufferToClient = encoder.GetBytes(IpAdress.ToString());
                        UdpClient.Send(bufferToClient, bufferToClient.Length, remoteEndPoint);
                        Console.WriteLine(DateTime.Now + " Gesendeter Broadcast: "
                                          + "\""
                                          + IpAdress.ToString()
                                          + "\" "
                                          + "an: "
                                          + IpAdress.ToString());
                        ServerVM.WriteToLog(" Gesendeter Broadcast: " + "\"" + IpAdress.ToString() + "\" " + "an: " + IpAdress.ToString());
                    }
                    else
                    {
                        Console.WriteLine(seperator);
                        Console.WriteLine(DateTime.Now + "Anfrage war ungültig");
                    }
                    Console.WriteLine(seperator + Environment.NewLine);
                    messageFromUdp = String.Empty;
                    UdpClient.Close();
                    ServerVM.WriteToLog("UdpClient wurde geschlossen");
                }
            }
            catch (Exception ex)
            {
                //Hier ging etwas schief :/
            }
        }