C# 獲取一個可用的TCP端口號

C# 獲取一個可用的TCP端口號

第一種方式:

public static int GetAvailablePort(IPAddress ip)
{
    TcpListener listener = new TcpListener(ip, 0);
    listener.Start();
    int port = ((IPEndPoint)listener.LocalEndpoint).Port;
    listener.Stop();
    return port;
}

第二種方式:

public static class FreePort
{
    private const string PortReleaseGuid = "CE068CFE-E3C6-4A72-B19A-E2743E2B08C6";

    /// <summary>
    /// 尋找空閒的端口號
    /// </summary>
    /// <param name="startPort">開始尋找的起始端口號</param>
    /// <returns>空閒的端口號</returns>
    public static int FindNextAvailableTCPPort(int startPort)
    {
        int port = startPort;
        bool isAvailable = true;

        var mutex = new Mutex(false,
                              string.Concat("Global/", PortReleaseGuid));
        mutex.WaitOne();
        try
        {
            IPGlobalProperties ipGlobalProperties =
                IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] endPoints =
                ipGlobalProperties.GetActiveTcpListeners();

            do
            {
                if (!isAvailable)
                {
                    port++;
                    isAvailable = true;
                }

                foreach (IPEndPoint endPoint in endPoints)
                {
                    if (endPoint.Port != port) continue;
                    isAvailable = false;
                    break;
                }

            } while (!isAvailable && port < IPEndPoint.MaxPort);

            if (!isAvailable)
                throw new ApplicationException("Not able to find a free TCP port.");

            return port;
        }
        finally
        {
            mutex.ReleaseMutex();
        }
    }

    /// <summary>
    /// 尋找空閒的udp端口號
    /// </summary>
    /// <param name="startPort">開始尋找的起始端口號</param>
    /// <returns>空閒的端口號</returns>
    public static int FindNextAvailableUDPPort(int startPort)
    {
        int port = startPort;
        bool isAvailable = true;

        var mutex = new Mutex(false,
                              string.Concat("Global/", PortReleaseGuid));
        mutex.WaitOne();
        try
        {
            IPGlobalProperties ipGlobalProperties =
                IPGlobalProperties.GetIPGlobalProperties();
            IPEndPoint[] endPoints =
                ipGlobalProperties.GetActiveUdpListeners();

            do
            {
                if (!isAvailable)
                {
                    port++;
                    isAvailable = true;
                }

                foreach (IPEndPoint endPoint in endPoints)
                {
                    if (endPoint.Port != port)
                        continue;
                    isAvailable = false;
                    break;
                }

            } while (!isAvailable && port < IPEndPoint.MaxPort);

            if (!isAvailable)
                throw new ApplicationException("Not able to find a free TCP port.");

            return port;
        }
        finally
        {
            mutex.ReleaseMutex();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章