Internet connectivity

  • Thread starter Thread starter Adam Honek
  • Start date Start date
A

Adam Honek

Hi all,

How would one test for internet connectivity in VB.NET instead
of using the Win32 API methods?

Thanks,
Adam
 
Adam,

Pinging the first IP address outside your own environment is the way as is
always used with or withouth a program.

Cor
 
Yes I realise it's done via ICMP but I thought .NET has its own method
to check it without employing specialised techniques.

Adam
 
Hi all,

How would one test for internet connectivity in VB.NET instead
of using the Win32 API methods?

Thanks,
Adam

If you want to know if a user is able to connect to the internet:
Dim isAvailable As Boolean
isAvailable = My.Computer.Network.IsAvailable

False = user has no current network connection of any kind
True = user is connected to a network of some sort, but doesn't
necessarily mean the internet is reachable.

The companion statement to the above is:
My.Computer.Network.NetworkAvailabilityChanged

I usually do the above at startup in (MyApplicationEvents).

Whether or not you use the above, each attempt to reach a server
should use WebRequest and a Try/Catch Block where you are looking for
a response of "OK" (200). If there is a problem, you would interpret
the exception message which could be "No Connection", "Not
Authorized", "Timeout" etc.

There are WebRequest examples in the help file if you need them.

Gene
 
I've done what you suggest with WebRequest but it seems it's quite
a thread hog if it's called every second. Unless I put it in its own thread
the UI thread is visibly interupted.

Hmmm, maybe instead of pinging a website it pinged the NIC default IP
gateway on one's PC, this would give better performance?

Nice method though, quick and easy.

Adam
 
Adam said:
Hmmm, maybe instead of pinging a website it pinged the NIC default IP
gateway on one's PC, this would give better performance?

The default gateway is not necessarily outside the LAN. Neither is(are) the
DNS Server(s).

E.g. C:\>ipconfig

Windows IP Configuration

Ethernet adapter Local Area Connection:

IP Address. . . . . . . . . . . . : 192.168.123.14
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.123.1 <-firewall appliance

Andrew
 
I've done what you suggest with WebRequest but it seems it's quite
a thread hog if it's called every second. Unless I put it in its own thread
the UI thread is visibly interupted.

Hmmm, maybe instead of pinging a website it pinged the NIC default IP
gateway on one's PC, this would give better performance?

Nice method though, quick and easy.

Adam

I'm not sure what you mean by "called every second". Here is a snip
of something I use. When the execution gets to this point, it's
either a valid response, or there was an exception. On a broadband
connection, here, from the time the URL is selected until the picture
is completely displayed is generally 2-3 seconds. Exceptions other
than timeout are virtually instaneous.

(Code is used in a BackgroundWorker component, DoWork event)

Try
Dim request As HttpWebRequest =
DirectCast(WebRequest.Create(PicLoc), HttpWebRequest)
request.Timeout = 10000
Dim response As HttpWebResponse =
DirectCast(request.GetResponse(), HttpWebResponse)
If response.StatusDescription = "OK" Then
RequestStatus = True
End If
response.Close()

Catch ex As WebException

Select Case ex.Status
Case WebExceptionStatus.ConnectFailure

Case WebExceptionStatus.Timeout

Case WebExceptionStatus.ProtocolError

Case Else

end select

End Try


Gene
 
Yup so yours is not running in the same time slice as the UI is. On mine
it's currently running this way hence there's this lag in moving the window
or even clicking through the menus.

That above is why I have a threading question posted in this newsgroup.

Thanks,
Adam
 
Back
Top