예제 #1
0
        static async Task PingAndUpdateAsync(string ip, PingResult pr)
        {
            using var ping = new System.Net.NetworkInformation.Ping();
            Interlocked.Increment(ref pr.IpsPinged);
            var reply = await ping.SendPingAsync(ip, 500);

            if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
            {
                pr.OnlineIps[reply.Address] = true;
            }
        }
예제 #2
0
        public static async Task <bool> PingAsync(string host)
        {
            try
            {
                var ping  = new System.Net.NetworkInformation.Ping();
                var reply = await ping.SendPingAsync(host, 1000);

                return(reply.Status == System.Net.NetworkInformation.IPStatus.Success);
            }
            catch { return(false); }
        }
예제 #3
0
        private async Task PingAndUpdateAsync(System.Net.NetworkInformation.Ping ping, string ip)
        {
            var reply = await ping.SendPingAsync(ip, timeout);

            if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
            {
                lock (lockObj)
                {
                    nFound++;
                }
            }
        }
예제 #4
0
        /// <summary>
        ///     测试 ICMP 延迟
        /// </summary>
        /// <param name="addr"></param>
        /// <returns></returns>
        private static int ICMPing(IPAddress addr)
        {
            using (var client = new System.Net.NetworkInformation.Ping())
            {
                var tk = client.SendPingAsync(addr);
                if (!tk.Wait(1000))
                {
                    return(999);
                }

                return(Convert.ToInt32(tk.Result.RoundtripTime));
            }
        }
    private static async Task <bool> PingAsync(string ip, int timeout)
    {
        try
        {
            var ping  = new System.Net.NetworkInformation.Ping();
            var reply = await ping.SendPingAsync(ip, timeout);

            return(reply.Status == System.Net.NetworkInformation.IPStatus.Success);
        }
        catch
        {
            return(false);
        }
    }
예제 #6
0
        public override Task <PingReplyWrapper> SendPingAsync(string hostnameOrAddress, TimeSpan timeout)
        {
            ensureNotDisposed();
            var _ping = new System.Net.NetworkInformation.Ping();

            return(_ping.SendPingAsync(hostnameOrAddress, (int)timeout.TotalMilliseconds).ContinueWith <PingReplyWrapper>(t =>
            {
                _ping.Dispose();
                return new PingReplyWrapper
                {
                    RoundtripTime = t.Result.RoundtripTime,
                    Status = t.Result.Status
                };
            }));
        }
예제 #7
0
        private static async Task PingAndUpdateAsync(System.Net.NetworkInformation.Ping ping, string ip, int timeout)
        {
            var reply = await ping.SendPingAsync(ip, timeout);

            //Console.WriteLine($"ip: {ip} - reply: {reply.Status}");

            if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
            {
                Console.WriteLine(ip);
                lock (lockObj)
                {
                    upCount++;
                }
            }
        }
예제 #8
0
        public static async ValueTask ContinuousPingAsync(IPAddress address, int timeout, int delayBetweenPings, IProgress <PingerResult> progress, CancellationToken cancellationToken)
        {
            try
            {
                var ping   = new System.Net.NetworkInformation.Ping();
                var result = new PingerResult(address);

                while (!cancellationToken.IsCancellationRequested)
                {
                    System.Net.NetworkInformation.PingReply res = null;
                    try
                    {
                        try
                        {
                            res = await ping.SendPingAsync(address, timeout).ConfigureAwait(false);

                            result.AddResult(res);
                            progress.Report(result);
                            await Task.Delay(delayBetweenPings, cancellationToken).ConfigureAwait(false);
                        }
                        catch (System.Net.NetworkInformation.PingException /*pex*/)
                        {
                            //var s = pex.ToString();
                            result.AddResult(res);
                            progress.Report(result);
                            await Task.Delay(delayBetweenPings, cancellationToken).ConfigureAwait(false);
                        }
                    }
                    catch (Exception /*ex*/)
                    {
                        //res = null;
                        //ex.ToString();
                        result.AddResult(null);
                        progress.Report(result);
                        await Task.Delay(delayBetweenPings, cancellationToken).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception /*ex*/)
            {
                //ex.ToString();
            }
        }
    public static void Main(string[] args)
    {
        PingReply pingResult = null;
        int       i          = 0;

        Task.Run(async() =>
        {
            var ii = 0;
            //no error handling, example only
            //no cancelling, example only
            var ping = new System.Net.NetworkInformation.Ping();
            while (true)
            {
                Console.WriteLine($"A: {ii} > {DateTime.Now.TimeOfDay}");
                var localPingResult = await ping.SendPingAsync("duckduckgo.com");
                Console.WriteLine($"A: {ii} < {DateTime.Now.TimeOfDay}, status: {localPingResult?.Status}");
                lock (_o)
                {
                    i          = ii;
                    pingResult = localPingResult;
                }

                await Task.Delay(1000);
                ii++;
            }
        });
        Task.Run(async() =>
        {
            //no error handling, example only
            while (true)
            {
                await Task.Delay(2000);
                lock (_o)
                {
                    Console.WriteLine($"B: Checking at {DateTime.Now.TimeOfDay}, status no {i}: {pingResult?.Status}");
                }
            }
        });
        Console.WriteLine("This is the end of Main()");
        Console.ReadLine();
    }
예제 #10
0
        public static async Task <bool> Ping(string url, int timeout = 1000)
        {
            using (var ping = new System.Net.NetworkInformation.Ping())
            {
                for (int times = 0; times < 3; times++)
                {
                    try
                    {
                        var reply = await ping.SendPingAsync(url, timeout);

                        if (reply?.Status == System.Net.NetworkInformation.IPStatus.Success)
                        {
                            return(true);
                        }
                    }
                    catch
                    {
                        //TODO
                    }
                }
            }
            return(false);
        }
예제 #11
0
        private async Task PingAndUpdateAsync(System.Net.NetworkInformation.Ping ping, string ip)
        {
            var reply = await ping.SendPingAsync(ip, timeout);

            if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
            {
                if (MainWindow.GetMachineNameFromIPAddress(reply.Address.ToString()).Length > 1)
                {
                    UsersModel x = new UsersModel();
                    x.pc_name = MainWindow.GetMachineNameFromIPAddress(reply.Address.ToString());
                    x.ip      = reply.Address.ToString();

                    UM.Add(x);
                    Console.WriteLine(x.ip + " " + x.pc_name);
                    try
                    {
                        SqliteDataAccess.AddUser(x.pc_name, x.ip);
                    }
                    catch (System.Data.SQLite.SQLiteException)
                    {
                        SqliteDataAccess.UpdateUserIp(x.pc_name, x.ip);
                    }
                }

                lock (lockObj)
                {
                    nFound++;
                    ping.Dispose();
                }
                ping.Dispose();
            }
            if (reply.Status != System.Net.NetworkInformation.IPStatus.Success)
            {
                ping.Dispose();
            }
        }