Source code for ping using VB

Source: Internet
Author: User

Option explicit
 
Public const ip_status_base= 11000
Public const ip_success = 0
Public const ip_buf_too_small = (1, 11000 + 1)
Public const ip_dest_net_unreachable = (11000 + 2)
Public const ip_dest_host_unreachable = (11000 + 3)
Public const ip_dest_prot_unreachable = (11000 + 4)
Public const ip_dest_port_unreachable = (11000 + 5)
Public const ip_no_resources = (11000 + 6)
Public const ip_bad_option = (11000 + 7)
Public const ip_hw_error = (11000 + 8)
Public const ip_packet_too_big = (11000 + 9)
Public const ip_req_timed_out = (11000 + 10)
Public const ip_bad_req = (11000 + 11)
Public const ip_bad_route = (11000 + 12)
Public const ip_ttl_expired_transit = (11000 + 13)
Public const ip_ttl_expired_reassem = (11000 + 14)
Public const ip_param_problem = (11000 + 15)
Public const ip_source_quench = (11000 + 16)
Public const ip_option_too_big = (11000 + 17)
Public const ip_bad_destination = (11000 + 18)
Public const ip_addr_deleted = (11000 + 19)
Public const ip_spec_mtu_change = (11000 + 20)
Public const ip_mtu_change = (11000 + 21)
Public const ip_unload = (11000 + 22)
Public const ip_addr_added = (11000 + 23)
Public const ip_general_failure = (11000 + 50)
Public const max_ip_status = 11000 + 50
Public const ip_pending = (11000 + 255)
Public const (ping_timeout = 200)
Public const ws_version_reqd = & H101
Public const ws_version_major = ws_version_reqd/& h100 and & HFF &
Public const ws_version_minor = ws_version_reqd and & HFF &
Public const min_sockets_reqd = 1
Public const socket_error =-1
 
Public const max_wsade133 = 256
Public const max_wsasysstatus = 128
 
Public type icmp_options
TTL as byte
ToS as byte
Flags as byte
Optionssize as byte
Optionsdata as long
End type
 
Dim icmpopt as icmp_options
 
Public type icmp_echo_reply
Address as long
Status as long
Roundtriptime as long
Datasize as integer
Reserved as integer
Datapointer as long
Options as icmp_options
Data as string * 250
End type
 
Public type hostent
Hname as long
Haliases as long
Haddrtype as integer
Hlen as integer
Haddrlist as long
End type
 
Public type wsadata
Wversion as integer
Whighversion as integer
Szdescription (0 to max_wsadete) as byte
Szsystemstatus (0 to max_wsasysstatus) as byte
Wmaxsockets as integer
Wmaxudpdg as integer
Dwvendorinfo as long
End type
 
 
Public declare function icmpcreatefile lib "ICMP. dll" () as long
 
Public declare function icmpclosehandle lib "ICMP. dll "_
(Byval icmphandle as long) as long
 
Public declare function icmpsendecho lib "ICMP. dll "_
(Byval icmphandle as long ,_
Byval destinationaddress as long ,_
Byval requestdata as string ,_
Byval requestsize as integer ,_
Byval requestoptions as long ,_
Replybuffer as icmp_echo_reply ,_
Byval replysize as long ,_
Byval timeout as long) as long
 
Public declare function wsagetlasterror lib "wsock32.dll" () as long
 
Public declare function wsastartup lib "wsock32.dll "_
(Byval wversionrequired as long ,_
Lpwsadata as wsadata) as long
 
Public declare function wsacleanup lib "wsock32.dll" () as long
 
Public declare function gethostname lib "wsock32.dll "_
(Byval szhost as string ,_
Byval dwhostlen as long) as long
 
Public declare function gethostbyname lib "wsock32.dll "_
(Byval szhost as string) as long
 
Public declare sub rtlmovememory lib "Kernel32 "_
(Hpvdest as any ,_
Byval hpvsource as long ,_
Byval cbcopy as long)
 
 
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/& h100 and & HFF &
 
End Function
 
 
Public Function lobyte (byval wparam as integer)
 
Lobyte = wparam and & HFF &
 
End Function
 
 
Public Function pingecho (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)
 
Call socketsinitialize
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
'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
Pingecho = echo. roundtriptime
Else: pingecho = echo. Status *-1
End if
 
Call icmpclosehandle (hport)
Call socketscleanup
 
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
'1970. 456.789.123 string, delimited
'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
'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 "&_
Trim $ (STR $ (min_sockets_reqd) & "supported sockets ."
Msgbox szbuf, vbexclamation
Socketsinitialize = false
Exit Function
End if
 
Socketsinitialize = true
 
End Function
 
'-- End block --'
 
 
''Form code
''
''To a form Add a command button (command1), two text boxes (text3, text4) and
''Six text boxes in a control array (text1 (0)-text1 (5).
''Labels are optional. Add the following to the command button:
''
''
''
''Option explicit
 
''Private sub commandementclick ()
''
''Dim ECHO as icmp_echo_reply
''Dim POS as integer
''
'''Ping an IP address, passing
'''Address and the Echo Structure
''Call Ping ("209.68.48.118", ECHO)
''
'''Display the results from the Echo Structure
''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
 
'-- End block --'

''If the return value is greater than or equal to 0, Ping is successful, and <0 indicates an error.
Public Function pingip (byval ipaddr as string) as long
Dim ECHO as icmp_echo_reply
Pingip = pingecho (ipaddr, ECHO)
End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.