|
-
Dec 18th, 2001, 11:47 PM
#1
Thread Starter
Hyperactive Member
Internet connection
I need a way to tell when there is an internet connection no mater what kind. There are a lot of examples out there, but they just don't work right. I want to tell if there is a connection no mater if it’s Dial-up, AOL, Cable, DSL, whatever! Just like all the programs out there do. Like AIM for example; It knows pretty well when there’s a connection and when there’s not.
Also, getting just an IP address doesn’t cut it. I've tried these things out there. I got cable and I run these examples on a timer, I pull the plug on my cable, all the other programs shut down, but the examples (More then just one I’ve had a few) STILL ARE SAYING "ONLINE"! I don't know how to do it can anyone help?
-
Dec 19th, 2001, 01:21 AM
#2
PowerPoster
The only way to know effectively if you have an internet connection is to ping a reliable server , say Yahoo or Netscape, (don't ping Microsoft as it does not repond to ping requests). To get an example on how to use ping in VB, goto www.allapi.net
-
Dec 19th, 2001, 01:53 AM
#3
Thread Starter
Hyperactive Member
Hey thanks a lot!
You know, I know about ping and stuff but not from vb, thats a good idea. I used (http://www.allapi.net/apilist/apifun...heckConnection) example but i don't know if that counts as using ping, but it seems to work. That's what came up when I searched for 'Ping'.
Thanks
-
Dec 19th, 2001, 05:02 AM
#4
PowerPoster
As far as I remember, it uses ping results to determine Internet Connection state. Look in the code, and if you find ICMPEcho api call listed there, it means it is using PING.
-
Dec 19th, 2001, 10:23 PM
#5
Thread Starter
Hyperactive Member
Nah, i got this one i think its pretty good thanks any way.
This is what i used:
VB Code:
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Private Const FLAG_ICC_FORCE_CONNECTION = &H1
'***************************************
Function InternetConnectionCheck(WebSite As String) As Boolean
If InternetCheckConnection(WebSite, FLAG_ICC_FORCE_CONNECTION, 0&) = 0 Then
'No connection
InternetConnectionCheck = False
Else
'There is a connection
InternetConnectionCheck = True
End If
End Function
'*******************************************
If InternetConnectionCheck("http://www.yahoo.com") = True Then
MsgBox "You are online"
Else
MsgBox "You are offline"
End If
-
Dec 20th, 2001, 02:33 AM
#6
PowerPoster
malc.net
You might have problems with your code when your computer is coonected through proxy. In some cases it might work, in some it may not. The best way still remains the Ping utility.
-
Dec 20th, 2001, 11:03 AM
#7
valid point
yes my could merely establishes the connection is present without accessing the web.
whereas MikeBAM's code requires checking a URL, which is fine unless the user has a firewall (like zonealarm) in which case the code will beunable to verify a web connection without user intervention.
Either way is fine, i guess it depends on your requirements.
-
Dec 20th, 2001, 11:38 AM
#8
Yea, I know that if I was running a program that started pinging things for no reason, I'd have ZoneAlarm shut it down perminantly. 
Always inform the user when a program is about to hit the Internet (unless the thing's primary function is to do that and it's pretty apparent...)
You should also have a user option to disable the internet detection. Be sure to put in a "Automatically Detect Connection?" check box. There's nothing more annoying than having a "dial-on-demand" connection going nuts because some stupid, chatty program wants to get out.
I had to firewall out half my internal network, because every time I plugged my laptop in and started it up, my main server would start dialing out for no useful reason.
-
Dec 23rd, 2001, 06:30 PM
#9
Thread Starter
Hyperactive Member
Hey amitabh, Hoes this? Does this do it?
(i found problems with that old one now)
VB Code:
Const SOCKET_ERROR = 0
Private Type WSAdata
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type
Private Type Hostent
h_name As Long
h_aliases As Long
h_addrtype As Integer
h_length As Integer
h_addr_list As Long
End Type
Private Type IP_OPTION_INFORMATION
TTL As Byte
Tos As Byte
Flags As Byte
OptionsSize As Long
OptionsData As String * 128
End Type
Private Type IP_ECHO_REPLY
Address(0 To 3) As Byte
Status As Long
RoundTripTime As Long
DataSize As Integer
Reserved As Integer
data As Long
Options As IP_OPTION_INFORMATION
End Type
Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long
Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSAdata As WSAdata) As Long
Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, ByVal TimeOut As Long) As Boolean
Function Internet() As Boolean
Const HostName = "www.aol.com"
Dim hFile As Long, lpWSAdata As WSAdata
Dim hHostent As Hostent, AddrList As Long
Dim Address As Long, rIP As String
Dim OptInfo As IP_OPTION_INFORMATION
Dim EchoReply As IP_ECHO_REPLY
Call WSAStartup(&H101, lpWSAdata)
If GetHostByName(HostName + String(64 - Len(HostName), 0)) <> SOCKET_ERROR Then
CopyMemory hHostent.h_name, ByVal GetHostByName(HostName + String(64 - Len(HostName), 0)), Len(hHostent)
CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
CopyMemory Address, ByVal AddrList, 4
End If
hFile = IcmpCreateFile()
If hFile = 0 Then
Internet = False
Exit Sub
End If
OptInfo.TTL = 255
If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then
rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
Else
Internet = False
End If
If EchoReply.Status = 0 Then
Internet = True
Else
Internet = False
End If
Call IcmpCloseHandle(hFile)
Call WSACleanup
End Function
-
Dec 25th, 2001, 11:35 AM
#10
PowerPoster
This is the example I was talking about. The ICMPEcho is used to ping to a certain server. Whenever you need to ascertain whether an internet connection just ping atleast two reliable servers(in case first one fails).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|