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

Hybrid View

  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,757
    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 2004
    Posts
    16

    Re: Detecting an internet connection.

    The online function always return true though i am working in offline. Please tell me what is problem. I aam connected to Internet by LAN.

    Quote Originally Posted by kochhar
    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.

  6. #6
    Join Date
    Mar 2005
    Posts
    1

    Re: Detecting an internet connection.

    Hi,

    ran into this problem of having to detect the presence of an internet connection, found this thread and decided to post my VERY unsatisfying, but working, 'solution'. Especially the part where it needs a batch file is extremely annoying.
    Moreover, as discussed in this thread, this 'solution' obviously doesn't work from behind routers that disabled pinging.

    cheers,
    Dennis

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <process.h>
    
    int main(int argc, char* argv[])
    {
        FILE *f1;
        int check;
        char fbuf[150];
    
        // try to ping 66.102.11.104 (www.google.nl), just ONCE, waiting for MAX 500 msec
        // batch file needed because ping doesn't take "> testIC.bat" for an argument
        check = spawnl( P_WAIT, "c:\\windows\\testIC.bat", "testIC.bat", "500", "1", 
        "66.102.11.104", "c:\\windows\\testIC.tmp", NULL );
        if( check == -1 )
            // error from spawnl
            return -1; 
    
        f1 = fopen( "c:\\windows\\testIC.tmp", "rb" );
        if( !f1 )
            // error opening file
            return -1; 
    
        // read the first 149 bytes from testIC.tmp
        fread( fbuf, 149, 1, f1 ); 
        fbuf[149] = NULL;
        fclose( f1 );
        if( strstr( fbuf, "Reply from" ) != NULL )
            // reply received, internet connection viable
            return 1;   
    
        // no reply received, assume internet connection lost
        return 0;    
    }
    testIC.bat just reads:
    Code:
    ping -w %1 -n %2 %3 > %4

    _

  7. #7
    Join Date
    Feb 2004
    Posts
    4

    Re: Detecting an internet connection.

    You should try the DNS name resolution instead of ping (gethostbyname). That will probably not be disabled by such routers.

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

  9. #9
    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.
    ...

  10. #10
    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..

  11. #11
    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.
    ...

  12. #12
    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.

  13. #13
    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.
    ...

  14. #14
    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.

  15. #15
    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

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
  •  





Click Here to Expand Forum to Full Width

Featured