check connection

jasonsky

New member
Joined
Oct 25, 2005
Messages
3
Programming Experience
3-5
I am doing a project about a web browser. One small task in my initialization is to check if the client's computer connect to the Internet. Any control support this connection checking?

Thanks.
 
Hi,
There are couple ways to check if the current computer has internet but i prefer this one (simply call an URL and if you get response that's it) i.e.

VB.NET:
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] AvailableConnection() [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'Returns True if connection is available 
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objUrl [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Uri([URL="http://www.google.com/"]http://www.google.com/[/URL]) [COLOR=seagreen]'u can use any other domain[/COLOR]
[/SIZE][SIZE=2][COLOR=#008000] 
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] webReq [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.WebRequest
webReq = System.Net.WebRequest.Create(objUrl)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] resp [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.WebResponse
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'if get response returns True
[/COLOR][/SIZE][SIZE=2]resp = webReq.GetResponse
resp.Close()
webReq = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#008000]'error
[/COLOR][/SIZE][SIZE=2]resp.Close()
webReq = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]

Usage:
VB.NET:
[COLOR=blue]If [/COLOR]AvailableConnection() = [COLOR=blue]True then[/COLOR]
[COLOR=lime]'do something[/COLOR]
[COLOR=blue]Else[/COLOR]
[COLOR=lime]'do something[/COLOR]
[COLOR=blue]End if[/COLOR]

Regards ;)
 
Kulrom you made that far too easy for a homework task :p
 
Back
Top