Detecting an internet connection.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 8 1234 ... LastLast
Results 1 to 15 of 111

Thread: Detecting an internet connection.

  1. #1
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520

    Detecting an internet connection.

    Hi

    My application connects to the internet to check for updates.

    It does so using functions in ras32.dll (RasGetConnectStatus)
    If ras32.dll does not exist, I use win inet functions (CInternetSession::OpenURL)

    Two problems :

    1. If not connected, the wininet functions take a while to time out. I always call it in a thread which i timeout after 5 seconds, but still it's not perfect.

    2. RAS functions only work when an internet dialup is present. If you have cable modem / router / lan, RAS will always return "not connected", even if you are.

    I read a newsgroup posting from a microsoft guy who suggested also using IsNetworkAlive() and IsDestinationReachable(), but both of those fail for me, regardless of connection state.

    I have writtten a class which works well for dialup connection. But in the case of "always on" connections, I have yet to find a good method for detection of internet availability.

    Any suggestions guys ?

    Jase

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  2. #2
    Join Date
    Feb 2002
    Posts
    5,758
    Winsock WSAConnect() sometimes takes five or more seconds to before returning. One solution is to manually close the socket. Thus, as for your question there is no solution.

    Kuphryn

  3. #3
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    There must be a way. How do internet browsers know whether or not you are connected to the net ? There absolutley has to be a fool proof way, and it's probably undocumented ...

    Jase

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  4. #4
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    I've been using the following function with success. It returns
    TRUE if a connection is available, and takes less than
    a sec in either case:

    Code:
    BOOL Online()
    {
        DWORD dwState = 0; 
        DWORD dwSize = sizeof(DWORD);
    	 return InternetQueryOption(NULL,
    		 INTERNET_OPTION_CONNECTED_STATE, &dwState, &dwSize)
    		 && (dwState & INTERNET_STATE_CONNECTED);
    }
    Credit: I got this from Paul Dilascia's column in MSDN mag a while
    back.

  5. #5
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    Nope

    It returns connected even when i'm not ...

    Next !

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  6. #6
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    Bump!

    Anybody ?

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  7. #7
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    Originally posted by jase jennings
    Nope

    It returns connected even when i'm not ...
    What platform are you testing this on? I tried my function
    both at work (Win/XP, VS v6, ethernet (twiested pair))
    and at home (Win/XP, VS v6, modem) and every time it
    returned the correct answer within a second or less.

    My ethernet card (actually it's built into the motherboard)
    is an "Intel(R) PRO/100 VE". Maybe that makes a difference..

  8. #8
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    > What platform are you testing this on?

    XP Pro, VS.NET, Cable modem (always on)

    I've no doubt your function works fine with a dial up modem, but not when you are using a router / lan

    Jase

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  9. #9
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by jase jennings
    > What platform are you testing this on?

    XP Pro, VS.NET, Cable modem (always on)

    I've no doubt your function works fine with a dial up modem, but not when you are using a router / lan

    Jase
    I assume you just want to check connectivity, so if you want a fast time out. Ping the destination first.

  10. #10
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    Do you have an example of a ping() function ?
    I don't want to execute ping through the shell if i can help it.

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  11. #11
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by jase jennings
    Do you have an example of a ping() function ?
    I don't want to execute ping through the shell if i can help it.
    hmm should be something like this, for a shorter condensed version.., I'll pull it outta one of my classes and see if I can shorten it into a small console app in a couple of minutes...

    Code:
       HANDLE icmphandle = ::IcmpCreateFile();
    
    	    char reply[sizeof(icmp_echo_reply)+8];
    
    	    
    	    DWORD dw = IcmpSendEcho(icmphandle,
            *((u_long*)hostinfo->h_addr_list[0]),
    		    0,0,NULL,
    		    reply,sizeof(icmp_echo_reply)+8,1000);
    	    
    
    	    IcmpCloseHandle(icmphandle);
    
            if (dw == 0)
    You'll need the SDK, the ICMP files are not in the standared include/lib dirs, it's in SDK\misc\icmp\xxx

    But here is a link that looks ok, for all the long winded code...

    http://www.sockets.com/ms_icmp.htm

    You just want to look for the Icmp calls above, on google, probably some samples on codeguru 2.
    Last edited by Mick; August 6th, 2003 at 10:53 AM.

  12. #12
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    Originally posted by jase jennings
    I've no doubt your function works fine with a dial up modem, but not when you are using a router / lan
    I tested as follows:
    (1) Ethernet cable connected: returned TRUE
    (2) Disconnect cable: returnd FALSE
    (3) Reconnected cable: returned TRUE

    I suppose there must be cases where it fails, but I don't
    know what they are or why.

    I am attaching a small program that shows how to ping
    from your program. Most of the code was posted by
    Steve Bryndin at Codeguru. I just modified in small ways.
    Note that it does a lot more than just ping a number, e.g.
    you can ask it to ping by name, etc. But it works well.

    Good luck.
    Attached Files Attached Files

  13. #13
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    Thanks Mick and Kochar

    Kochar :

    I have a router which my cable modem is connected to. The internet connection is constantly fed to the router. The router then connects two pcs on a home network, thus sharing the cable modem between both pcs.

    If i bring the router down, and therefore the network, your original example still returns "connected"
    If i bring the cable mode down, but leave the router up, your original code still displays connected.

    In either case, the internet is not available.

    It may work for you, but be advised that if your ousers are using a similar set up to mine, your code may not work as intended.

    Jase

    www.slideshowdesktop.com
    View your images and photos on your desktop with ease using SlideShow Desktop, the desktop wallpaper manager for Microsoft Windows.
    ...

  14. #14
    Join Date
    Aug 1999
    Location
    Wisconsin
    Posts
    507
    Originally posted by jase jennings
    If i bring the router down, and therefore the network, your original example still returns "connected"
    If i bring the cable mode down, but leave the router up, your original code still displays connected.
    Interesting, thanks. I will try a similar test to check.
    Hope the ping sample did the trick.

  15. #15
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by kochhar
    Interesting, thanks. I will try a similar test to check.
    Hope the ping sample did the trick.
    There is a ton of code out there that uses ping to test connectivity before connection, it's even more useful with COM...Older OS/2 ip stacks were fairly unstable so ping was the option of choice before the socket connection.

    /ICMP...it's not just for dinner anymore.

Page 1 of 8 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center