zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    
  • 相关阅读:
    YTU 2405: C语言习题 牛顿迭代法求根
    学军中学推理社2017届招新试题
    UWP开发入门系列笔记之(零):UWP的前世今生
    微信开发基础教程
    text-align:center与<CENTER>的区别
    css中em与px的区别
    text-align 属性规定元素中的文本的水平对齐方式。
    常用颜色代码
    CSS中font-style的属性有Italic oblique,它们俩的区别是什么呢?
    css对大小写不敏感,空格不会影响css在浏览器的工作效果
  • 原文地址:https://www.cnblogs.com/zzr-stdio/p/14918388.html
Copyright © 2011-2022 走看看