Results 1 to 4 of 4

Thread: how to ping using winsock

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    4

    how to ping using winsock

    Is there anyone can give an idea on how to ping a server using VB?

  2. #2
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: how to ping using winsock

    vb 6.0

    VB Code:
    1. Option Explicit
    2. Public Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long
    3. Public Const IP_SUCCESS As Long = 0
    4. Public Const MAX_IP_STATUS As Long = (11000 + 50)
    5. Public Const IP_PENDING As Long = (11000 + 255)
    6. Public Const PING_TIMEOUT As Long = 500
    7. Public Const WS_VERSION_REQD As Long = &H101
    8. Public Const MIN_SOCKETS_REQD As Long = 1
    9. Public Const SOCKET_ERROR As Long = -1
    10. Public Const INADDR_NONE As Long = &HFFFFFFFF
    11. Public Const MAX_WSADescription As Long = 256
    12. Public Const MAX_WSASYSStatus As Long = 128
    13.  
    14. Public Type ICMP_OPTIONS
    15.     Ttl             As Byte
    16.     Tos             As Byte
    17.     Flags           As Byte
    18.     OptionsSize     As Byte
    19.     OptionsData     As Long
    20. End Type
    21.  
    22.  
    23. Public Type ICMP_ECHO_REPLY
    24.     Address         As Long
    25.     status          As Long
    26.     RoundTripTime   As Long
    27.     DataSize        As Long 'formerly integer
    28.    'Reserved        As Integer
    29.     DataPointer     As Long
    30.     Options         As ICMP_OPTIONS
    31.     Data            As String * 250
    32. End Type
    33.  
    34. Public Type WSADATA
    35.    wVersion As Integer
    36.    wHighVersion As Integer
    37.    szDescription(0 To MAX_WSADescription) As Byte
    38.    szSystemStatus(0 To MAX_WSASYSStatus) As Byte
    39.    wMaxSockets As Long
    40.    wMaxUDPDG As Long
    41.    dwVendorInfo As Long
    42. End Type
    43.  
    44. Public Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
    45.  
    46. Public Declare Function IcmpCloseHandle Lib "icmp.dll" _
    47.    (ByVal IcmpHandle As Long) As Long
    48.    
    49. Public Declare Function IcmpSendEcho Lib "icmp.dll" _
    50.    (ByVal IcmpHandle As Long, _
    51.     ByVal DestinationAddress As Long, _
    52.     ByVal RequestData As String, _
    53.     ByVal RequestSize As Long, _
    54.     ByVal RequestOptions As Long, _
    55.     ReplyBuffer As ICMP_ECHO_REPLY, _
    56.     ByVal ReplySize As Long, _
    57.     ByVal Timeout As Long) As Long
    58.    
    59. Public Declare Function WSAGetLastError Lib "wsock32" () As Long
    60.  
    61. Public Declare Function WSAStartup Lib "wsock32" _
    62.    (ByVal wVersionRequired As Long, _
    63.     lpWSADATA As WSADATA) As Long
    64.    
    65. Public Declare Function WSACleanup Lib "wsock32" () As Long
    66.  
    67. Public Declare Function gethostname Lib "wsock32" _
    68.    (ByVal szHost As String, _
    69.     ByVal dwHostLen As Long) As Long
    70.    
    71. Public Declare Function gethostbyname Lib "wsock32" _
    72.    (ByVal szHost As String) As Long
    73.  
    74. Public Declare Sub CopyMemory Lib "kernel32" _
    75.    Alias "RtlMoveMemory" _
    76.   (xDest As Any, _
    77.    xSource As Any, _
    78.    ByVal nbytes As Long)
    79.    
    80. Public Declare Function inet_addr Lib "wsock32" _
    81.    (ByVal s As String) As Long
    82.    
    83. Public Function Ping(sAddress As String, _
    84.                      sDataToSend As String, _
    85.                      ECHO As ICMP_ECHO_REPLY) As String
    86.  
    87.   'If Ping succeeds :
    88.   '.RoundTripTime = time in ms for the ping to complete,
    89.   '.Data is the data returned (NULL terminated)
    90.   '.Address is the Ip address that actually replied
    91.   '.DataSize is the size of the string in .Data
    92.   '.Status will be 0
    93.   '
    94.   'If Ping fails .Status will be the error code
    95.    
    96.    Dim hPort As Long
    97.    Dim dwAddress As Long
    98.    
    99.   'convert the address into a long representation
    100.    dwAddress = inet_addr(sAddress)
    101.    
    102.   'if a valid address..
    103.    If dwAddress <> INADDR_NONE Then
    104.    
    105.      'open a port
    106.       hPort = IcmpCreateFile()
    107.      
    108.      'and if successful,
    109.       If hPort Then
    110.      
    111.         'ping it.
    112.          Call IcmpSendEcho(hPort, _
    113.                            dwAddress, _
    114.                            sDataToSend, _
    115.                            Len(sDataToSend), _
    116.                            0, _
    117.                            ECHO, _
    118.                            Len(ECHO), _
    119.                            PING_TIMEOUT)
    120.  
    121.         'return the status as ping succes and close
    122.          Ping = ECHO.status & "#" & ECHO.RoundTripTime
    123.          
    124.          Call IcmpCloseHandle(hPort)
    125.      
    126.       End If
    127.      
    128.    Else
    129.         'the address format was probably invalid
    130.          Ping = INADDR_NONE
    131.          
    132.    End If
    133. End Function
    134.    
    135.  
    136. Public Sub SocketsCleanup()
    137.    
    138.    If WSACleanup() <> 0 Then
    139.        MsgBox "Windows Sockets error occurred in Cleanup.", vbExclamation
    140.    End If
    141.    
    142. End Sub
    143.  
    144.  
    145. Public Function SocketsInitialize() As Boolean
    146.  
    147.    Dim WSAD As WSADATA
    148.    
    149.    SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = IP_SUCCESS
    150.    
    151. End Function
    152.  
    153.  
    154. Public Function GetStatusCode(status As Long) As String
    155.  
    156.    Dim MSG As String
    157.    
    158.    Select Case status
    159.       Case IP_SUCCESS:               MSG = "ip success"
    160.       Case Else:                     MSG = "unknown  msg returned"
    161.    End Select
    162.    
    163.    GetStatusCode = CStr(status) & "   [ " & MSG & " ]"
    164.    
    165. End Function

    ----------------------------------------------
    VB.NET 2005 (FRAMEWORK 2.0)

    VB Code:
    1. Dim png As New System.Net.NetworkInformation.Ping
    2. Dim rslt As System.Net.NetworkInformation.PingReply
    3. Dim ba() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9}
    4.  
    5. rslt = png.Send("63.236.73.220", 1500, ba)
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    4

    Re: how to ping using winsock

    Thank you very much TokersBall_CDXX!

    I'm sorry I'am very new to VB and VB.net, I tried the VB.net code that you have gave but I've the error messages below. Please advise.

    Type 'System.Net.NetworkInformation.Ping' is not defined.
    Type 'System.Net.NetworkInformation.PingReply' is not defined.


    Thanks again.

  4. #4
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: how to ping using winsock

    what version of .net are you using?

    ping is unavailable in 2003 and you will need to use the vb6 code with some slight modifications.
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width