예제 #1
0
        public static PingResult SendIcmp(IPAddress address, int timeout = 5000)
        {
            Ping ping   = new Ping();
            var  reply  = ping.Send(address, timeout);
            var  result = PingResult.FromPingReply(reply);

            if (result.Address == null || result.Address.Equals(IPAddress.Any))
            {
                result.Address = address;
            }
            return(result);
        }
예제 #2
0
        public static Task <PingResult> SendIcmpAsync(IPAddress address, int timeout = 5000)
        {
            var  tcs  = new TaskCompletionSource <PingResult>();
            Ping ping = new Ping();

            ping.PingCompleted += (obj, sender) => {
                var ad     = sender.UserState as IPAddress;
                var result = PingResult.FromPingReply(sender.Reply);
                if (result.Address == null || result.Address.Equals(IPAddress.Any))
                {
                    result.Address = ad;
                }
                tcs.SetResult(result);
            };
            ping.SendAsync(address, timeout, address);
            return(tcs.Task);
        }