CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: ping

  1. #1
    Join Date
    Oct 1999
    Posts
    21

    ping

    How do I ping a computer from a Nt machine. I want to get a response back and I have to run this application every half and hour to test if the server is running.



  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: ping

    Try this Example I put together using the Winsock APIs:

    For this Example you need a Form with 2 Textboxes and a CommandButton..

    private Const MAX_WSADescription = 256
    private Const MAX_WSASYSStatus = 128
    private Const WS_VERSION_REQD = &H101

    private Type HOSTENT
    hName as Long
    hAliases as Long
    hAddrType as Integer
    hLen as Integer
    hAddrList as Long
    End Type

    private Type WSADATA
    wVersion as Integer
    wHighVersion as Integer
    szDescription(0 to MAX_WSADescription) as Byte
    szSystemStatus(0 to MAX_WSASYSStatus) as Byte
    wMaxSockets as Integer
    wMaxUDPDG as Integer
    dwVendorInfo as Long
    End Type

    private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest as Any, byval hpvSource as Long, byval cbCopy as Long)
    private Declare Function gethostbyname Lib "WSOCK32.DLL" (byval szHost as string) as Long
    private Declare Function WSAStartup Lib "WSOCK32.DLL" (byval wVersionRequired as Long, lpWSADATA as WSADATA) as Long
    private Declare Function WSACleanup Lib "WSOCK32.DLL" () as Long

    private Function GetIPAddress(byval sHost as string) as string

    Dim I as Integer
    Dim lHost as Long
    Dim lIP as Long
    Dim sIP() as Byte
    Dim sFullIP as string
    Dim tHost as HOSTENT
    Dim tSocket as WSADATA

    'Establish an Open Socket
    If WSAStartup(WS_VERSION_REQD, tSocket) <> 0 then Exit Function

    'Attempt to Locate Data pointer for Specified Host Name
    lHost = gethostbyname(sHost)

    If lHost = 0 then
    MsgBox "Unable to Locate Host."
    else
    'get the Host Info
    CopyMemory tHost, lHost, len(tHost)
    'Extract the Address of the IP Data from the Addresslist Pointer
    CopyMemory lIP, tHost.hAddrList, len(tHost.hAddrList)
    'Build a Tempory array to hold the IP Values
    ReDim sIP(tHost.hLen - 1)
    'Copy the IP Values into the Array
    CopyMemory sIP(0), lIP, tHost.hLen
    'Build the IP Address
    for I = 0 to tHost.hLen - 1
    sFullIP = sFullIP & "." & sIP(I)
    next
    'Trim of the Preceeding Period
    GetIPAddress = mid(sFullIP, 2)
    End If

    'Close up the Socket
    Call WSACleanup

    End Function

    private Sub Command1_Click()
    Text2 = GetIPAddress(Text1)
    End Sub





    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  3. #3
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Ping using levels???

    I used this useful code. It works and returns the IP address of a specific server giving as input the server name (eg www.yahoo.com)
    The problem is that in a specific situation where the server was "down" (I mean it was not connected to Internet because of a network damage), the program continued to find the server and giving the right IP. How is it possible? Are there various levels of ping?
    And something important: Trying to ping with this code, it resolved the right IP address, but at exactly the same time, I tried manually to ping by msdos prompt and the server not found (as expected). What's going on???

    Michael Vlastos
    Automation Engineer
    Intracom, Research & Development Division
    Development Programmes Department
    Athens, Greece

  4. #4
    Join Date
    Apr 2000
    Location
    Houston, TX, USA
    Posts
    199

    Re: Ping using levels???

    All ping is telling you that is that the server's NIC is responding to ICMP requests. That does not mean that the web site is necessarily working. The web site is listening over a socket (typically port 80) and just because you can't load the web page does not mean the server is down. And just because ping is working does not mean the web site is up.

    Bottom Line : If ping returns the server is powered up, and configured to return ICMP. It does not mean that anything else is working on the server besides the NIC....

    Tim Cartwright 'Will write code for food.
    Sr Systems Architect - Information Systems
    Splitrock Services Inc.
    Tim C.
    //Will write code for food

  5. #5
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Ping using levels???

    Ok I understood but you missed a point: I said that in the same time, I was pinging manually (by MSDOS prompt) and taking NO reply, but besides the application with the code posted here, it's pinging was giving reply!!! How is it possible? And most of all, how can I ping the server correctly, as exactly can be done by MSDOS prompt?

    Michael Vlastos
    Automation Engineer
    Intracom, Research & Development Division
    Development Programmes Department
    Athens, Greece

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

Featured