• 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();
            }
        }
    }
    
  • 相关阅读:
    尝试用博客园写个人日记
    db4o能代替Hibernate吗?
    RSS是否能支持携带扩展的权重信息呢?
    log4view可以更清楚地看log文件,不知道咋用
    tafiti.com体验微软的silverlight
    推荐两个插件DPack, Resharper
    用美味书签的标签实现类似nextlib的知识管理
    进程通讯的多种方式
    多维角度聊聊结对编程
    在Windows下编译和.NET运行MemCached
  • 原文地址:https://www.cnblogs.com/zzr-stdio/p/14918388.html
Copyright © 2020-2023  润新知