Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public static PingResult FromPingReply(PingReply reply)
        {
            if (reply == null)
            {
                return(new PingResult()
                {
                    Status = IPStatus.Unknown,
                });
            }

            PingResult result = new PingResult()
            {
                Address       = reply.Address,
                Buffer        = reply.Buffer,
                Options       = reply.Options,
                RoundtripTime = reply.RoundtripTime,
                Status        = reply.Status,
            };

            return(result);
        }
Ejemplo n.º 4
0
        public static PingResult SendUdp(IPAddress address, int port, int timeout = 5000)
        {
            UdpClient client = new UdpClient();

            client.Client.ReceiveTimeout = timeout;

            IPEndPoint remoteEndPoint = new IPEndPoint(address, port);

            byte[] data = BitConverter.GetBytes(DateTime.Now.Ticks);

            client.Send(data, data.Length, remoteEndPoint);
            PingResult result = new PingResult()
            {
                Address = address,
                Status  = IPStatus.TimedOut,
            };

            try {
                var response = client.Receive(ref remoteEndPoint);
                if (response != null)
                {
                    var ticks         = BitConverter.ToInt64(response, 0);
                    var roundtripTime = (long)TimeSpan.FromTicks(DateTime.Now.Ticks - ticks).TotalMilliseconds;

                    result = new PingResult()
                    {
                        Address       = address,
                        Buffer        = response,
                        RoundtripTime = roundtripTime,
                        Status        = IPStatus.Success,
                    };
                    return(result);
                }
            } catch (Exception ex) {
            }
            return(result);
        }