6

アプリケーションでインターネット接続を常に確認し、接続が利用できない場合に応答するにはどうすればよいですか?

現在私は使用しています:

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

しかし、それはむしろエレガントではないようです。

4

11 に答える 11

3

スーパーユーザーに関するこの質問に対する受け入れられた回答は、Windows がネットワーク アクセスを持っているかどうかを判断する方法を説明しています。同様の方法を使用することもできますが、アプリケーションの開始時に、チェックを行う別のスレッドを生成します。別のスレッドに最適な方法でチェックを実行させ、接続ステータスが変化した場合はイベントを発生させます。

于 2011-06-26T03:41:38.017 に答える
2

次のコードを使用します。

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,
}
于 2012-04-30T14:59:04.290 に答える
2

NetworkAvailabilityChangedあなたはイベントを探しています。

インターネット接続を確認するには、Google.com などの信頼できる Web サイトにpingを実行します。

インターネット接続のすべての変更 (ISP の停止など)を通知できるわけではないことに注意してください。

于 2011-06-26T03:38:26.103 に答える
1

少なくとも 1 つの接続が利用可能かどうかだけを知りたい場合は、次の方法を試してください。

InternetGetConnectedStateEx()

http://msdn.microsoft.com/en-us/library/aa384705%28v=vs.85%29.aspx

于 2011-06-26T03:52:45.210 に答える
0

インターネットに接続しているかどうかはどうやってわかりますか? 近くのルーターにパケットをルーティングできれば十分ですか? おそらく、マシンには 1 つの NIC と 1 つのゲートウェイしかなく、そのゲートウェイの接続がダウンしても、マシンは引き続きゲートウェイとローカル ネットワークにルーティングできるでしょうか?

おそらくマシンには 1 つの NIC と 12 のゲートウェイがあります。多分彼らはいつも行き来しますが、そのうちの1人は常に稼働していますか?

マシンに複数の NIC があり、ゲートウェイが 1 つしかない場合はどうなりますか? おそらく、インターネットの一部のサブセットにルーティングできますが、インターネットに接続されていないローカル ネットワークへの接続は優れていますか?

マシンに複数の NIC と複数のゲートウェイがあるが、管理ポリシー上の理由から、依然としてインターネットの一部のみがルーティング可能である場合はどうなるでしょうか?

クライアントがサーバーに接続しているかどうかだけを本当に気にしますか?

パケット間の遅延はどの程度許容できますか? (30 ミリ秒は適切です。300 ミリ秒は人間の持久力の限界を押し上げています。3000 ミリ秒は長い間耐えられません。ソーラー プローブへの接続には 960000 ミリ秒が必要です。) どのような種類のパケット損失が許容されますか?

あなたは本当に何を測定しようとしていますか?

于 2011-06-26T03:35:15.370 に答える
0

これは出発点ですが、sarnold が述べたように、考慮しなければならないことがたくさんあります。

于 2011-06-26T03:37:00.770 に答える
0

次のような Web サイトに ping を実行して、インターネット接続をテストできます。

    public bool IsConnectedToInternet
    {
        try
        {
            using (System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping())
            {
                string address = @"http://www.google.com";//                        System.Net.NetworkInformation.PingReply pingReplay = ping.Send(address);//you can specify timeout.
                if (pingReplay.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    return true;
                }
             }
        }
        catch
        {
#if DEBUG
            System.Diagnostics.Debugger.Break();
#endif//DEBUG
        }

        return false;
    }
于 2011-06-26T03:50:28.653 に答える