| 发表于:2007-03-27 11:57:032楼 得分:0 |
public function getstatuscode(status as long) as string dim msg as string select case status case ip_success: msg = "ip success " case ip_buf_too_small: msg = "ip buf too_small " case ip_dest_net_unreachable: msg = "ip dest net unreachable " case ip_dest_host_unreachable: msg = "ip dest host unreachable " case ip_dest_prot_unreachable: msg = "ip dest prot unreachable " case ip_dest_port_unreachable: msg = "ip dest port unreachable " case ip_no_resources: msg = "ip no resources " case ip_bad_option: msg = "ip bad option " case ip_hw_error: msg = "ip hw_error " case ip_packet_too_big: msg = "ip packet too_big " case ip_req_timed_out: msg = "ip req timed out " case ip_bad_req: msg = "ip bad req " case ip_bad_route: msg = "ip bad route " case ip_ttl_expired_transit: msg = "ip ttl expired transit " case ip_ttl_expired_reassem: msg = "ip ttl expired reassem " case ip_param_problem: msg = "ip param_problem " case ip_source_quench: msg = "ip source quench " case ip_option_too_big: msg = "ip option too_big " case ip_bad_destination: msg = "ip bad destination " case ip_addr_deleted: msg = "ip addr deleted " case ip_spec_mtu_change: msg = "ip spec mtu change " case ip_mtu_change: msg = "ip mtu_change " case ip_unload: msg = "ip unload " case ip_addr_added: msg = "ip addr added " case ip_general_failure: msg = "ip general failure " case ip_pending: msg = "ip pending " case ping_timeout: msg = "ping timeout " case else: msg = "unknown msg returned " end select getstatuscode = cstr(status) & " [ " & msg & " ] " end function public function hibyte(byval wparam as integer) hibyte = wparam \ &h1 and &hff& end function public function lobyte(byval wparam as integer) lobyte = wparam and &hff& end function public function ping(szaddress as string, echo as icmp_echo_reply) as long dim hport as long dim dwaddress as long dim sdatatosend as string dim iopt as long sdatatosend = "echo this " dwaddress = addressstringtolong(szaddress) hport = icmpcreatefile() if icmpsendecho(hport, dwaddress, sdatatosend, len(sdatatosend), 0, echo, len(echo), ping_timeout) then 'the ping succeeded, '.status will be 0 '.roundtriptime is the time in ms for the ping to complete, '.data is the data returned (null terminated) '.address is the ip address that actually replied '.datasize is the size of the string in .data ping = echo.roundtriptime else ping = echo.status * -1 end if call icmpclosehandle(hport) end function function addressstringtolong(byval tmp as string) as long dim i as integer dim parts(1 to 4) as string i = 0 'we have to extract each part of the '123.456.789.123 string, delimited by 'a period while instr(tmp, ". ") > 0 i = i + 1 parts(i) = mid(tmp, 1, instr(tmp, ". ") - 1) tmp = mid(tmp, instr(tmp, ". ") + 1) wend i = i + 1 parts(i) = tmp if i <> 4 then addressstringtolong = 0 exit function end if 'build the long value out of the 'hex of the extracted strings addressstringtolong = val( "&h " & right( "00 " & hex(parts(4)), 2) & _ right( "00 " & hex(parts(3)), 2) & _ right( "00 " & hex(parts(2)), 2) & _ right( "00 " & hex(parts(1)), 2)) end function public function socketscleanup() as boolean dim x as long x = wsacleanup() if x <> 0 then msgbox "windows sockets error " & trim$(str$(x)) & " occurred in cleanup. ", vbexclamation socketscleanup = false else socketscleanup = true end if end function public function socketsinitialize() as boolean dim wsad as wsadata dim x as integer dim szlobyte as string, szhibyte as string, szbuf as string x = wsastartup(ws_version_reqd, wsad) if x <> 0 then msgbox "windows sockets for 32 bit windows " & "environments is not successfully responding. " socketsinitialize = false exit function end if if lobyte(wsad.wversion) < ws_version_major or (lobyte(wsad.wversion) = ws_version_major and hibyte(wsad.wversion) < ws_version_minor) then szhibyte = trim$(str$(hibyte(wsad.wversion))) szlobyte = trim$(str$(lobyte(wsad.wversion))) szbuf = "windows sockets version " & szlobyte & ". " & szhibyte szbuf = szbuf & " is not supported by windows " & "sockets for 32 bit windows environments. " msgbox szbuf, vbexclamation socketsinitialize = false exit function end if if wsad.wmaxsockets < min_sockets_reqd then szbuf = "this application requires a minimum of " & trim$(str$(min_sockets_reqd)) & " supported sockets. " msgbox szbuf, vbexclamation socketsinitialize = false exit function end if socketsinitialize = true end function 在form中添加一个命令按钮command1,一个文本框text2,创建一个textbox数组(text1(0)到text1(5))。在窗体中写入如下代码: private sub command1_click() dim echo as icmp_echo_reply dim pos as integer call ping(text2.text, echo) text1(0) = getstatuscode(echo.status) text1(1) = echo.address text1(2) = echo.roundtriptime & " ms " text1(3) = echo.datasize & " bytes " if left$(echo.data, 1) <> chr$(0) then pos = instr(echo.data, chr$(0)) text1(4) = left$(echo.data, pos - 1) end if text1(5) = echo.datapointer end sub | | |
|