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. How to know programmatically if network cable is connected or not?

How to know programmatically if network cable is connected or not?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++sysadmintutorial
8 Posts 5 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.
  • A Offline
    A Offline
    Amarelia
    wrote on last edited by
    #1

    Hello Friends I am back again with a question. I have a dialog based MFC application in which I want to know whether my network cable is connected to my network card or not? Which function is best for this and how to use it? Or any other idea to do this? Regards, Mahesh

    D M B 3 Replies Last reply
    0
    • A Amarelia

      Hello Friends I am back again with a question. I have a dialog based MFC application in which I want to know whether my network cable is connected to my network card or not? Which function is best for this and how to use it? Or any other idea to do this? Regards, Mahesh

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Check out ISensNetwork. Also see here. In addition to a missing cable, there could other reasons for no connectivity.


      "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

      A 1 Reply Last reply
      0
      • A Amarelia

        Hello Friends I am back again with a question. I have a dialog based MFC application in which I want to know whether my network cable is connected to my network card or not? Which function is best for this and how to use it? Or any other idea to do this? Regards, Mahesh

        M Offline
        M Offline
        M Mehrdad M
        wrote on last edited by
        #3

        i read IP Address of NIC, if Network Cable is Unplugged, The Address is 127.127.127.0

        A 1 Reply Last reply
        0
        • A Amarelia

          Hello Friends I am back again with a question. I have a dialog based MFC application in which I want to know whether my network cable is connected to my network card or not? Which function is best for this and how to use it? Or any other idea to do this? Regards, Mahesh

          B Offline
          B Offline
          bob16972
          wrote on last edited by
          #4

          An ISP connection does not mean the network cable is plugged in but would provide you with similar capabilities. I am assuming you want to see if you have network connectivity which requires you take any IP capable connection into consideration. Checking the routing table for a default route is the best technique I've come across so far as it does not require checking a URL, pinging an IP address, or other techniques that will not be entirely accurate in all contexts. I got the idea from someone on CodeGuru before they upgraded their website and lost all the legacy comments. Unfortunately, I do not know the name of the person who passed on the idea so I can't give credit where credit is do but I'll pass it on... The function to use is GetIpForwardTable(). I use a timer to periodically check for connectivity and update my status indicator accordingly. There is a slight delay maybe a second or two if you unplug the cable or plug it in (assuming your timer is set to fire every second). No noticeable delay when disabling the connection using a GUI tool. Don't forget to include in StdAfx.h... #include // For Routing table query And make sure to reference the Platform SDK lib file... iphlpapi.lib BOOL CNetworkConnectionDlg::IsInternetAvailable(void) { MIB_IPFORWARDTABLE * pRoutingTable; DWORD dwBufferSize=0; BOOL bIsInternetAvailable=FALSE; DWORD dwResult; DWORD dwIndex; DWORD dwRowCount; // Get the required buffer size GetIpForwardTable(NULL,&dwBufferSize,FALSE); pRoutingTable=(MIB_IPFORWARDTABLE*)new BYTE[dwBufferSize]; // Attempt to fill buffer with routing table information dwResult=GetIpForwardTable(pRoutingTable,&dwBufferSize,FALSE); if (dwResult==NO_ERROR) { dwRowCount=pRoutingTable->dwNumEntries; // Get row count // Look for default route to gateway for (dwIndex=0;dwIndextable[dwIndex].dwForwardDest==0) { // Default route designated by 0.0.0.0 in table bIsInternetAvailable=TRUE; // Found it break; // Short circuit loop } } } delete pRoutingTable; // Clean up. Just say "No" to memory leaks return bIsInternetAvailable; }

          A B 2 Replies Last reply
          0
          • D David Crow

            Check out ISensNetwork. Also see here. In addition to a missing cable, there could other reasons for no connectivity.


            "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

            A Offline
            A Offline
            Amarelia
            wrote on last edited by
            #5

            Hi David Thankx a lot for spending time to look at my questions. With tons of thankx and regards, Mahesh

            1 Reply Last reply
            0
            • M M Mehrdad M

              i read IP Address of NIC, if Network Cable is Unplugged, The Address is 127.127.127.0

              A Offline
              A Offline
              Amarelia
              wrote on last edited by
              #6

              Hi Thankx a lot to you too...... U'r quote also helped me a lot. Mahesh

              1 Reply Last reply
              0
              • B bob16972

                An ISP connection does not mean the network cable is plugged in but would provide you with similar capabilities. I am assuming you want to see if you have network connectivity which requires you take any IP capable connection into consideration. Checking the routing table for a default route is the best technique I've come across so far as it does not require checking a URL, pinging an IP address, or other techniques that will not be entirely accurate in all contexts. I got the idea from someone on CodeGuru before they upgraded their website and lost all the legacy comments. Unfortunately, I do not know the name of the person who passed on the idea so I can't give credit where credit is do but I'll pass it on... The function to use is GetIpForwardTable(). I use a timer to periodically check for connectivity and update my status indicator accordingly. There is a slight delay maybe a second or two if you unplug the cable or plug it in (assuming your timer is set to fire every second). No noticeable delay when disabling the connection using a GUI tool. Don't forget to include in StdAfx.h... #include // For Routing table query And make sure to reference the Platform SDK lib file... iphlpapi.lib BOOL CNetworkConnectionDlg::IsInternetAvailable(void) { MIB_IPFORWARDTABLE * pRoutingTable; DWORD dwBufferSize=0; BOOL bIsInternetAvailable=FALSE; DWORD dwResult; DWORD dwIndex; DWORD dwRowCount; // Get the required buffer size GetIpForwardTable(NULL,&dwBufferSize,FALSE); pRoutingTable=(MIB_IPFORWARDTABLE*)new BYTE[dwBufferSize]; // Attempt to fill buffer with routing table information dwResult=GetIpForwardTable(pRoutingTable,&dwBufferSize,FALSE); if (dwResult==NO_ERROR) { dwRowCount=pRoutingTable->dwNumEntries; // Get row count // Look for default route to gateway for (dwIndex=0;dwIndextable[dwIndex].dwForwardDest==0) { // Default route designated by 0.0.0.0 in table bIsInternetAvailable=TRUE; // Found it break; // Short circuit loop } } } delete pRoutingTable; // Clean up. Just say "No" to memory leaks return bIsInternetAvailable; }

                A Offline
                A Offline
                Amarelia
                wrote on last edited by
                #7

                Hi Thankx thankx thankx thankx........I can't describe it in words... Any way have a nice time Mahesh

                1 Reply Last reply
                0
                • B bob16972

                  An ISP connection does not mean the network cable is plugged in but would provide you with similar capabilities. I am assuming you want to see if you have network connectivity which requires you take any IP capable connection into consideration. Checking the routing table for a default route is the best technique I've come across so far as it does not require checking a URL, pinging an IP address, or other techniques that will not be entirely accurate in all contexts. I got the idea from someone on CodeGuru before they upgraded their website and lost all the legacy comments. Unfortunately, I do not know the name of the person who passed on the idea so I can't give credit where credit is do but I'll pass it on... The function to use is GetIpForwardTable(). I use a timer to periodically check for connectivity and update my status indicator accordingly. There is a slight delay maybe a second or two if you unplug the cable or plug it in (assuming your timer is set to fire every second). No noticeable delay when disabling the connection using a GUI tool. Don't forget to include in StdAfx.h... #include // For Routing table query And make sure to reference the Platform SDK lib file... iphlpapi.lib BOOL CNetworkConnectionDlg::IsInternetAvailable(void) { MIB_IPFORWARDTABLE * pRoutingTable; DWORD dwBufferSize=0; BOOL bIsInternetAvailable=FALSE; DWORD dwResult; DWORD dwIndex; DWORD dwRowCount; // Get the required buffer size GetIpForwardTable(NULL,&dwBufferSize,FALSE); pRoutingTable=(MIB_IPFORWARDTABLE*)new BYTE[dwBufferSize]; // Attempt to fill buffer with routing table information dwResult=GetIpForwardTable(pRoutingTable,&dwBufferSize,FALSE); if (dwResult==NO_ERROR) { dwRowCount=pRoutingTable->dwNumEntries; // Get row count // Look for default route to gateway for (dwIndex=0;dwIndextable[dwIndex].dwForwardDest==0) { // Default route designated by 0.0.0.0 in table bIsInternetAvailable=TRUE; // Found it break; // Short circuit loop } } } delete pRoutingTable; // Clean up. Just say "No" to memory leaks return bIsInternetAvailable; }

                  B Offline
                  B Offline
                  Balon Fan
                  wrote on last edited by
                  #8

                  Great code, thank you a lot! :-D

                  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