Results 1 to 10 of 10

Thread: Internet connection

  1. #1

    Thread Starter
    Hyperactive Member MikeBAM's Avatar
    Join Date
    Sep 2000
    Location
    Metro Detroit
    Posts
    284

    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?
    ~* )v( ! /< E *~

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    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

  3. #3

    Thread Starter
    Hyperactive Member MikeBAM's Avatar
    Join Date
    Sep 2000
    Location
    Metro Detroit
    Posts
    284
    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
    ~* )v( ! /< E *~

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    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.

  5. #5

    Thread Starter
    Hyperactive Member MikeBAM's Avatar
    Join Date
    Sep 2000
    Location
    Metro Detroit
    Posts
    284
    Nah, i got this one i think its pretty good thanks any way.

    This is what i used:

    VB Code:
    1. Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
    2.  
    3. Private Const FLAG_ICC_FORCE_CONNECTION = &H1
    4. '***************************************
    5.  
    6. Function InternetConnectionCheck(WebSite As String) As Boolean
    7.  
    8. If InternetCheckConnection(WebSite, FLAG_ICC_FORCE_CONNECTION, 0&) = 0 Then
    9. 'No connection
    10. InternetConnectionCheck = False
    11. Else
    12. 'There is a connection
    13. InternetConnectionCheck = True
    14. End If
    15.  
    16. End Function
    17. '*******************************************
    18. If InternetConnectionCheck("http://www.yahoo.com") = True Then
    19. MsgBox "You are online"
    20. Else
    21. MsgBox "You are offline"
    22. End If
    ~* )v( ! /< E *~

  6. #6
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    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.

  7. #7
    malc.net
    Guest

    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.

  8. #8
    Corwin
    Guest
    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.

  9. #9

    Thread Starter
    Hyperactive Member MikeBAM's Avatar
    Join Date
    Sep 2000
    Location
    Metro Detroit
    Posts
    284
    Hey amitabh, Hoes this? Does this do it?
    (i found problems with that old one now)

    VB Code:
    1. Const SOCKET_ERROR = 0
    2. Private Type WSAdata
    3.     wVersion As Integer
    4.     wHighVersion As Integer
    5.     szDescription(0 To 255) As Byte
    6.     szSystemStatus(0 To 128) As Byte
    7.     iMaxSockets As Integer
    8.     iMaxUdpDg As Integer
    9.     lpVendorInfo As Long
    10. End Type
    11. Private Type Hostent
    12.     h_name As Long
    13.     h_aliases As Long
    14.     h_addrtype As Integer
    15.     h_length As Integer
    16.     h_addr_list As Long
    17. End Type
    18. Private Type IP_OPTION_INFORMATION
    19.     TTL As Byte
    20.     Tos As Byte
    21.     Flags As Byte
    22.     OptionsSize As Long
    23.     OptionsData As String * 128
    24. End Type
    25. Private Type IP_ECHO_REPLY
    26.     Address(0 To 3) As Byte
    27.     Status As Long
    28.     RoundTripTime As Long
    29.     DataSize As Integer
    30.     Reserved As Integer
    31.     data As Long
    32.     Options As IP_OPTION_INFORMATION
    33. End Type
    34. Private Declare Function GetHostByName Lib "wsock32.dll" Alias "gethostbyname" (ByVal HostName As String) As Long
    35. Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSAdata As WSAdata) As Long
    36. Private Declare Function WSACleanup Lib "wsock32.dll" () As Long
    37. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    38. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
    39. Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean
    40. 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
    41.  
    42. Function Internet() As Boolean
    43. Const HostName = "www.aol.com"
    44.     Dim hFile As Long, lpWSAdata As WSAdata
    45.     Dim hHostent As Hostent, AddrList As Long
    46.     Dim Address As Long, rIP As String
    47.     Dim OptInfo As IP_OPTION_INFORMATION
    48.     Dim EchoReply As IP_ECHO_REPLY
    49.     Call WSAStartup(&H101, lpWSAdata)
    50.     If GetHostByName(HostName + String(64 - Len(HostName), 0)) <> SOCKET_ERROR Then
    51.         CopyMemory hHostent.h_name, ByVal GetHostByName(HostName + String(64 - Len(HostName), 0)), Len(hHostent)
    52.         CopyMemory AddrList, ByVal hHostent.h_addr_list, 4
    53.         CopyMemory Address, ByVal AddrList, 4
    54.     End If
    55.     hFile = IcmpCreateFile()
    56.     If hFile = 0 Then
    57.         Internet = False
    58.         Exit Sub
    59.     End If
    60.     OptInfo.TTL = 255
    61.     If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then
    62.         rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3))
    63.     Else
    64.         Internet = False
    65.     End If
    66.     If EchoReply.Status = 0 Then
    67.         Internet = True
    68.     Else
    69.         Internet = False
    70.     End If
    71.     Call IcmpCloseHandle(hFile)
    72.     Call WSACleanup
    73.  
    74. End Function
    ~* )v( ! /< E *~

  10. #10
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    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
  •  



Click Here to Expand Forum to Full Width