c#判斷網(wǎng)絡(luò)連接狀態(tài)
運行效果
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
string url = "www.baidu.com;www.sina.com;www.cnblogs.com;www.google.com;www.163.com;www.csdn.com";
string[] urls = url.Split(new char[] { ';' });
CheckServeStatus(urls);
Console.ReadKey();
}
/// <summary>
/// 檢測網(wǎng)絡(luò)連接狀態(tài)
/// </summary>
/// <param name="urls"></param>
public static void CheckServeStatus(string[] urls)
{
int errCount = 0;//ping時連接失敗個數(shù)
if (!LocalConnectionStatus())
{
Console.WriteLine("網(wǎng)絡(luò)異常~無連接");
}
else if (!MyPing(urls, out errCount))
{
if ((double)errCount / urls.Length >= 0.3)
{
Console.WriteLine("網(wǎng)絡(luò)異常~連接多次無響應(yīng)");
}
else
{
Console.WriteLine("網(wǎng)絡(luò)不穩(wěn)定");
}
}
else
{
Console.WriteLine("網(wǎng)絡(luò)正常");
}
}
#region 網(wǎng)絡(luò)檢測
private const int INTERNET_CONNECTION_MODEM = 1;
private const int INTERNET_CONNECTION_LAN = 2;
[System.Runtime.InteropServices.DllImport("winInet.dll")]
private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved);
/// <summary>
/// 判斷本地的連接狀態(tài)
/// </summary>
/// <returns></returns>
private static bool LocalConnectionStatus()
{
System.Int32 dwFlag = new Int32();
if (!InternetGetConnectedState(ref dwFlag, 0))
{
Console.WriteLine("LocalConnectionStatus--未連網(wǎng)!");
return false;
}
else
{
if ((dwFlag & INTERNET_CONNECTION_MODEM) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用調(diào)制解調(diào)器上網(wǎng)。");
return true;
}
else if ((dwFlag & INTERNET_CONNECTION_LAN) != 0)
{
Console.WriteLine("LocalConnectionStatus--采用網(wǎng)卡上網(wǎng)。");
return true;
}
}
return false;
}
/// <summary>
/// Ping命令檢測網(wǎng)絡(luò)是否暢通
/// </summary>
/// <param name="urls">URL數(shù)據(jù)</param>
/// <param name="errorCount">ping時連接失敗個數(shù)</param>
/// <returns></returns>
public static bool MyPing(string[] urls, out int errorCount)
{
bool isconn = true;
Ping ping = new Ping();
errorCount = 0;
try
{
PingReply pr;
for (int i = 0; i < urls.Length; i++)
{
pr = ping.Send(urls[i]);
if (pr.Status != IPStatus.Success)
{
isconn = false;
errorCount++;
}
Console.WriteLine("Ping " + urls[i] + " " + pr.Status.ToString());
}
}
catch
{
isconn = false;
errorCount = urls.Length;
}
//if (errorCount > 0 && errorCount < 3)
// isconn = true;
return isconn;
}
#endregion
}
}
- C# ping網(wǎng)絡(luò)IP 實現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測的方法
- c#判斷網(wǎng)絡(luò)連接狀態(tài)的示例分享
- C#中判斷本地系統(tǒng)的網(wǎng)絡(luò)連接狀態(tài)的方法
- js計數(shù)器代碼
- 一個簡單的網(wǎng)站訪問JS計數(shù)器 刷新1次加1次訪問
- Redis的使用模式之計數(shù)器模式實例
- PHP計數(shù)器的實現(xiàn)代碼
- 基于JQuery的數(shù)字改變的動畫效果--可用來做計數(shù)器
- php計數(shù)器的設(shè)計與實現(xiàn)
- C#利用性能計數(shù)器監(jiān)控網(wǎng)絡(luò)狀態(tài)
相關(guān)文章
C#基礎(chǔ)知識系列八const和readonly關(guān)鍵字詳細介紹
這篇文章主要介紹了C#中的const和readonly關(guān)鍵字,有需要的朋友可以參考一下2014-01-01C#調(diào)用windows api關(guān)機(關(guān)機api)示例代碼分享
本文主要介紹了C#調(diào)用windows api關(guān)機的示例代碼,大家參考使用吧2014-01-01C# 無需COM組件創(chuàng)建快捷方式的實現(xiàn)代碼
做一個小程序, 需要創(chuàng)建快捷方式, 網(wǎng)上普遍的做法是引入 COM 組件, 雖然也挺方便的, 但引入之后, 程序就需要多帶一個 dll 文件, 這樣, 想做成單文件便攜版就不行了2011-05-05C#學(xué)習(xí)筆記整理_深入剖析構(gòu)造函數(shù)、析構(gòu)函數(shù)
下面小編就為大家?guī)硪黄狢#學(xué)習(xí)筆記整理_深入剖析構(gòu)造函數(shù)、析構(gòu)函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09WPF利用CommunityToolkit.Mvvm實現(xiàn)級聯(lián)選擇器
這篇文章主要介紹了WPF如何利用CommunityToolkit.Mvvm實現(xiàn)級聯(lián)選擇器,文中的示例代碼講解詳細,對我們的學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下2023-12-12