如何在我的應用程序中不斷檢查互聯網連接,並在連接不可用時作出響應?不斷檢查互聯網連接
目前我使用:
while(true) {
if(HasConnection()) {
//doSomething..
}
//stop app by 1sec
}
,但它似乎相當不雅。
如何在我的應用程序中不斷檢查互聯網連接,並在連接不可用時作出響應?不斷檢查互聯網連接
目前我使用:
while(true) {
if(HasConnection()) {
//doSomething..
}
//stop app by 1sec
}
,但它似乎相當不雅。
如果您有Internet連接,您將如何知道?你可以將數據包路由到附近的路由器就足夠了嗎?也許機器只有一個網卡,一個網關,並且可能該網關的連接斷開,但機器仍然可以路由到網關和本地網絡?
也許機器有一個NIC和十幾個網關;也許他們一直來去,但其中一個他們總是起來?
如果機器有多個NIC,但只有一個網關會怎麼樣?也許它可以路由到互聯網的一些子集,但仍然與本地網絡沒有連接到Internet的良好連接?
如果機器具有多個網卡,多個網關,但出於管理策略的原因,只有部分互聯網是可路由的呢?
你真的只關心客戶是否連接到你的服務器?
數據包之間有什麼樣的延遲是可以接受的? (30ms是好的,300ms正在推動人體耐力的極限,3000ms是無法忍受的長時間,960000ms是連接太陽能探測器所需要的。)什麼樣的數據包丟失是可以接受的?
你在做什麼真的試圖測量?
This將是一個開始,但作爲sarnold提到有很多你需要考慮
你正在尋找的東西NetworkAvailabilityChanged
event。
要檢查互聯網連接,你可以ping一個可靠的網站,如Google.com。
請注意,每隔互聯網連接(如ISP中斷)的更改都不可能被通知。
超級用戶對this question的接受答案描述了Windows確定其是否具有網絡訪問權的方式。你可以使用類似的方法,但是當你的應用程序啓動時,我會產生一個單獨的線程來負責檢查。讓單獨的線程以您認爲最好的方式執行檢查,並在連接狀態更改時提出事件。
+1:這裏是API的鏈接:[Windows Vista和Windows 7中的網絡意識](http://msdn.microsoft.com/zh-cn/library/ee264321(v = vs.85).aspx),這裏是一篇CodeProject文章:[如何使用Windows NLM API獲取新的網絡連接通知](http://www.codeproject.com/KB/IP/usenetworklist.aspx)。 –
@Rick Sladkey:很好,謝謝你的鏈接! – rsbarro
您可以通過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;
}
如果你只需要知道,如果至少一個連接可用,你可以試試這個:
InternetGetConnectedStateEx()
http://msdn.microsoft.com/en-us/library/aa384705%28v=vs.85%29.aspx
使用下面的代碼:
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,
}
即使網絡是「up」,這並不能保證互聯網可用 –
@Daniel:是的;我正在接近這一點。 – SLaks