• ping指定地址


    最近用到相应功能,网上搜了下。

    感谢网友的分享,原文链接如下:http://blog.csdn.net/nanfeiyannan/article/details/6636280

    稍微更改了内容,代码如下:

    unit uPing;
    
    interface
    
    uses
      WinSock, Windows;
    
    type
      PIPOptionInformation = ^TIPOptionInformation;
    
      TIPOptionInformation = packed record
        TTL: Byte;
        TOS: Byte;
        Flags: Byte;
        OptionsSize: Byte;
        OptionsData: PChar;
      end;
    
      PIcmpEchoReply = ^TIcmpEchoReply;
    
      TIcmpEchoReply = packed record
        Address: DWORD;
        Status: DWORD;
        RTT: DWORD;
        DataSize: Word;
        Reserved: Word;
        Data: Pointer;
        Options: TIPOptionInformation;
      end;
    
      TIcmpCreateFile = function: THandle; stdcall;
      TIcmpCloseHandle = function(IcmpHandle: THandle): Boolean; stdcall;
      TIcmpSendEcho = function(IcmpHandle: THandle; DestinationAddress: DWORD;
        RequestData: Pointer; RequestSize: Word;
        RequestOptions: PIPOptionInformation; ReplyBuffer: Pointer;
        ReplySize: DWORD; Timeout: DWORD): DWORD; stdcall;
    
    function PingHost(HostIP: String): Boolean;
    
    implementation
    
    function PingHost(HostIP: String): Boolean;
    
    var
      hICMP: THandle;
      hICMPdll: THandle;
      IcmpCreateFile: TIcmpCreateFile;
      IcmpCloseHandle: TIcmpCloseHandle;
      IcmpSendEcho: TIcmpSendEcho;
      pIPE: PIcmpEchoReply; // ICMP   Echo   reply   buffer
      FIPAddress: DWORD;
      FSize: DWORD;
      FTimeOut: DWORD;
      BufferSize: DWORD;
      pReqData, pRevData: PAnsiChar;
      MyString: string;
    begin
      Result := False;
      hICMPdll := LoadLibrary('icmp.dll');
      if hICMPdll = 0 then
        exit;
      @IcmpCreateFile := GetProcAddress(hICMPdll, 'IcmpCreateFile');
      @IcmpCloseHandle := GetProcAddress(hICMPdll, 'IcmpCloseHandle');
      @IcmpSendEcho := GetProcAddress(hICMPdll, 'IcmpSendEcho');
      hICMP := IcmpCreateFile;
      if (hICMP = INVALID_HANDLE_VALUE) then
        exit;
      FIPAddress := inet_addr(PAnsiChar(HostIP));
      MyString := 'Hello,World'; // send   data   buffer
      pReqData := PAnsiChar(MyString);
      FSize := 40; // receive   data   buffer
      BufferSize := SizeOf(TIcmpEchoReply) + FSize;
      GetMem(pIPE, BufferSize);
      FillChar(pIPE^, SizeOf(pIPE^), 0);
      GetMem(pRevData, FSize);
      pIPE^.Data := pRevData;
      FTimeOut := 1000;
      try
        Result := IcmpSendEcho(hICMP, FIPAddress, pReqData, Length(MyString), nil,
          pIPE, BufferSize, FTimeOut) > 0;
      finally
        IcmpCloseHandle(hICMP);
        FreeLibrary(hICMPdll);
        FreeMem(pRevData);
        FreeMem(pIPE);
      end;
    end;
    
    end.
    

     现将代码放这,有空再吃透代码。

  • 相关阅读:
    synchronized 到底该不该用?
    线上Java程序占用 CPU 过高,请说一下排查方法?
    『JVM』我不想知道我是怎么来滴,我就想知道我是怎么没滴
    Golang 实现 Redis(8): TCC分布式事务
    Golang 实现 Redis(7): 集群与一致性 Hash
    Golang 实现 Redis(6): 实现 pipeline 模式的 redis 客户端
    LSM 树详解
    uni-app subNVue 原生子窗体简单使用案例(app端)
    日计不足涓滴成河-自定义响应结果格式化器
    RTT之软件包
  • 原文地址:https://www.cnblogs.com/BlackList-Sakura/p/5169636.html
Copyright © 2020-2023  润新知