Click here to Skip to main content
14,932,748 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm doing a small program for testing network connectivity
All My work is ok but the only problem I'm facing is the code
Res = My.Computer.Network.Ping(txtComputerName.Text).ToString

it's working ok on my local network and give me good results
but when I try to ping a web site it gives me error

note that I can access the internet through an ISA server 2006 and can't connect my computer directly to the router

What I have tried:

This is the entire function
<pre>     Try
            Dim Res As Boolean

            lblError.Text = String.Empty
            StopPing = False
            Me.PictureBox1.Image = ImageList1.Images(0)
            Me.lblElpased.ForeColor = Color.MidnightBlue

            Dim i As Short = 0
                If StopPing Then Exit Do
                Me.PictureBox1.Image = ImageList1.Images(0)
                Mtim = Stopwatch.StartNew
                Res = My.Computer.Network.Ping(txtComputerName.Text).ToString
                Me.lblElpased.Text = "Time: " & Mtim.ElapsedMilliseconds & " ms"

                Dim hostInfo As IPHostEntry = Dns.GetHostEntry(txtComputerName.Text)
                Me.lblError.Text = hostInfo.HostName & vbCrLf

                If Res Then
                    Me.PictureBox1.Image = ImageList1.Images(1)
                    Me.lblElpased.ForeColor = Color.DarkGreen

                    Dim index As Integer
                    For index = 0 To hostInfo.AddressList.Length - 1
                        Me.lblError.Text &= hostInfo.AddressList(index).ToString & vbCrLf

                    Me.PictureBox1.Image = ImageList1.Images(2)
                    Me.lblElpased.ForeColor = Color.DarkRed

                    Dim index As Integer
                    For index = 0 To hostInfo.AddressList.Length - 1
                        Me.lblError.Text &= hostInfo.AddressList(index).ToString & vbCrLf

                End If
                i += 1
            Loop Until ((i >= NuPings.Value) And (chkKeepPing.Checked = False))
        Catch ex As Exception
            lblError.Text = ex.Message
        End Try
Updated 30-Jul-18 22:45pm
Richard MacCutchan 31-Jul-18 4:26am
"t gives me error"
Then you need to find out why it gives you error.
samerselo 31-Jul-18 4:32am
the cause of error is

I have ISA server that provide internet to the network that means to connect to internet I need (proxy + credentials) to be passed correctly to ping command to be executed correctly that is what I failed to do
Dave Kreskowiak 31-Jul-18 9:32am
What's the name you're trying to ping? A lot of sites no longer respond to pings.

1 solution

note that I can access the internet through an ISA server 2006 and can't connect my computer directly to the router
In such cases you can only use protocols that are allowed (forwarded) by the server. For ping this requires that outgoing ICMP packets and the corresponding responses are not filtered. You might ask the administrator of the ISA server about this.

You can't ping a website. The parameter for the Ping() function must be an IP address, an URL like, or a computer name of a system in your local network.

See also Network.Ping Method (Microsoft.VisualBasic.Devices) | Microsoft Docs[^]:
The Ping method is not a fail-safe method for determining the availability of a remote computer: the ping port on the target computer may be turned off or the ping request may be blocked by a firewall or router.

The address passed to the Ping method must be DNS resolvable and cannot be preceded by "http://".
That means that you might get no response even when ICMP ping packets are not filtered by the ISA server.

I'm doing a small program for testing network connectivity
Depending on what you mean by "network connectivity" this can be done by other methods:

Check the status of your systems network interface (NetworkInterface.OperationalStatus Property (System.Net.NetworkInformation) | Microsoft Docs[^]).

Perform a name resolution using Dns.GetHostEntry Method (System.Net) | Microsoft Docs[^] with a valid name. This will check the connection to one of the DNS resolvers configured for your network interface. This might be either the ISA server, the router, or a DNS server in the net and tells you so if connectivity to that system is present.

For internet connectivity (which would be a problem of the ISA server or router if not available) try to connect to a service like HTTP of a known server that is not blocked by the ISA server.

There is also a Windows API function returning the connection state: INetworkListManager::GetConnectivity | Microsoft Docs[^]

Unfortunately there seems to be no .Net equivalent. But the web should provides examples on how to call it from .Net like How to use the Windows NLM API to get notified of new network connectivity[^].
samerselo 31-Jul-18 7:22am
thank you
that expain alot

the goal of my program is two things
1 - is to Know if computer is on and I can access it thorough local network
2 - is to check if my local computer have an access to internet or not

I achieved the number 1 successfully but failed on number 2
samerselo 31-Jul-18 7:26am
note: How ping command of command prompt does the two goals at the same time
Jochen Arndt 31-Jul-18 7:50am
See my updated answer.
Using ping is the least reliable method even when not filtered.
samerselo 31-Jul-18 8:12am
interesting I'll study that maybe I'll find something useful
samerselo 31-Jul-18 8:30am
That didn't do it

the function give true to the result Is the machine connected to internet? even when I use an account that does not have access to internet
Jochen Arndt 31-Jul-18 8:48am
That is the a correct answer:
The system itself has internet access.

But due to your specific setup, access to some services - like probably HTTP[s] - is restricted by using a gateway / proxy which - again in your case as I understand - requires authorisation.

So your question is probably
"How to check if the current user is allowed to access the internet"?

That can be only answered when knowing which restrictions are set for authorised users and for others and how the authorisation is implemented. That means you have to find a protocol and a server that is allowed for authorised users and denied for others. The person who can help with this is the administrator of the ISA server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900