網卡信息檢測與網絡流量檢測
文章目錄
每臺計算機進行網線通訊,都要藉助一個硬件設備,網卡,簡稱NIC,
NIC
是 Network Interface Controller
的縮寫。網卡則負責將比特流轉換成電信號發送出去和將檢測到的電信號轉換成比特流並接收。
網絡適配器:
-
又稱網卡是連接計算機與網絡的硬件設備。
-
整理計算機上發往網線上的數據,並將數據分解爲適當大小的數據包之後向網絡上發送。
在C#中,提供了System.Net.NetworkInformation命名空間,來提供網卡的相關信息。
其中包括:
- 對本機網卡相關信息的檢測
- 本機有多少網卡,網卡名稱、速度、硬件地址等。
- 對本機網絡流量的檢測
- 網絡連接配置、接收與發送的數據包等。
網卡信息檢測相關類
Network Interface類
提供了網絡適配器的配置和統計信息:
-
網絡適配器個數
-
網絡適配器型號
-
網絡適配器的速度
-
網絡適配器MAC地址
-
網絡適配器連接是否可用
每個網絡適配器都包含一個NetworkInterface對象與之對應。
屬性及方法 | 說明 |
---|---|
Name屬性 | 獲取網絡適配器的名稱 |
Speed屬性 | 獲取網絡適配器的速度(*bit/*秒) |
GetAllNetworkInterfaces方法 | 返回描述本地計算機上的所有網絡適配器對象 |
GetIPProperties方法 | 返回描述此網絡適配器配置的對象 |
GetIsNetworkAvailable方法 | 指示是否有任何可用的網絡連接 |
GetPhysicalAddress方法 | 返回此適配器的媒體訪問控制(MAC)地址 |
Supports方法 | 指示接口是否支持指定的協議(IPv4或IPv6) |
獲取網卡的信息
private static void NetworkInterfaces()
{
#region NetWorkInterface
//利用NetworkInterface類提供的靜態方法得到NetworkInterface類型的數組。
NetworkInterface[] networkInterface = NetworkInterface.GetAllNetworkInterfaces();//聲明並初始化了一個 NetworkInterface類的對象數組。
Console.WriteLine($"網絡適配器的個數爲:{networkInterface.Length}");
Console.WriteLine($"是否有可以用網絡連接:{NetworkInterface.GetIsNetworkAvailable()}");
Console.WriteLine();
foreach (NetworkInterface network in networkInterface)
{
Console.WriteLine($"網卡名字:{network.Name}");
Console.WriteLine($"物理地址:{network.GetPhysicalAddress()}");
Console.WriteLine($"速度:{network.Speed}");
Console.WriteLine($"網卡ID:{network.Id}");
Console.WriteLine($"網卡描述:{network.Description}");
Console.WriteLine($"是否僅接受數據包:{network.IsReceiveOnly}");
Console.WriteLine($"是否支持IPV4:{network.Supports(NetworkInterfaceComponent.IPv4)}");
Console.WriteLine($"是否支持IPV6:{network.Supports(NetworkInterfaceComponent.IPv6)}");
Console.WriteLine("-----------------------------------------------------------------");
}
網卡名字:本地連接* 1
物理地址:144F8A2158EB
速度:-1
網卡ID:{FA9901D2-C3AD-412B-BCC2-D6FF592EE29B}
網卡描述:Microsoft Wi-Fi Direct Virtual Adapter
是否僅接受數據包:False
是否支持IPV4:True
是否支持IPV6:True
IPInterfaceProperties類
-
檢測本機所有網絡適配器支持的各種地址
- Dns服務器的IP地址、網關地址以及多路廣播地址。
-
IPInterfaceProperties類是抽象類,不能實例化。
- 通過NetworkInterface對象的GetIPProperties()獲得其實例
IPInterfaceProperties類常用的屬性和方法
屬性及方法 | 說明 |
---|---|
AnycastAddresses屬性 | 獲取分配給此接口的任意廣播IP地址 |
DhcpServerAddresses屬性 | 獲取此接口的動態主機配置協議(DHCP)服務器的地址 |
DnsAddresses屬性 | 獲取此接口的域名系統(DNS)服務器的地址 |
DnsSuffix屬性 | 獲取與此接口關聯的域名系統(DNS)後綴 |
GatewayAddresses屬性 | 獲取此接口的網關地址 |
MulticastAddresses屬性 | 獲取分配給此接口的多路廣播地址 |
UnicastAddresses屬性 | 獲取分配給此接口的單播地址 |
GetIPv4Properties方法 | 獲取此網絡接口的Internet協議版本(IPv4)配置數據 |
GetIPv6Properties方法 | 獲取此網絡接口的Internet協議版(IPv6)配置數據 |
實例 網卡單播地址的信息
private static void GetUnicastAdress()
{
#region UnicastAddress 網絡接口的單播地址
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
UnicastIPAddressInformationCollection unicasts = iPInterfaceProperties.UnicastAddresses;
//定義時間格式
//dddd 周幾
//mmmm 月份
//yyyy 年份
//dd 幾日
//hh:mm:ss 時分秒
// tt 上下午
string timetype = "dddd, MMMM dd, yyyy hh: mm: ss tt";//自己定義時間的格式
if (unicasts.Count > 0)
{
Console.WriteLine(adapter.Description);//打印出網卡的描述
foreach (UnicastIPAddressInformation unicastIPAddressInformation in unicasts)
{
DateTime when;//聲明一個當前時間的日期變量
Console.WriteLine("單播地址:.................{0}", unicastIPAddressInformation.Address);
Console.WriteLine("獲取IPv4子網掩碼:.........{0}", unicastIPAddressInformation.IPv4Mask);
Console.WriteLine("此地址作爲首選地址的秒數...{0}", unicastIPAddressInformation.AddressPreferredLifetime);
Console.WriteLine("此地址的有效剩餘秒數:.....{0}", unicastIPAddressInformation.AddressValidLifetime);
Console.WriteLine("DHCP剩餘時間:.............{0}", unicastIPAddressInformation.DhcpLeaseLifetime);
Console.WriteLine("前綴的長度:...............{0}", unicastIPAddressInformation.PrefixLength);
Console.WriteLine("標識前綴的值:.............{0}", unicastIPAddressInformation.PrefixOrigin);
Console.WriteLine("標識後綴的值:.............{0}", unicastIPAddressInformation.SuffixOrigin);
when = DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddressInformation.AddressPreferredLifetime);//計算時間
when.ToLocalTime();//轉化爲本地時間
Console.WriteLine("此地址作爲首選地址的到期時間爲......{0}",
when.ToString(timetype, System.Globalization.CultureInfo.CurrentCulture));//當地時間格式
when = DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddressInformation.AddressValidLifetime);//計算時間
when.ToLocalTime();//轉化爲本地時間
Console.WriteLine("此地址的到期時間爲..................{0}",
when.ToString(timetype, System.Globalization.CultureInfo.CurrentCulture));//當地時間格式
when = DateTime.UtcNow + TimeSpan.FromSeconds(unicastIPAddressInformation.DhcpLeaseLifetime);//計算時間
when.ToLocalTime();//轉化爲本地時間
Console.WriteLine("DHCP到期時間爲.......................{0}",
when.ToString(timetype, System.Globalization.CultureInfo.CurrentCulture));//當地時間格式
}
}
}
Console.ReadLine();
獲取網關地址
private static void GatewayAddress()
{
#region IPInterfaceProperties
NetworkInterface[] adapterd = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in adapterd)
{
IPInterfaceProperties iPInterfaceProperties = networkInterface.GetIPProperties();
//網關信息
GatewayIPAddressInformationCollection gatewayIPAddressInformation = iPInterfaceProperties.GatewayAddresses;
foreach (var item in gatewayIPAddressInformation)
{
Console.WriteLine($"網關地址:..............{gatewayIPAddressInformation[0].Address }");
Console.WriteLine();
}
}
Console.ReadLine();
獲取任意廣播地址
private static void AnyCastAddress()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (var adapter in adapters)
{
IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
IPAddressInformationCollection iPAddressInformation = iPInterfaceProperties.AnycastAddresses;
if (iPAddressInformation.Count > 0)
{
foreach (var i in iPAddressInformation)
{
Console.WriteLine($"廣播地址爲:..........i.Address");
Console.WriteLine($"是否在DNS服務器中出現:..........{(i.IsDnsEligible ? "是" : "否")}");//注意此處條件表達式的用法
Console.WriteLine($"廣播地址爲:..........{(i.IsTransient ? "是" : "否")}");
}
}
else
{
Console.WriteLine("{0}不存在廣播地址", adapter.Name);
}
}
Console.ReadLine();
}
獲取此接口的動態主機配置協議(DHCP)服務器的地址
private static void DhcpserverIPAddress()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (var adapter in adapters)
{
IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
IPAddressCollection iPAddressInformation = iPInterfaceProperties.DhcpServerAddresses;
if (iPAddressInformation.Count > 0)
{
foreach (var i in iPAddressInformation)
{
Console.WriteLine($"廣播地址爲:..........{i.Address}");
}
Console.ReadLine();
}
}
}
獲取此接口的域名系統(DNS)服務器的地址
private static void DnsSeverAddress()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (var adapter in adapters)
{
IPInterfaceProperties iPInterfaceProperties = adapter.GetIPProperties();
IPAddressCollection dnsServers = iPInterfaceProperties.DnsAddresses;
if (dnsServers.Count > 0)
{
Console.WriteLine(adapter.Description);
foreach (var i in dnsServers)
{
Console.WriteLine($"dns服務器地址爲:..........{i.ToString()}");
}
}
}
Console.ReadLine();
}
獲取此網絡接口的Internet協議版本(IPv4)配置數據
public static void DisplayIPv4NetworkInterfaces()
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine("IPv4 interface information for {0}.{1}",
properties.HostName, properties.DomainName);//獲取計算機名字和域
Console.WriteLine();
foreach (NetworkInterface adapter in nics)
{
// Only display informatin for interfaces that support IPv4.
if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false)//判斷是否支持IPV4
{
continue;//如果不支持,直接跳出循環
}
Console.WriteLine(adapter.Description);//打印出網卡的描述
// Underline the description.
Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length, '='));
IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
// Try to get the IPv4 interface properties.
IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties();
if (p == null)
{
Console.WriteLine("No IPv4 information is available for this interface.");
Console.WriteLine();
continue;
}
// Display the IPv4 specific data.
Console.WriteLine(" Index ............................. : {0}", p.Index);
Console.WriteLine(" MTU ............................... : {0}", p.Mtu);
Console.WriteLine(" APIPA active....................... : {0}",
p.IsAutomaticPrivateAddressingActive);
Console.WriteLine(" APIPA enabled...................... : {0}",
p.IsAutomaticPrivateAddressingEnabled);
Console.WriteLine(" Forwarding enabled................. : {0}",
p.IsForwardingEnabled);
Console.WriteLine(" Uses WINS ......................... : {0}",
p.UsesWins);
Console.WriteLine();
}
}
對齊方式的小技巧
private static void printFenge()
{
string a = "1234567890";
Console.WriteLine(a);
Console.WriteLine(String.Empty.PadLeft(a.Length, '='));
Console.WriteLine(String.Empty.PadLeft(a.Length, '*'));
Console.WriteLine(String.Empty.PadRight(a.Length, 'n'));
}
網絡流量檢測類
IPGlobalProperties類
- 提供了本地計算機網絡連接和通信統計數據的信息。
- 接收到的數據包個數、丟棄的數據包個數等。
IPGlobalProperties類提供的常用方法
名 稱 | 說 明 |
---|---|
GetActiveTcpConnections | 返回有關本地計算機上的 Internet協議版本 4 (IPV4) 傳輸控制協議 (TCP) 連接的信息 |
GetActiveTcpListeners | 返回有關本地計算機上的 Internet 協議版本 4 (IPV4) 傳輸控制協議 (TCP) 偵聽器的終結點信息 |
GetActiveUdpListeners | 返回有關本地計算機上的 Internet 協議版本 4 (IPv4) 用戶數據報協議 (UDP) 偵聽器的信息 |
GetIPv4GlobalStatistics | 提供本地計算機的 Internet 協議版本 4 (IPv4) 統計數據 |
GetIPv6GlobalStatistics | 提供本地計算機的 Internet 協議版本 6 (IPv6) 統計數據 |
GetTcpIPv4Statistics | 提供本地計算機的傳輸控制協議/Internet 協議版本 (TCP/IPv4) 統計數據 |
GetTcpIPv6Statistics | 提供本地計算機的傳輸控制協議/Internet 協議版本 6 (TCP/IPv6)統計數據 |
獲取本機TCP連接信息
private static void TCPlistener()
{
IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] iPEndPoint = iPGlobalProperties.GetActiveTcpListeners();//
Console.WriteLine("監聽的IP地址個數爲" + iPEndPoint.Length);
Console.WriteLine("-------------------------------------------------");
foreach (IPEndPoint iep in iPEndPoint)
{
Console.WriteLine("TCP監聽的IP地址...........{0}", iep.Address);
Console.WriteLine("TCP監聽的IP地址端口.......{0}", iep.Port);
Console.WriteLine("TCP監聽的IP遵循的協議.....{0}", iep.AddressFamily);
Console.WriteLine("-------------------------------------------------");
}
}
private static void TCPtraffic()
{
IPGlobalProperties iPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
Console.WriteLine($"主機名:{iPGlobalProperties.HostName} 主機所在的域{iPGlobalProperties.DomainName}");
IPGlobalStatistics iPGlobalStatistics = iPGlobalProperties.GetIPv4GlobalStatistics();
Console.WriteLine("IP數據包默認生存時間:" + iPGlobalStatistics.DefaultTtl);
Console.WriteLine("網絡接口的數量:" + iPGlobalStatistics.NumberOfInterfaces);
Console.WriteLine("分配的IPv4地址的數目:" + iPGlobalStatistics.NumberOfIPAddresses);
Console.WriteLine("路由數:" + iPGlobalStatistics.NumberOfRoutes);
Console.WriteLine("出站數據波=包" + iPGlobalStatistics.OutputPacketRequests);
Console.WriteLine("收到的數據包=" + iPGlobalStatistics.ReceivedPackets);
Console.ReadLine();
}
總結
要學會調試程序
通過斷點調試解決問題。
學會查官方文檔
官方寫的很清楚