How to know programmatically if network cable is connected or not?
-
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
-
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
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
-
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
i read IP Address of NIC, if Network Cable is Unplugged, The Address is 127.127.127.0
-
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
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; }
-
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
-
i read IP Address of NIC, if Network Cable is Unplugged, The Address is 127.127.127.0
-
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; }
-
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; }