Link to home
Start Free TrialLog in
Avatar of Jofnn
JofnnFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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?
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

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
Avatar of Jofnn

ASKER

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
Avatar of Jofnn

ASKER

The error I keep getting for that is:

A first chance exception of type 'System.Net.NetworkInformation.PingException' occurred in System.dll
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.
Avatar of Jofnn

ASKER

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

Avatar of Jofnn

ASKER

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.
Avatar of Jofnn

ASKER

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.

Avatar of Jofnn

ASKER

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?
Avatar of Jofnn

ASKER

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.
Avatar of Jofnn

ASKER

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!
ASKER CERTIFIED SOLUTION
Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jofnn

ASKER

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.
Avatar of Jofnn

ASKER

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!
Avatar of Jofnn

ASKER

I'm objecting on this so that I can accept multiple solutions
Avatar of Jofnn

ASKER

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