Exemple #1
0
        private async Task <(int port, PortStatus portStatus, Protocol protocol)> CheckPort(IPAddress ipAddr,
                                                                                            int port, int timeout = 3000)
        {
            while (true)
            {
                try
                {
                    using var udpClient = new UdpClient();
                    udpClient.Connect(ipAddr, port);

                    for (var i = 0; i < 2; i++)
                    {
                        try
                        {
                            await udpClient.SendAsync(pakeges[i], pakeges[i].Length);

                            var count = await udpClient.ReceiveWithTimeoutAsync(timeout);

                            if (count.Status == TaskStatus.RanToCompletion)
                            {
                                foreach (var v in count.Result.Buffer)
                                {
                                    Console.Write(v);
                                }
                                Console.WriteLine();
                                Console.WriteLine(port);
                                foreach (var v in pakeges[i])
                                {
                                    Console.Write(v);
                                }

                                switch (i)
                                {
                                case 0:
                                    return(port, PortStatus.OPEN, Protocol.DNS);

                                case 1:
                                    return(port, PortStatus.OPEN, Protocol.NTP);
                                }
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                    }

                    return(port, PortStatus.CLOSED, Protocol.UNDEFINED);
                }
                catch
                {
                    // ignored
                }
            }
        }