首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > PB >

一段Ping测试的代码,不知具体意义

2013-01-05 
一段Ping测试的代码,不知具体意思本帖最后由 nettt 于 2010-07-20 02:03:56 编辑//以下是一段PIng测试的代

一段Ping测试的代码,不知具体意思
本帖最后由 nettt 于 2010-07-20 02:03:56 编辑 //以下是一段PIng测试的代码,
Long ll_hICMP
Long ll_RTN
String ls_Message
Ulong aul_IPAddr

IP_OPTION_INFORMATION lstr_IPInfo
ICMP_ECHO_REPLY lstr_ICMPReplay

//打开ICMP句柄
ll_hICMP = IcmpCreateFile()

If ll_hICMP <> 0 Then

ls_Result = ""
ls_Message = "   "   //???????? 这个有什么用

aul_IPAddr = of_ChangeIPv4ToLong(as_IPaddress) //192.169.5.252

//发送ICMP请求
ll_RTN = IcmpSendEcho (ll_hICMP, aul_IPAddr, ls_Message, Len(ls_Message), 0, lstr_ICMPReplay, 282, 200)
//???????关键是这里不懂,这几个参数都是什么作用,这个Len(ls_Message) 有什么用处,还有能不能用这段代码实现 Ping 2000 的包。
//判断回应状态
If ll_RTN > 0 Then
ls_Result = "Ping 成功,状态值为:" + String(lstr_ICMPReplay.Status) 
Else
ls_Result = "Ping 失败,错误号为:" + String(WSAGetLastError())
End If

//关闭ICMP句柄
IcmpCloseHandle(ll_hICMP)

Return ll_RTN
Else
ls_Result = "无法打开ICMP句柄!"
Return -1;
End If
[解决办法]
同有仔细研究过

函数详细说明:http://msdn.microsoft.com/en-us/library/Aa366051

IcmpSendEcho2的原型如下: 
DWORD   IcmpSendEcho2( 
    HANDLE   IcmpHandle, 
    HANDLE   Event, 
    FARPROC   ApcRoutine, 
    PVOID   ApcContext, 
    IPAddr   DestinationAddress, 
    LPVOID   RequestData, 
    WORD   RequestSize, 
    PIP_OPTION_INFORMATION   RequestOptions, 
    LPVOID   ReplyBuffer, 
    DWORD   ReplySize, 
    DWORD   Timeout 
); 

其中3个参数为结构体: 

1   DestinationAddress   
[in]   Destination   of   the   echo   request,   in   the   form   of   an   IPAddr   structure.   
typedef   struct   { 
    union   { 
        struct   { 
            u_char   s_b1,s_b2,s_b3,s_b4;       
        }   S_un_b; 
        struct   {   
          u_short   s_w1,s_w2; 
        }   S_un_w; 
        u_long   S_addr; 
        }   S_un; 
  }   IPAddr;     
这个不用理它,不管它怎么折腾,固定占四个字节,定义为long 




2   RequestOptions   
[in]   Pointer   to   the   IP   header   options   for   the   request,   in   the   form   of   an   IP_OPTION_INFORMATION   structure.   
这个定义为: 
Type   IP_OPTION_INFORMATION   
    Ttl                   As   Byte   'Time   To   Live 
    Tos                   As   Byte   '   Type   Of   Service 
    Flags               As   Byte   'IP   header   flags 
    OptionsSize   As   Byte   '   Size   in   bytes   of   options   data 
    OptionsData   As   Long   '   Pointer   to   options   data 
End   Type 

3   ReplyBuffer   
[out]   Buffer   to   hold   any   replies   to   the   request.   Upon   return,   the   buffer   contains   an   array   of   ICMP_ECHO_REPLY   structures   followed   by   options   and   data.   The   buffer   must   be   large   enough   to   hold   at   least   one   ICMP_ECHO_REPLY   structure.   It   must   be   large   enough   to   also   hold   9   more   bytes   of   data.   
这个定义为: 
Type   ICMP_ECHO_REPLY 
    Address               As   Long                                     'Replying   address 
    Status                 As   Long                                     'Reply   IP_STATUS 
    RoundTripTime   As   Long                                     'RTT   in   milliseconds 


    DataSize             As   Integer                               'Reply   data   size   in   bytes 
    Reserved             As   Integer                               'Reserved   for   system   use 
    DataPointer       As   Long                                     'Pointer   to   the   reply   data 
    Options               As   IP_OPTION_INFORMATION   'Reply   options 
    Data                     As   String   *   MAX_PATH           'Reply   data   pointed   to   by   "DataPointer " 
End   Type 

其中的Data为缓冲区,要保证它足够大 

最后,IcmpSendEcho2的声明为: 
Private   Declare   Function   IcmpSendEcho2   Lib   "icmp.dll "   _ 
    (ByVal   IcmpHandle   As   Long,   _ 
      ByVal   Events   As   Long,   _ 
      ByVal   ApcRoutine   As   Long,   _ 
      ByVal   ApcContext   As   Long,   _ 
      ByVal   DestinationAddress   As   Long,   _ 
      ByVal   RequestData   As   String,   _ 
      ByVal   RequestSize   As   Integer,   _ 
      RequestOptions   As   IP_OPTION_INFORMATION,   _ 
      ReplyBuffer   As   ICMP_ECHO_REPLY,   _ 
      ByVal   ReplySize   As   Long,   _ 
      ByVal   Timeout   As   Long)   As   Long 

热点排行