mirror of
https://github.com/netchx/netch.git
synced 2026-03-16 17:53:17 +08:00
59 lines
1.5 KiB
C#
59 lines
1.5 KiB
C#
using System.Net;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace Netch.Utils
|
|
{
|
|
class PortHelper
|
|
{
|
|
/// <summary>
|
|
/// 端口是否被使用
|
|
/// </summary>
|
|
/// <param name="port"></param>
|
|
/// <returns></returns>
|
|
public static bool PortInUse(int port)
|
|
{
|
|
return PortInUse(port, PortType.TCP) || PortInUse(port, PortType.UDP);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 指定类型的端口是否已经被使用了
|
|
/// </summary>
|
|
/// <param name="port">端口号</param>
|
|
/// <param name="type">端口类型</param>
|
|
/// <returns></returns>
|
|
public static bool PortInUse(int port, PortType type)
|
|
{
|
|
bool flag = false;
|
|
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
|
|
IPEndPoint[] ipendpoints = null;
|
|
if (type == PortType.TCP)
|
|
{
|
|
ipendpoints = properties.GetActiveTcpListeners();
|
|
}
|
|
else
|
|
{
|
|
ipendpoints = properties.GetActiveUdpListeners();
|
|
}
|
|
foreach (IPEndPoint ipendpoint in ipendpoints)
|
|
{
|
|
if (ipendpoint.Port == port)
|
|
{
|
|
flag = true;
|
|
break;
|
|
}
|
|
}
|
|
ipendpoints = null;
|
|
properties = null;
|
|
return flag;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 端口类型
|
|
/// </summary>
|
|
enum PortType
|
|
{
|
|
TCP,
|
|
UDP
|
|
}
|
|
} |