Detecting an internet connection. - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 8 FirstFirst 123456 ... LastLast
Results 31 to 45 of 111

Thread: Detecting an internet connection.

  1. #31
    Join Date
    Jul 2003
    Location
    Stuttgart, Germany
    Posts
    45
    Hi Jase,

    I ran into precisely the same problems when trying to do precisely the same thing.
    And I bet there are several other guys out there trying to add a "check for updates" feature to their applications.

    'just want to motivate you to post an article on this topic as soon as you found a solution.

    Take care,
    Peter
    Solutions in the embedded- software area:
    www.gliwa.com

  2. #32
    Join Date
    Oct 1999
    Location
    Missouri, USA
    Posts
    453
    1. "Not Connected" takes too long to time out
    Using a timer set to timeout at 1 and half seconds would help.
    If the time elapses - no internet connection.

    In fact, it willl continue always to say i am connected until i close the test dlg and run it again.
    When testing my code, I found that I must call WSAStartup(),
    WSAAsyncGetHostByName(), and then WSACleanup()
    upon completion of the connection test.

    Including AfxSocketInit() dosn't call WSACleanup() until the app
    is closed - thus causing an incorrect result in the example that
    you mentioned.
    If you need an answer, don't forget to try this link:
    http://www.codeguru.com/forum/search.php?
    __________________

  3. #33
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    Hi Peter

    You're absolutely right, and of course when i come across a fail safe (or somewhere close to) method, you can be sure I will write an article on it.

    You would expect this to be a fairly simple procedure given that the internet forms an essential line of communications for todays software.

    You can bet there is a simple api call to ascertain connection state, but most likely kept completely secret by microsoft

    There are many ways of determining connected state, and many api's for this purpose. But not of them is reliable in its own right. I somehow think the solution will be a mixture of a number of api calls and socket functions.

    It also seems to be the case that with most of the api's, if the connection state changes while your application is runing, they will continue to return the initial result from the first call, so if connected at startup, it will always return connected, even if you hang up. Perhaps a solution may be to call an external application/dll to ascertain the connected state, then send the result to your application. The external app/dll then exits - so when invoked again it may hopefully give the correct result.

    Dunno, just thinking out loud.

    Anybody want to make a "project" of this ? I'm sure if a few of use get our heads together, collectively we can find a solution.

    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. #34
    Join Date
    May 2003
    Location
    Pakistan
    Posts
    223
    Well after reading all these reply ..I Have come to know that it is easy with dialup connection...But difficult with Cable Modem.
    But my question is that if cable modem is directly connected to my computer then will RASEnumConnection will work or not.My Emphasis is on"IF MY CABLE MODEM IS DIRECTLY CONNECTED TO MY COMPUTER"

    And is there someway of gettting some event on this.
    cauz checking the connection after sometimes is not a good solution.

  5. #35
    Join Date
    Oct 2002
    Posts
    27
    With this two functions.
    RasEnumConnections
    RasGetConnectStatus
    When ras is not installed,
    your application won't start
    under Winnt, so you have to
    address the ras dll your self.
    It also should detect DSL connections.
    In order to find out which Phone book entrie it is,
    which is connected, you have to match this against
    RasEnumEntries()



    #undef RasEnumConnections
    DWORD RasEnumConnections(
    LPRASCONN lprasconn,
    LPDWORD lpcb,
    LPDWORD lpcConnections
    );
    DWORD RasEnumConnections(
    LPRASCONN lprasconn,
    LPDWORD lpcb,
    LPDWORD lpcConnections
    ){

    CString name("RasEnumConnectionsA");
    DWORD ret;


    typedef int (FAR WINAPI *RASENUMCONNECTIONSFARPROC)(
    LPRASCONN lprasconn,
    LPDWORD lpcb,
    LPDWORD lpcConnections
    );

    RASENUMCONNECTIONSFARPROC address;

    if(!GetRasDll()){
    return GetLastError();
    }
    address=(RASENUMCONNECTIONSFARPROC)GetProcAddress(RasDll,name);
    if(!address){
    ret=GetLastError();
    return ret;
    }
    ret=(address)(
    lprasconn,
    lpcb,
    lpcConnections
    );

    return ret;

    }
    #undef RasGetConnectStatus
    DWORD RasGetConnectStatus(
    HRASCONN hrasconn,
    LPRASCONNSTATUS lprasconnstatus
    );

    DWORD RasGetConnectStatus(
    HRASCONN hrasconn,
    LPRASCONNSTATUS lprasconnstatus
    ){
    ;
    CString name("RasGetConnectStatusA");
    DWORD ret;


    typedef int (FAR WINAPI *RASGETCONNECTSTATUSFARPROC)(
    HRASCONN hrasconn,
    LPRASCONNSTATUS lprasconnstatus
    );

    RASGETCONNECTSTATUSFARPROC address;

    if(!GetRasDll()){
    return GetLastError();
    }
    address=(RASGETCONNECTSTATUSFARPROC)GetProcAddress(RasDll,name);
    if(!address){
    ret=GetLastError();
    return ret;
    }
    ret=(address)(
    hrasconn,
    lprasconnstatus
    );

    return ret;
    }

  6. #36
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    Thanks for your post Surgeonde

    Unfortunately, I already use these RAS api's in the way that you describe.

    If the ras.dll is not available, I use wininet functions as a backup.

    The problem with the ras a pproach (in fact, it seems to be a common problem amongst all methods), is that if i disconnect during the lifetime of the program which checks the connection, it will continue to tell me that i am connected. This is certainly true when using a cable modem / router. If i disconnect the cable between my nic and my router (therefore losing all network and internet connectivity), those RAS functions will tell me i am still connected

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

  7. #37
    Join Date
    Jan 2003
    Posts
    24
    why dont ya use the InternetGetConnectedState(,) function
    just don't forget to include the wininet.h file and to link with the wininet.dll file here is a simple code i wrote as an ex.




    DWORD connection;//connection parameters return as if lan or dial
    //up connected see the msdn

    if(InternetGetConnectedState(&connection,0))
    {
    m_bConnected=TRUE;//set our status to connected
    }
    else//we are not connected
    {
    m_bConnected=FALSE;//set our connected status to false
    }


    U can Call the internetgetconnectedstate function many times and it works just fine for me in a win98 pc



    Hope it works fine !!

  8. #38
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    Originally posted by leyusha
    why dont ya use the InternetGetConnectedState(,) function
    ... because it doesn't work !

    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. #39
    Join Date
    Jan 2003
    Posts
    24
    what Do you mean by it doesn't work it won't link or what?

  10. #40
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    InternetGetConnectedState() is unreliable.

    Just having a dialup connection configured on your pc is often enough for IGCS() to tell you are connected - doesn't matter whether or not you are actually connected.

    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. #41
    Join Date
    Apr 2001
    Location
    San Diego CA
    Posts
    378
    I used this code and it worked on a DSL connection..
    I don't remember where I got it from ( proablly codeguru )

    Code:
    #include <Afxsock.h>
    
    BOOL CYourDlg::IsInternetConnected (void)
    {
     int nCheck = AfxSocketInit();
     DWORD dwDial = 0;
     DWORD dwDialSize = sizeof( DWORD );
    
     // if autodial is on then turn it off temporarily.
     HKEY hKey;
     if( RegOpenKeyEx( HKEY_CURRENT_USER,
    	_T("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"), 0,
    	KEY_SET_VALUE, &hKey ) == ERROR_SUCCESS )
     {
    	LONG lRet = RegQueryValueEx( hKey, _T("EnableAutodial"), NULL, NULL,
    	(BYTE*)&dwDial, &dwDialSize ) ;
    	if (lRet == ERROR_SUCCESS )
    	{
    		if( dwDial )
    		{
    			DWORD dw = 0;
    			VERIFY( RegSetValueEx( hKey, _T("EnableAutodial"), NULL, REG_DWORD,
    			(BYTE*)&dw, dwDialSize ) == ERROR_SUCCESS );
    		}
    	}
        else
    	{// --- add this in order to view potential errors ---
    		LPVOID lpMsgBuf;
    		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
    		FORMAT_MESSAGE_FROM_SYSTEM,
    		NULL,
    		GetLastError(),
    		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    		(LPTSTR) &lpMsgBuf,
            0,
    		NULL);
    		TRACE("GetComputerName() failed %d\n  Message: %s\n", GetLastError(),
    		lpMsgBuf);
    		// Process any inserts in lpMsgBuf.
    		// ...
    		// Display the string.
    		// AfxMessageBox((LPCTSTR)lpMsgBuf, MB_OK | MB_ICONINFORMATION );
    		// Free the buffer.
    		LocalFree( lpMsgBuf );
    	}
     }
    
    BOOL bResult = true;
    CSocket m_Server;
    
    if ( !m_Server.Create() )
     	// m_sError = _T( "Unable to create the socket." );
    	bResult = false;   
    else if ( !m_Server.Connect( "www.microsoft.com", 80 ) ) 
    {	// You can use www.microsoft.com in order to check whether DNS is available
    	//m_sError = _T( "Unable to connect to server" );        
    	m_Server.Close();
    	bResult = false;
    }
     
     
    // if we turned off autodial then turn it back on again.
    if ( dwDial )
    	VERIFY( RegSetValueEx( hKey, _T("EnableAutodial"), NULL, REG_DWORD,
    	(BYTE*)&dwDial, dwDialSize ) == ERROR_SUCCESS );
    
    (void)RegCloseKey( hKey );
    
    return ( bResult );
    }

  12. #42
    Join Date
    Dec 2002
    Posts
    437
    i read this post and think it the most detailed one this subject.
    i want to check the state of the internet........ the alogrithm that something like what voidspace has put forward only thing i will write my own socket with connection timeout.
    jenis i think u have done a lot of R&D on the subject please suggest if it the right approach. As i donot have much of the testing environment and also donot want to reinvent the wheeel again.

  13. #43
    Join Date
    Mar 2000
    Location
    Birmingham, England
    Posts
    2,520
    i think u have done a lot of R&D on the subject please suggest if it the right approach. As i donot have much of the testing environment and also donot want to reinvent the wheeel again.
    ... reinventing the wheel is only a bad thing if you know what a wheel looks like and how it operates in the first place. The problem with this internet connection wheel is that nobody yet published how to make it, and it's not visible to copy

    For sure there are many different ways of tackling this one, reading this thread from the start gives a pretty thorough list. The trick i, i believe, is to create a solution which combines these methods in a smart way.

    The ping/timeout issue is just no good to me - I'm not prepared to wait. The wininet functions are not reliable. The RAS functions are not reliable. gethostbyname is not reliable. none of it seems reliable But there must be a magic method, because every time i fire up internet explorer, it knows instantly if i am online or not, regardless of my connection type.

    I'm still searching for the holy grail. If any other contributors would like to step forward and share some ideas, I'd love to hear them

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

  14. #44
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by jase jennings
    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
    If you do discover a "fool proof way", then the experts probably are interested in knowing about it. See the How do I detect if there is an Internet connection? question and answer in the Winsock Programmer's FAQ: Intermediate Winsock Issues.

  15. #45
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882
    Hey guys,
    I am worried about my code , I have used InternetGetConnectedState function from WinInet library.
    Can you tell me where does it fail to detect internet connection ?
    Regards,
    Ramkrishna Pawar

Page 3 of 8 FirstFirst 123456 ... 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center