How do I ping a remote PC and confirm if passed/failed on VB

Jofnn
Jofnn used Ask the Experts™
on
Hi,

I'm writing a new application at the moment in Visual Basic 2008 in vb.net.

I have a text box which the user will enter the "asset number" of the remote PC in our company.  When the user changes the data in the box, I want it to do a ping of the data in the box and if passed (i.e. gets reply) it will show box1... or if it fails (i.e. timeout) it will show box2.

Box1/Box2 will just be labels which will appear/disappear dependant on the result...

Can you help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If you google "vb.net ping ip address" you will find a lot of material.

The very first result link seems like a good start.

http://www.xtremevbtalk.com/archive/index.php/t-261882.html

Author

Commented:
Hi Todd,

Thanks for the quick reply.  I have done a bit of searching on Google and can't find anything that seems to answer the question.

I've checked the link that you have mentioned, however, this is for a command in vb2005... When I use the command in vb2008 it falls over if the ping is false.

Jonathon

Author

Commented:
The error I keep getting for that is:

A first chance exception of type 'System.Net.NetworkInformation.PingException' occurred in System.dll
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

When you say something targets 2005, you're really talking about the framework as of that version.

You'd need to post code and the actual exception.

Author

Commented:
Todd,

The coding is from that page (see snippet) and the error is that shown above.
Jonathon



Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DidItRespond As Boolean
 
DidItRespond = My.Computer.Network.Ping(AssetNumber.Text, 1000)
 
If DidItRespond Then
Debug.WriteLine("Host responded.")
Else
Debug.WriteLine("Host did not respond.")
End If
 
End Sub

Open in new window

That code works for me without error. The message you provided doesn't contain anything useful, so I'm not sure how to proceed.


Dim DidItRespond As Boolean
DidItRespond = My.Computer.Network.Ping("112.16.1.21", 1000)

Open in new window

Author

Commented:
Todd,

I don't understand what you mean by "doesn't contain anything useful"? I've provided you both the code that I'm using, and the error message I get...

Basically... for test purposes I have the following on a Form:

TextBox (AssetNumber)
Button (Button1)

The test has been setup so that when you click the button, it will run the code, and send a ping request to the Asset number included in the "AssetNumber" text box.  Therefore... in the code that's provided, I've omitted the ip address... and replaced it with "AssetNumber.text".

I tested this by filling in the box with my own asset number (A12735) and then clicked the button... a message was added to the debug box saying "Host Responded".  I then tried another number which I knew was not a live machine (A12734) - which SHOULD write "Host did not respond" to the debug box... but it didn't...

When it fails to respond, the app stops with the following message:

"A first chance exception of type 'System.Net.NetworkInformation.PingException' occurred in System.dll"
The message provided is a custom exception defined by the framwork for ping functions. In an of itself, it tells you nothing. If you run the code I provided and enter a bogus address, when it breaks with that exception, you can drill into the exception object and you'll see the real message.

{"No such host is known"}

This function works just fine for me with a reachable host, and provides the shown exception with an unreachable host.

Of course, you need to handle the exception for unreachable hosts. It might be nice if the framework didn't rely on an exception, but that's how they implemented it.

Author

Commented:
No such host is known is the error I got... when you looked into it further...

However, I thought that that should return the "host did not respond" (hence it being an "else")...

I'll have another play about with it tomorrow... but any other advice would be great!
That would be how most of us woudl expect it to work , but because it throws the exception, you never get to the point where you can evaluate the return value. Since VB has that ugly "on error resume next" you can flip that before the call and the return value will work as expected. I just confirmed this.
------
Public Function Ping(ByVal hostNameOrAddress As String, ByVal timeout As Integer) As Boolean
     Member of Microsoft.VisualBasic.Devices.Network
Summary:
Pings the specified server.

Parameters:
hostNameOrAddress: String. The URL, computer name, or IP number of the server to ping. Required.
timeout: System.Int32. Time threshold in milliseconds for contacting the destination. Default is 500. Required.

Return Values:
Boolean indicating whether or not the operation was successful.

Author

Commented:
I've managed to work it out by trying some more things... The following code works and lets you perform an action dependant on whether it passed/failed:

        Try
            My.Computer.Network.Ping(AssetNumber.Text, 600)
            'Perform an action if it works
        Catch ex As Exception
            'Perform an action if it fails
        End Try

If you wanted to see what the exception was, then add a part in under the "Catch ex As Exception" with a simple line of:

MsgBox(ex.message)
Is this issue resolved? Need any more help with it?

Author

Commented:
Issue is resolved Todd!

I used the Try method (as documented above) and this works perfectly!  Thanks for all your help on this!
Great. Glad I could help.

Of course, "On Error Resume Next/Check error code" and Try/Catch do exactly the same thing.

I'd appreciate it if you could accept the solution.

Author

Commented:
Well... they didn't quite do the same thing...

The "on error resume next and check error code" didn't work out in a live environment... both of those made the application fall over if the ping request failed!  This was useless for an application which one of it's main tasks was to do a background ping...

Whereas "try" does what it says on the tin... it will try it, and give you the results if it works or not... but can be used in a controlled way!

As such, I will accept my solution as the answer (as this will work for future users) however due to your hard work and patience throughout this, I will be awarding you the points!

Thank you again for everything, and I hope my answer can go on and help others!
Obviously, I worked quite a bit on this solution and did provide a working solution, as shown below.

On Error Resume Next is as old as the hills and most certainly does work. There's nothing that a "production" environment can do to change that behavior. Only the IDE can be configured to stop on all errors.

I do admit I like the Try/Catch better and I should have suggested it, but ... I do feel I should get the points for my work.

In the end, it doesn't matter that, out of this entire thread, the one message with the try/catch be flagged as the solution, especially when the one I posted before it (and below) most certainly does work.
        Dim DidItRespond As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        On Error Resume Next
 
        DidItRespond = My.Computer.Network.Ping("bad address", 1000)
 
        If DidItRespond Then
            MsgBox("Host responded.")
        Else
            MsgBox("Host did not respond.")
        End If
 
        On Error GoTo 0
End Sub

Open in new window

Author

Commented:
Todd,

I don't understand why you are getting frustrated over this?!

I tried the solution that was provided and it didn't work... as soon as the ping request failed, it fell over... and that was before the "on error resume next"  As such, I worked some more, and remembered an old try/catch that I had used in a previous code, so thought I'd try it out and it worked!

Also, if you had paid attention to the topic before objecting to it's closure, you would have noticed that I had actually accepted my own post, however, assigned the points to you... So... even though I've highlighted my answer as the fix, you have got the points for it!

I'm going to re-select my answer as the fix now... and once again will award the points to you for your hard work on this... I'd appreciate if you didn't object again without paying attention to the closure details!

(and thank you once again for your help!)

I did pay attention the closure details. I looked and looked and could find nothing about the points, which is why I reopened it. I've never experienced this type of closure before. Now I see the points.

I still don't agree with it, because I posted the on error resume next almost two hours before you posted the try catch. So clearly the correct solution was presented before you posted an alternative. I'll drop it and let it close as-is.

Author

Commented:
Todd,

I don't mean to drag this out and argue further, but the solution you provided didn't work for me!  So there's every chance that this won't work for other people!  (Same can be said for the fix I posted... it worked for me, but there's every chance it won't work for others!)

Remember though that these fixes will both be included on here for following users to choose from!

Author

Commented:
I'm objecting on this so that I can accept multiple solutions

Author

Commented:
turns out I can accept multiple when one of them is mine.

Therefore to keep the peace, I've accepted your solution... Future users can obviously read the page and choose their own option.

Thanks again

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial