• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi通过ICMP检测与远程主机连接

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
{ ping IP 地址(返回false or true) 2015-03-23}
function PingHost(HostIP: String): Boolean;
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;
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:PChar;
  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(PChar(HostIP));
  MyString := 'Hello,World';                                 //send   data   buffer
  pReqData:=PChar(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:=500;
  try
    Result:=IcmpSendEcho(hICMP, FIPAddress,pReqData,
    Length(MyString),nil,pIPE,BufferSize,FTimeOut)>0;
  finally
    IcmpCloseHandle(hICMP);
    FreeLibrary(hICMPdll);
    FreeMem(pRevData);
    FreeMem(pIPE);
  end;
end;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[MATLAB] 利用遗传算法函数求目标函数的最优解发布时间:2022-07-18
下一篇:
Matlab应用笔记--遗传算法发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap