Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Detecting when an internet connection is active

Detecting when an internet connection is active

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fferland
    wrote on last edited by
    #1

    I need to know how I can detect whether the user currently has an active internet connection. Can anyone help? I've tried InetIsOffline(), but that only determines if the user is in online or offline mode, not if the actual connection is currently active.

    V T 2 Replies Last reply
    0
    • F fferland

      I need to know how I can detect whether the user currently has an active internet connection. Can anyone help? I've tried InetIsOffline(), but that only determines if the user is in online or offline mode, not if the actual connection is currently active.

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      One solution is to simply connect to a server. Kuphryn

      1 Reply Last reply
      0
      • F fferland

        I need to know how I can detect whether the user currently has an active internet connection. Can anyone help? I've tried InetIsOffline(), but that only determines if the user is in online or offline mode, not if the actual connection is currently active.

        T Offline
        T Offline
        Todd Smith
        wrote on last edited by
        #3

        bool IsConnectedToInternet(void)
        {
        DEBUGF("IsConnectedToInternet");

        WORD wVersionRequested = MAKEWORD(1, 1);
        WSADATA	wsaData;
        TCHAR hostaname\[1000\];
        int	hostname\_size = sizeof(hostaname);
        bool connected = false;
        
        if (WSAStartup(wVersionRequested, &wsaData) != 0)
        {
        	DEBUGF("WSAStartup Failed (%i.%i) %s",
        		wsaData.wHighVersion, wsaData.wVersion, wsaData.szDescription);
        	return connected;
        }
        
        if (gethostname(hostaname, hostname\_size) != SOCKET\_ERROR)
        {
        	struct hostent FAR \*h = gethostbyname(hostaname);
        	if (h != NULL)
        	{
        		// cif ip=127.0.0.1 then we are not connected
        		DEBUGF("gethostname %s %s", h->h\_name, h->h\_addr\_list\[0\]);
        		if (strncmp(h->h\_addr\_list\[0\], "\\x7f\\x0\\x0\\0x1", 4)!=0)
        		{
        			connected = true;
        		}
        	}
        }
        else
        {
        	DEBUGF("gethostname Failed");
        }
        
        WSACleanup();
        
        return connected;
        

        }

        Todd Smith

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups