Results 1 to 14 of 14

Thread: Ping Network Computer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    79

    Ping Network Computer

    Very simple setup. Local network with 2 computers, a printer and Internet access. The server often needs to know the status of the 2nd computer. I am using the following code which seems a common solution:


    Code:
    If My.Computer.Network.Ping("Jack-Laptop") Then
        MsgBox("Pinged successfully.")
    Else
        MsgBox("Pinged computer is off-line")
    End If
    When "Jack-Laptop" is on-line, all is well ... MsgBox prints correctly.
    When "Jack-Laptop" is 0ff-line there is an error and MsgBox is not printed.

    The error msg is a rather ambiguous 'unhandled exception.'

    What am I missing here?

    Having seen my 83rd birthday come and go, I am thinking this will be my last project. Over the years you guys have been great ... there is VB Forum input in almost all of my projects. Thanks for that!
    Last edited by dday9; Jun 29th, 2021 at 04:52 PM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Ping Network Computer

    According to the documentation (here), the two exception types you can expect are:
    1. InvalidOperationException - No network connection is available.
    2. PingException - URL was not valid.


    My first suggestion would be to wrap your statement in a Try/Catch statement so that you can get the exception details:
    Code:
    Try
        If My.Computer.Network.Ping("Jack-Laptop") Then
            MessageBox.Show("Pinged successfully.")
        Else
            MessageBox.Show("Pinged computer is off-line")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Setup a breakpoint in exception handler portion of the Try/Catch and relay the details here.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    79

    Re: Ping Network Computer

    Thanks dday9. I should have mentioned that I did the Try...Catch with the same result. I also placed an 'On Error GoTo' (not with the Try...Catch) in the sub. Again the same response. In all cases I get: "An exception occurred during a Ping request." Execution stops at the first line i.e. "If My.Computer.Network.Ping("Jack-Laptop")"

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Ping Network Computer

    What was the Exception type?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Ping Network Computer

    Quote Originally Posted by dday9 View Post
    What was the Exception type?
    If I had to guess it would be that it is PingException and the resolving mechanism did NOT resolve "Jack-Laptop".
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    79

    Re: Ping Network Computer

    I did not get anything more than the error msg ... no number.
    If it is PingException, how do I fix that? This is my first network project.

  7. #7
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Ping Network Computer

    Quote Originally Posted by vbchJack View Post
    I did not get anything more than the error msg ... no number.
    If it is PingException, how do I fix that? This is my first network project.
    When it says "Unhandled Exception" it should at the very least give you the Type of exception, a number isn't normally used in this context.

    If you are unsure then just copy and paste the error message here.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Ping Network Computer

    Yeah, I'm not usually one for screenshots, but I think this might be a case where a screenshot of the error message may provide me with the details that I'm looking for.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Ping Network Computer

    Quote Originally Posted by vbchJack View Post
    I did not get anything more than the error msg ... no number.
    If it is PingException, how do I fix that? This is my first network project.
    Have you tried using the IP addresses of the machines, instead of names, you are pinging? Based on your earlier description you could assign IP addresses to the devices.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: Ping Network Computer

    May I propose the networkinformation ping with PingReply?
    I use it async but I suppose you can use it non async.
    It will give you more information but I suppose it will exception out in certain cases:

    https://docs.microsoft.com/en-us/dot...d?view=net-5.0
    https://docs.microsoft.com/en-us/dot...s?view=net-5.0

    Something like:

    Code:
     Dim pinger As New Ping()
    
            Try
                Dim pr As PingReply = pinger.Send("Jack-Laptop", 3000)
                If pr.Status = IPStatus.Success Then
                   MsgBox("Pinged successfully.")
                Else
                   MsgBox("Status",pr.Status.tostring) ' not sure if you just get the status here or you have to convert from the above link, haven't checked.
                End If
    ' and an exception catch  if it fails
            Catch ex As Exception
                MessageBox.Show("IP Error", ex.message)       
                Exit Sub
            End Try
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    79

    Re: Ping Network Computer

    Attachment 181775

    This from the Immediate window:
    at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)</ExceptionString><NativeErrorCode>2AF9</NativeErrorCode></InnerException></Exception></TraceRecord>

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,754

    Re: Ping Network Computer

    Based on your image, you're getting a PingException (displayed in the dialog title of the exception).

    I would suggest what dbasnett suggested in post #9 and try to ping just the IP address of the computer first. If that works, then what is likely happening is that either:
    1. There is a typo in the computer name
    2. There is a specific format you need to follow for pinging networked machines
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Ping Network Computer

    Try it like this

    First a method,
    Code:
        Private Function PingIt(HostOrIP As String) As Boolean
            Dim rv As Boolean = False
            Try
                Dim _ip As Net.IPAddress
                Dim he As Net.IPHostEntry
                Dim pr As Net.NetworkInformation.PingReply
                Dim _ping As New Net.NetworkInformation.Ping
                If Not Net.IPAddress.TryParse(HostOrIP, _ip) Then
                    he = Net.Dns.GetHostEntry(HostOrIP)
                    If he IsNot Nothing AndAlso he.HostName <> "" Then
                        pr = _ping.Send(he.HostName)
                        If pr.Status = Net.NetworkInformation.IPStatus.Success Then
                            rv = True
                        End If
                    End If
                    he = Nothing
                Else
                    pr = _ping.Send(_ip)
                    If pr.Status = Net.NetworkInformation.IPStatus.Success Then
                        rv = True
                    End If
                End If
            Catch ex As Exception
            End Try
            Return rv
        End Function
    Then use the method like this,
    Code:
            Dim foo As Boolean = PingIt("Jack-Laptop")
            If foo Then
                'success
            Else
                'fail
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jul 2012
    Posts
    79

    Re: Ping Network Computer

    Thanks dbasnett.
    Your solution is above my paygrade but it works.
    And thanks to all you other responders for your time and help.

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