How to ping faster when I reach unreachable IP?

前端 未结 5 1389
抹茶落季
抹茶落季 2020-12-16 22:15

I have an app which pings IP or IP range. The problem is that when hosts are closed it takes longer to ping than they are open. When host is closed the time to ping is about

相关标签:
5条回答
  • 2020-12-16 22:54

    Not sure if this is any help (see final post on the thread), it seems an almost identical problem. What you're butting up against there is the protocol stack's timeout. You can get around it if you use socket to connect as you'll have more control.

    0 讨论(0)
  • 2020-12-16 22:58

    I created a live host scanner not too long ago. It uses ARP to check if a computer is online. An ARP request is much faster than if you'd ping a host. Here's the code I used to check if a Host is available:

    //You'll need this pinvoke signature as it is not part of the .Net framework
    [DllImport("iphlpapi.dll", ExactSpelling = true)]
    public static extern int SendARP(int DestIP, int SrcIP, 
                                     byte[] pMacAddr, ref uint PhyAddrLen);
    
    //These vars are needed, if the the request was a success 
    //the MAC address of the host is returned in macAddr
    private byte[] macAddr = new byte[6];
    private uint macAddrLen;
    
    //Here you can put the IP that should be checked
    private IPAddress Destination = IPAddress.Parse("127.0.0.1");
    
    //Send Request and check if the host is there
    if (SendARP((int)Destination.Address, 0, macAddr, ref macAddrLen) == 0)
    {
        //SUCCESS! Igor it's alive!
    }
    

    If you're interested Nmap also uses this technique to scan for available hosts.

    ARP scan puts Nmap and its optimized algorithms in charge of ARP requests. And if it gets a response back, Nmap doesn't even need to worry about the IP-based ping packets since it already knows the host is up. This makes ARP scan much faster and more reliable than IP-based scans. So it is done by default when scanning ethernet hosts that Nmap detects are on a local ethernet network. Even if different ping types (such as -PE or -PS) are specified, Nmap uses ARP instead for any of the targets which are on the same LAN.

    EDIT:

    This only works within the current subnet! As long as there is no router between the requesting machine and the target it should work fine.

    ARP is a non-routable protocol, and can therefore only be used between systems on the same Ethernet network. [...] arp-scan can be used to discover IP hosts on the local network. It can discover all hosts, including those that block all IP traffic such as firewalls and systems with ingress filters. - Excerpt from NTA-Monitor wiki

    For more information on the SendARP function you can check the pinvoke.net documentation.

    0 讨论(0)
  • 2020-12-16 23:07

    You need to redesign your application to use multithreading -> tasks. Issue a task for each ping, and when you receive a response from a given host fire an event and update the UI. Changing socket timeout will only help you to reduce the timeout from outrageous to insufferable.

    0 讨论(0)
  • 2020-12-16 23:13

    You shouldn't reduce the timeout. Try to send multiple pings at once async.

    var ping = new Ping();
    ping.PingCompleted += (sender, eventArgs) =>
    {
        // eventArgs.Reply.Address
        // eventArgs.Reply.Status
    };
    ping.SendAsync(ip, etc.);
    
    0 讨论(0)
  • 2020-12-16 23:16

    Your address is a string. Thus it will go via DNS first to see if this is possibly a hostname (even if it is an IP address).

    I suggest you use the overload taking an IPAddress instead.

    0 讨论(0)
提交回复
热议问题