What's the best way to ping many network devices in parallel?

后端 未结 6 2242
鱼传尺愫
鱼传尺愫 2021-02-09 09:36

I poll a lot of devices in network (more than 300) by iterative ping.

The program polls the devices sequentially, so it\'s slow. I\'d like to enhance the speed of pollin

6条回答
  •  没有蜡笔的小新
    2021-02-09 10:31

    Direct ICMP access is deprecated on windows. Direct access to the ICMP protocol on Windows is controlled. Due to malicious use of ICMP/ping/traceroute style raw sockets, I believe that on some versions of Windows you will need to use Windows own api. Windows XP, Vista, and Windows 7, in particular, don't let user programs access raw sockets.

    I have used the canned-functionality in ICMP.dll, which is what some Delphi ping components do, but a comment below alerted me to the fact that this is considered "using an undocumented API interface".

    Here's a sample of the main delphi ping component call itself:

    function TICMP.ping: pIcmpEchoReply;
    {var  }
    begin
      // Get/Set address to ping
      if ResolveAddress = True then begin
        // Send packet and block till timeout or response
        _NPkts := _IcmpSendEcho(_hICMP, _Address,
                                _pEchoRequestData, _EchoRequestSize,
                                @_IPOptions,
                                _pIPEchoReply, _EchoReplySize,
                               _TimeOut);
        if _NPkts = 0 then begin
          result := nil;
          status := CICMP_NO_RESPONSE;
        end else begin
          result := _pIPEchoReply;
        end;
      end else begin
        status := CICMP_RESOLVE_ERROR;
        result := nil;
      end;
    end;
    

    I believe that most modern Ping component implementations are going to be based on a similar bit of code to the one above, and I have used it to run this ping operation in a background thread, without any probems. (Demo program included in link below).

    Full sample source code for the ICMP.DLL based demo is here.

    UPDATE A more modern IPHLPAPI.DLL sample is found at About.com here.

提交回复
热议问题