C# 如何判斷某個 TCP 端口是否被占用?
在C#中使用TcpClient或者其他通用的方式建立的Socket,該如何判斷這個端口是否被占用?比如下面的代碼:
TcpClient tc = new TcpClient(ip,port);
在C#中,可以使用System.Net.NetworkInformation命名空間下的IPGlobalProperties類和TcpConnectionInformation類來判斷某個TCP端口是否被占用:
using System.Net.NetworkInformation;
int port = 80;
if(IsPortInUse(port)){
Console.WriteLine($"{port} 端口被占用!");
}else{
Console.WriteLine($"{port} 端口未被占用!");
}
Console.ReadLine();
bool IsPortInUse(int port)
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnectionInformation connection in connections)
{
if (connection.LocalEndPoint.Port == port)
{
// 端口已被占用
return true;
}
}
// 端口未被占用
return false;
}
80 端口未被占用!
在上述示例中,IsPortInUse方法接收一個整數參數表示端口號。它通過IPGlobalProperties.GetIPGlobalProperties方法獲取當前計算機的網絡連接信息,然后使用GetActiveTcpConnections方法獲取所有活動的TCP連接信息。接著,使用循環遍歷每個TCP連接信息,判斷本地端點的端口號是否與傳入的端口號相同。如果找到匹配的端口號,則表示端口已被占用;否則,表示端口未被占用。
需要注意的是,為了執行此操作,可能需要管理員權限或運行在適當的操作系統上。此外,還要確保沒有其他進程在占用同一端口,因為TCP連接僅能檢測到已建立的連接而無法檢測到監聽狀態的端口。
想要檢測監聽狀態的端口,可以使用System.Net.NetworkInformation命名空間下的IPGlobalProperties類和IPEndPoint類來獲取所有監聽的TCP端口。
using System.Net;
using System.Net.NetworkInformation;
public void GetListeningPorts()
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endpoints = properties.GetActiveTcpListeners();
foreach (IPEndPoint endpoint in endpoints)
{
Console.WriteLine($"監聽狀態的端口:{endpoint.Port}");
}
}
監聽狀態的端口:80
監聽狀態的端口:135
監聽狀態的端口:445
監聽狀態的端口:1433
監聽狀態的端口:2383
監聽狀態的端口:3306
監聽狀態的端口:3389
監聽狀態的端口:5040
....
在命令提示符(cmd)中,可以使用 netstat 命令來列出占用的端口
netstat -ano | findstr LISTENING
這個命令會列出所有正在監聽(被占用)的端口,并顯示與每個端口關聯的進程標識符(PID)。
這個命令的部分:
- netstat:用于顯示網絡狀態和連接信息的命令。
- -ano:參數選項,a 用于顯示所有連接和偵聽端口,n 用于以數字格式顯示端口和地址,o 用于顯示關聯的進程標識符。
- |:管道操作符,將 netstat 的輸出結果傳遞給下一個命令。
- findstr:用于搜索指定的字符串或文本的命令。
- LISTENING:要搜索的字符串,表示只搜索正在監聽的端口。
執行上述命令后,將看到正在監聽的端口列表以及與它們關聯的進程的 PID??梢愿鶕枰檎姨囟ǖ亩丝诨蜻M一步處理輸出結果
C:\Users\Admin>netstat -ano | findstr LISTENING
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 848
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:1433 0.0.0.0:0 LISTENING 4956
TCP 0.0.0.0:2383 0.0.0.0:0 LISTENING 5200
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING 4280
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING 1204
TCP 0.0.0.0:5040 0.0.0.0:0 LISTENING 6424
TCP 0.0.0.0:7680 0.0.0.0:0 LISTENING 8344
TCP 0.0.0.0:49664 0.0.0.0:0 LISTENING 308
TCP 0.0.0.0:49665 0.0.0.0:0 LISTENING 932
TCP 0.0.0.0:49666 0.0.0.0:0 LISTENING 1512
TCP 0.0.0.0:49667 0.0.0.0:0 LISTENING 1364
TCP 0.0.0.0:49668 0.0.0.0:0 LISTENING 2484
TCP 0.0.0.0:49669 0.0.0.0:0 LISTENING 3440
TCP 0.0.0.0:49673 0.0.0.0:0 LISTENING 68
TCP 0.0.0.0:49706 0.0.0.0:0 LISTENING 4956
TCP 0.0.0.0:50128 0.0.0.0:0 LISTENING 4
TCP 127.0.0.1:22 0.0.0.0:0 LISTENING 8960
TCP 127.0.0.1:81 0.0.0.0:0 LISTENING 8960
TCP 127.0.0.1:443 0.0.0.0:0 LISTENING 8960
TCP 127.0.0.1:1434 0.0.0.0:0 LISTENING 4956
TCP 127.0.0.1:4012 0.0.0.0:0 LISTENING 4180
TCP 127.0.0.1:4013 0.0.0.0:0 LISTENING 4180
TCP 127.0.0.1:4301 0.0.0.0:0 LISTENING 8008
TCP 127.0.0.1:5533 0.0.0.0:0 LISTENING 18928
TCP 127.0.0.1:9210 0.0.0.0:0 LISTENING 6924
TCP 127.0.0.1:9418 0.0.0.0:0 LISTENING 8960
TCP 127.0.0.1:49672 0.0.0.0:0 LISTENING 3804
TCP 127.0.0.1:57178 0.0.0.0:0 LISTENING 2600
TCP 127.0.0.1:57335 0.0.0.0:0 LISTENING 17900
TCP 192.168.1.108:139 0.0.0.0:0 LISTENING 4
TCP [::]:80 [::]:0 LISTENING 4
TCP [::]:135 [::]:0 LISTENING 848
TCP [::]:445 [::]:0 LISTENING 4
TCP [::]:1433 [::]:0 LISTENING 4956
TCP [::]:2383 [::]:0 LISTENING 5200
TCP [::]:3306 [::]:0 LISTENING 4280
TCP [::]:3389 [::]:0 LISTENING 1204
TCP [::]:7680 [::]:0 LISTENING 8344
TCP [::]:49664 [::]:0 LISTENING 308
TCP [::]:49665 [::]:0 LISTENING 932
TCP [::]:49666 [::]:0 LISTENING 1512
TCP [::]:49667 [::]:0 LISTENING 1364
TCP [::]:49668 [::]:0 LISTENING 2484
TCP [::]:49669 [::]:0 LISTENING 3440
TCP [::]:49673 [::]:0 LISTENING 68
TCP [::]:49706 [::]:0 LISTENING 4956
TCP [::]:50128 [::]:0 LISTENING 4
TCP [::1]:22 [::]:0 LISTENING 8960
TCP [::1]:81 [::]:0 LISTENING 8960
TCP [::1]:443 [::]:0 LISTENING 8960
TCP [::1]:1434 [::]:0 LISTENING 4956
TCP [::1]:9418 [::]:0 LISTENING 8960