DELPHI里有没这样一个函数,可以返回在PING一个IP地址时的响应时间以及丢包数等。

解决方案 »

  1.   

    直接的函数没有
    去google搜索一下delphi,ping可以找到很多方法实现
      

  2.   

    indy中的IdIcmpClient控件Indy Demo 下面有个它的程序,效果不错。
      

  3.   

    使用过网络的用户都熟悉“ping”这个指令,它是一个dos下的可执行文件,一般用它来检查网络连接的好坏程度。其基本原理是利用tcp/ip协议包中icmp协议中的一个功能,即向所指定的计算机发送一个请求,收到请求的计算机返回一个应答,借此来判断该计算机是否在网上运行或者检查网络连接是否稳定可靠。在ping程序执行过程中,双方计算机所耗费的资源都很少,因此,它是一个非常实用的工具。   我们可以通过编程来实现“ping”操作,对其加以改进,使之具有windows的界面风格,显示比dos更加直观。   首先,对编程中需要的动态链接库作一简要说明:在windows的system目录下,你可以找到icmp.dll文件,该动态链接库提供了icmp协议的所有功能,我们的编程就建立在对该动态链接库的调用上。   icmp.dll文件内的调用函数说明如下:   1、icmpcreatefile   打开一个句柄,通过该句柄你可以发送icmp的请求回送报文。   2、icmpclosehandle   关闭你通过icmpcreatefile函数打开的句柄。   3、icmpsendecho   通过你打开的句柄发送icmp请求,在超时或应答报文接收后返回。其参数基本上和它的帧结构一致,可参看下面的程序部分,其具体含意你可以参看有关icmp协议的书籍。   初步了解了上述的三个函数后,我们就可以开始编程了。   首先,我们的程序运行后应该有如图1所示的基本功能。为此,我们可先在delphi的窗口中放入右上图中所示的控件,如按钮、编辑框和文本显示框等。   (g72.jpg)   例程运行示意图   然后,在程序的开始部分(formcreate)对winsocket进行初始化,其作用是申明使用的版本信息,同时调入icmp.dll库。   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;    tmyping = class(tform)    panel1: tpanel;    label1: tlabel;    pingedit: tedit;    exebtn: tbutton;    button2: tbutton;    button3: tbutton;    statusshow: tmemo;    procedure button3click(sender: tobject);    procedure formcreate(sender: tobject);    procedure exebtnclick(sender: tobject);    private    { private declarations }    hicmp: thandle;    icmpcreatefile : ticmpcreatefile;    icmpclosehandle: ticmpclosehandle;    icmpsendecho: ticmpsendecho;    public    { public declarations }   end;   procedure tmyping.formcreate(sender: tobject);   var    wsadata: twsadata;    hicmpdll: hmodule;   begin    wsastartup($101, wsadata);    // load the icmp.dll stuff    hicmpdll := loadlibrary('icmp.dll');    @icmpcreatefile := getprocaddress(hicmpdll, 'icmpcreatefile');    @icmpclosehandle := getprocaddress(hicmpdll, 'icmpclosehandle');    @icmpsendecho := getprocaddress(hicmpdll, 'icmpsendecho');    hicmp := icmpcreatefile;    statusshow.text := '';    statusshow.lines.add('目的ip地址 字节数 返回时间(毫秒)');   end;   接下来,就要进行如下所示的ping操作的实际编程过程了。   procedure tmyping.exebtnclick(sender: tobject);   var    ipopt:tipoptioninformation;// ip options for packet to send    fipaddress:dword;    preqdata,prevdata:pchar;    pipe:picmpechoreply;// icmp echo reply buffer    fsize: dword;    mystring:string;    ftimeout:dword;    buffersize:dword;   begin    if pingedit.text < >  '' then    begin    fipaddress := inet_addr(pchar(pingedit.text));    fsize := 40;    buffersize := sizeof(ticmpechoreply) + fsize;    getmem(prevdata,fsize);    getmem(pipe,buffersize);    fillchar(pipe^, sizeof(pipe^), 0);    pipe^.data := prevdata;    mystring := 'hello,world';    preqdata := pchar(mystring);    fillchar(ipopt, sizeof(ipopt), 0);    ipopt.ttl := 64;    ftimeout := 4000;    icmpsendecho(hicmp, fipaddress, preqdata, length(mystring), @ipopt, pipe, buffersize, ftimeout);    if preqdata^ = pipe^.options.optionsdata^ then    begin    statusshow.lines.add(pchar(pingedit.text) + ' ' +inttostr(pipe^.datasize) + ' ' +inttostr(pipe^.rtt));    end;    freemem(prevdata);    freemem(pipe);    end   end;