How to check if you have a live internet connection programmatically using C ++

How to check if I have an internet connection or an internet connection using C ++?

+6
c ++ connection
source share
3 answers

C ++ has no built-in functions for this, you will need to access the system APIs. The easiest and most understandable way is to create a socket and try to connect it to a known IP address or check if DNS is working.

Some useful links:
http://msdn.microsoft.com/en-us/library/ms740673(VS.85).aspx (Windows Sockets)
http://www.tenouk.com/cnlinuxsockettutorials.html (Linux / Unix sockets)

+4
source share

The easiest way is to try connecting to a known external IP address. If this fails on Windows, the connect function will return SOCKET_ERROR, and WSAGetLastError will usually return WSAEHOSTUNREACH (this means that the packet cannot be sent to the host). On Linux, you get -1, and errno will be ENETUNREACH.

+2
source share

To get started, you can sign up for ISensIntf to check if you have a working network connection. (Let me know if you need help with this. It hurts to register for events, etc.). After that, you can use Api, such as IsNetworkAlive , InternetGetConnectedStateEx or InternetCheckConnection , to check your Internet connection, etc.

If you are using C # or VB, first add a link to

 Microsoft.VisualBasic.Code. Microsoft.VisualBasic.Devices.Network network = new Microsoft.VisualBasic.Devices.Network(); network.NetworkAvailabilityChanged += new Microsoft.VisualBasic.Devices.NetworkAvailableEventHandler(network_NetworkAvailabilityChanged); ... private static void network_NetworkAvailabilityChanged(object sender, Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs e) { if (e.IsNetworkAvailable) { //network is connected.. do something.. } else { //network isnt connected.. do something else. } 

Hope this helps

-one
source share

All Articles