Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for internet connection constantly

How can I check for an internet connection constantly in my application and respond if the connection is not available?

Currently I am using:

while(true) {
 if(HasConnection()) {
     //doSomething..
  }
   //stop app by 1sec
}

but it seems rather inelegant.

like image 621
The Mask Avatar asked Jun 26 '11 03:06

The Mask


People also ask

Why do I keep having Internet connection issues?

Your internet keeps cutting out because you or your internet provider need to resolve one or more issues. For example, your modem may be faulty, your router may be out of date, or you may have too many devices using too much data simultaneously. Cables may be damaged. Network congestion may slow speeds.

Why does my internet keep flickering on and off?

If your internet connection keeps disconnecting and reconnecting, it could be due to a variety of factors. What is this? The most common cause is an unstable connection between your computer and the router. This can be caused by loose cables, incorrect router settings, or a problem with the router itself.


3 Answers

The accepted answer to this question on superuser describes the way the Windows determines if it has network access. You could use a similar method, but I would spawn a separate thread when your application starts that is responsible for doing the check. Have the separate thread perform the check in whatever manner you feel is the best and raise an event if the connection status changes.

like image 67
rsbarro Avatar answered Sep 20 '22 16:09

rsbarro


You're looking for the NetworkAvailabilityChanged event.

To check for internet connectivity, you can ping a reliable website, such as Google.com.

Note that it is not possible to be notified of every change in internet connectivity (such as an ISP outage).

like image 2
SLaks Avatar answered Sep 20 '22 16:09

SLaks


Use the following code:

public static class LocalSystemConnection
{
    [DllImport("wininet.dll", SetLastError=true, CallingConvention = CallingConvention.ThisCall)]
    extern static bool InternetGetConnectedState(out ConnectionStates lpdwFlags, long dwReserved);

    /// <summary>
    /// Retrieves the connected state of the local system.
    /// </summary>
    /// <param name="connectionStates">A <see cref="ConnectionStates"/> value that receives the connection description.</param>
    /// <returns>
    /// A return value of true indicates that either the modem connection is active, or a LAN connection is active and a proxy is properly configured for the LAN.
    /// A return value of false indicates that neither the modem nor the LAN is connected.
    /// If false is returned, the <see cref="ConnectionStates.Configured"/> flag may be set to indicate that autodial is configured to "always dial" but is not currently active.
    /// If autodial is not configured, the function returns false.
    /// </returns>
    public static bool IsConnectedToInternet(out ConnectionStates connectionStates)
    {
        connectionStates = ConnectionStates.Unknown;
        return InternetGetConnectedState(out connectionStates, 0);
    }

    /// <summary>
    /// Retrieves the connected state of the local system.
    /// </summary>
    /// <returns>
    /// A return value of true indicates that either the modem connection is active, or a LAN connection is active and a proxy is properly configured for the LAN.
    /// A return value of false indicates that neither the modem nor the LAN is connected.
    /// If false is returned, the <see cref="ConnectionStates.Configured"/> flag may be set to indicate that autodial is configured to "always dial" but is not currently active.
    /// If autodial is not configured, the function returns false.
    /// </returns>
    public static bool IsConnectedToInternet()
    {
        ConnectionStates state = ConnectionStates.Unknown;
        return IsConnectedToInternet(out state);
    }
}

[Flags]
public enum ConnectionStates
{
    /// <summary>
    /// Unknown state.
    /// </summary>
    Unknown = 0,

    /// <summary>
    /// Local system uses a modem to connect to the Internet.
    /// </summary>
    Modem = 0x1,

    /// <summary>
    /// Local system uses a local area network to connect to the Internet.
    /// </summary>
    LAN = 0x2,

    /// <summary>
    /// Local system uses a proxy server to connect to the Internet.
    /// </summary>
    Proxy = 0x4,

    /// <summary>
    /// Local system has RAS (Remote Access Services) installed.
    /// </summary>
    RasInstalled = 0x10,

    /// <summary>
    /// Local system is in offline mode.
    /// </summary>
    Offline = 0x20,

    /// <summary>
    /// Local system has a valid connection to the Internet, but it might or might not be currently connected.
    /// </summary>
    Configured = 0x40,
}
like image 2
NET3 Avatar answered Sep 20 '22 16:09

NET3