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. Internet connection detection

Internet connection detection

Scheduled Pinned Locked Moved C / C++ / MFC
8 Posts 4 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.
  • M Offline
    M Offline
    Mr_Byte
    wrote on last edited by
    #1

    well I don't know much about internet programming but I need my application to do some action when the user connects to the internet in other words * while the pragram is running when connected fire some event(or simply call afunction) when connection terminated do othe action thanks for your time

    P RaviBeeR 2 Replies Last reply
    0
    • M Mr_Byte

      well I don't know much about internet programming but I need my application to do some action when the user connects to the internet in other words * while the pragram is running when connected fire some event(or simply call afunction) when connection terminated do othe action thanks for your time

      P Offline
      P Offline
      Peter Occil
      wrote on last edited by
      #2

      This function determines whether the user is connected to at least one RAS connection. Link with rasapi32.lib. You can use this function to start your program.

      #include <windows.h>
      #include <ras.h>

      BOOL IsUserConnected(){
      RASCONN *rasc=malloc(1000);
      DWORD bs,nc,i,j,dwRet;
      RASCONNSTATUS rcs;
      MSG msg;
      TCHAR s[257];
      rcs.dwSize=160;
      rasc->dwSize=412;
      dwRet=RasEnumConnections(rasc,&bs,&nc);
      if(!bs)return 0;
      rasc=realloc(rasc,bs);
      if(RasEnumConnections(rasc,&bs,&nc))
      return 0;
      for(i=0;i<bs/412;i++){
      HRASCONN hrc=rasc[i].hrasconn;
      dwRet = RasGetConnectStatus(hrc, &rcs);
      if (dwRet != ERROR_INVALID_HANDLE)
      continue;
      if (dwRet != ERROR_NOT_ENOUGH_MEMORY){
      SetLastError(dwRet);
      return 0;
      }
      if(rcs.rasconnstate==RASCS_Connected)
      return 1;
      }
      return 0;
      }

      Peter O.

      1 Reply Last reply
      0
      • M Mr_Byte

        well I don't know much about internet programming but I need my application to do some action when the user connects to the internet in other words * while the pragram is running when connected fire some event(or simply call afunction) when connection terminated do othe action thanks for your time

        RaviBeeR Offline
        RaviBeeR Offline
        RaviBee
        wrote on last edited by
        #3

        Also see InternetGetConnectedState(). You can call it in a timer handler and fire events when the connection state changes. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com

        L 1 Reply Last reply
        0
        • RaviBeeR RaviBee

          Also see InternetGetConnectedState(). You can call it in a timer handler and fire events when the connection state changes. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com

          L Offline
          L Offline
          l a u r e n
          wrote on last edited by
          #4

          hi ravi this function puzzles me cos it seems to fail when u are connecting thru a shared connection across a local network ... ie, it always thinks ur connected even if the connecting (server) machine isnt actually connected have i got something very wrong?


          "... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
          sonork: 100.18128   8028finder.com

          RaviBeeR 1 Reply Last reply
          0
          • L l a u r e n

            hi ravi this function puzzles me cos it seems to fail when u are connecting thru a shared connection across a local network ... ie, it always thinks ur connected even if the connecting (server) machine isnt actually connected have i got something very wrong?


            "... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
            sonork: 100.18128   8028finder.com

            RaviBeeR Offline
            RaviBeeR Offline
            RaviBee
            wrote on last edited by
            #5

            Ugh, thank you Microsoft. :( I haven't tried it in that mode. It seems to work when you're connected thru a LAN or dialed in via a modem. Perhaps Peter's RAS approach is more robust? /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com

            L 1 Reply Last reply
            0
            • RaviBeeR RaviBee

              Ugh, thank you Microsoft. :( I haven't tried it in that mode. It seems to work when you're connected thru a LAN or dialed in via a modem. Perhaps Peter's RAS approach is more robust? /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com

              L Offline
              L Offline
              l a u r e n
              wrote on last edited by
              #6

              i dont really blame them for bad api functionality cos the only reliable way i found was to try a ping on the server before assuming a connection was actually there ... i found a timeout of 10 seconds gives a fair compromise between a user waiting too long and the net being a little slow if there is a better way i would like to hear it


              "... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
              sonork: 100.18128   8028finder.com

              RaviBeeR 1 Reply Last reply
              0
              • L l a u r e n

                i dont really blame them for bad api functionality cos the only reliable way i found was to try a ping on the server before assuming a connection was actually there ... i found a timeout of 10 seconds gives a fair compromise between a user waiting too long and the net being a little slow if there is a better way i would like to hear it


                "... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
                sonork: 100.18128   8028finder.com

                RaviBeeR Offline
                RaviBeeR Offline
                RaviBee
                wrote on last edited by
                #7

                You're right. But testing for a ping needs to be done in a separate thread (i.e. in a non-blocking manner) for it to be useful. I can't think of a foolproof and efficient way to do this. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com

                L 1 Reply Last reply
                0
                • RaviBeeR RaviBee

                  You're right. But testing for a ping needs to be done in a separate thread (i.e. in a non-blocking manner) for it to be useful. I can't think of a foolproof and efficient way to do this. /ravi Let's put "civil" back into "civilization" http://www.ravib.com ravib@ravib.com

                  L Offline
                  L Offline
                  l a u r e n
                  wrote on last edited by
                  #8

                  thats why i use a timeout of 10 seconds ... a klunk of a compromise but at least sanity shows its face occasionally :)


                  "... and so i said to him ... if it don't dance (or code) and you can't eat it either f**k it or throw it away"
                  sonork: 100.18128   8028finder.com

                  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