Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of Ryan_R
Ryan_R🇦🇺

VB.NET - check is pc is online - current code not working
Trying to determine if a computer is connected to the Internet in VB.NET 2005

If My.Computer.Network.Ping("ryzza007.googlepages.com", 1000) Then...

The above line returns an 'Internal Error' and doesn't work for some reason. It worked the first time I used it (when I was online) - when I unplugged my network cable and re-ran it I got errors - the errors persisted after reconnecting.

Is there any other way to check if the PC is online?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Fernando SotoFernando Soto🇺🇸

Hi Ryan_R;

When you removed the cable and put it back and it stopped working, did your internet browser and or e-mail also stop working?

Is it also possible that you may have plugged it into the wrong connector?

You can also try the Ping class which give you more control of sending pigs out.

Ping class namespace, System.Net.NetworkInformation

System.Net.NetworkInformation.Ping.Send("ryzza007.googlepages.com", 1000)

fernando

Avatar of Ryan_RRyan_R🇦🇺

ASKER

All IE and Internet related software resumed normal operation when i plugged the cable back in - I made sure of this before I ran the program.
No - I'm 100% familiar with the back of my PC - built it myself

I'll try the new statement - hopefully it works.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Can you post the exact error that happen when when you try again.

Avatar of Ryan_RRyan_R🇦🇺

ASKER

>>System.Net.NetworkInformation.Ping.Send("ryzza007.googlepages.com", 1000)<<

The Ping item only has 'Equals' and 'RefernceEquals' items - doesn't have 'Send()'

Avatar of Ryan_RRyan_R🇦🇺

ASKER

Here's the unhandled exception: https://filedb.experts-exchange.com/incoming/ee-stuff/4679-Untitled.jpg

This time everything seemed to work - however I got that error in the following situation:

-Unplugged network cable - waited a few seconds
-Ran program - goes through the following code:
   If My.Computer.Network.IsAvailable Then
         If My.Computer.Network.Ping("ryzza007.googlepages.com", 1000) Then
             'All good
          Else
              If My.Computer.Network.Ping("https://www.experts-exchange.com", 1000) Then
                   'Problem with my site
              Else
                    'User is likely offline
              End if
           Endif
     Else
        'User offline
     End if


The second Ping (EE.com) gave me the error

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Ryan_RRyan_R🇦🇺

ASKER

If I put an 'On Error Resume Next' statement in and this error came up silently - do you have any ideas on what would happen / what msgbox's of mine would be displayed (ie User is online, offline, possibly online but can't find the server, etc).

if My.Computer.Network.IsAvailable = True Then
            ''''' make ping here
End If

Avatar of Ryan_RRyan_R🇦🇺

ASKER

It is already there...

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of telefoniatelefonia

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of Ryan_RRyan_R🇦🇺

ASKER

Thanks

Now I have something like this:
--------------------------------------------------------------------
On Error Resume Next
        If My.Computer.Network.IsAvailable = True Then
            Dim ip As Net.IPHostEntry = Net.Dns.GetHostEntry("ryzza007.googlepages.com")
            If UBound(ip.AddressList) > -1 Then
                If My.Computer.Network.Ping(ip.AddressList(0).ToString, 1000) = True Then
                    ''''OK
                    ' Server is available

                Else

                    ''''' OFFLINE
                    ' No network connection
                     MsgBox("It appears that you are still offline. Please check your Internet connection and try again later.", MsgBoxStyle.Information, "")
                End If
            Else

                ''''' INVALID HOST OR YOUR PROVIDER DNS CAN´T RESOLVE THE HOST
                ' Server is not responding
                If My.Computer.Network.Ping("https://www.experts-exchange.com", 1000) Then
                    MsgBox("Cannot connect to ryzza007.googlepages.com at the moment. Please try again later.", MsgBoxStyle.Information, "Server Unavailble")
                Else
                    ' No network connection
                    MsgBox("It appears that you are still offline. Please check your Internet connection and try again later.", MsgBoxStyle.Information, "VGEE")
                End If
            End If
        Else

            '''' PRIMARY NETWORK IS UNAVIABLE.
            ' No network connection
            MsgBox("It appears that you are still offline. Please check your Internet connection and try again later.", MsgBoxStyle.Information, "VGEE")
        End If
--------------------------------------------------------------------

You may notice that the second ping isn't resolving the IP address before pinging. I probaly could adapt the above code to change this but am not sure if the line of code mentioning the array that holds the first IP Address will need to be changed for the second time or not?  If you could let me know which code to use and where that would be fantastic.


Good to see you starting to answer questions, telefonia - keep up the good work   :o)


Hello Ryan.
I have an error on line the array because if exist some error you the app crash.
Copy this code:
--------------------

 If My.Computer.Network.IsAvailable = True Then
            Dim IP As Net.IPHostEntry
            Try
                IP = Net.Dns.GetHostEntry("www.google.com")
                Dim ping As New Net.NetworkInformation.Ping
                Try
                    Dim result As Net.NetworkInformation.PingReply = ping.Send(IP.AddressList(0), 1000)
                    Select Case result.Status
                        Case Net.NetworkInformation.IPStatus.Success
                            MsgBox("Success")
                            ''''' You can get the AVERAGE time ping result. and you can determine if is good time for you app.
                            Dim average As Long = result.RoundtripTime
                            MsgBox("Average Time: " & average)

                        Case Else
                            ''''' ERROR PING.
                            MsgBox(result.Status.ToString)

                    End Select
                Catch ex As Net.NetworkInformation.PingException
                    MsgBox(ex.Message)
                End Try
            Catch ex As Net.Sockets.SocketException
                Dim codeerror As Net.Sockets.SocketError
                codeerror = ex.ErrorCode
                Select Case codeerror
                    Case Is = Net.Sockets.SocketError.HostNotFound
                        '''''''''' Message for invalid host.
                    Case Else
                        '''''''''' Other errors.
                        MsgBox(codeerror.ToString)
                End Select
            End Try
        Else
            '''''' NO NETWORKS CONNECTION.

        End If

Ryan, other suggestion is chage timeout values from 1000 to 2000.
Some customer is using a Satelital connection and have higth delay.
Other problem is  somes host have firewall and block the icmp command in this case you can add a select case: Net.NetworkInformation.IPStatus.DestinationUnreachable to send message to user app.
You can see the enu for all case: Select Case result.Status

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Ryan_RRyan_R🇦🇺

ASKER

The method is simply to check if an updated version of my app is availble.

It's not exectly life-or-death if it doesn't work for some extreme cases such as those you mentioned above - I just wanted to make sure it would work in all usual cases - and not display an 'Unhandled Exception' error every time someone started my application. Your most recent suggestions might be a little over-the-top for me (no fault of yours of course) so I'll stick with what I have currently (it works for me so far which is good).

The final part to this question is as follows:
On the first DNS resolve and ping - I have these lines of code:

-----------------
Dim ip As Net.IPHostEntry = Net.Dns.GetHostEntry("ryzza007.googlepages.com")
            If UBound(ip.AddressList) > -1 Then
                If My.Computer.Network.Ping(ip.AddressList(0).ToString, 1000) = True Then
-----------------

For my 2nd ping - which is nested in an If statement - can I use this code exactly or will it need minor refinements:
------------------
Dim ip As Net.IPHostEntry = Net.Dns.GetHostEntry("https://www.experts-exchange.com")  'Not using google.com - as it is related to the 1st site
            If UBound(ip.AddressList) > -1 Then
                If My.Computer.Network.Ping(ip.AddressList(0).ToString, 1000) = True Then
------------------

And I'll also raise the points to 500 to help you get your Premium Services a little sooner    :o)

Avatar of Ryan_RRyan_R🇦🇺

ASKER

And I will change the timeout from 1000 to something a bit higher - will this make my program take longer to load if it is waiting for a reply?

Hello Ryan_R:, about the point no problem. My idea is only help you.
If you alway check a fix site "your site" is more easy because you know that exist :),
So, your resume lines is correct.:)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Ryan_RRyan_R🇦🇺

ASKER

Great - thanks for that
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.