码迷,mamicode.com
首页 > Windows程序 > 详细

[转] C# 获取本机可用端口

时间:2014-11-13 22:14:28      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   使用   sp   

当我们要创建一个Tcp/UDP Server connection ,我们需要一个范围在1000到65535之间的端口 。但是本机一个端口只能一个程序监听,所以我们进行本地监听的时候需要检测端口是否被占用。命名空间System.Net.NetworkInformation下定义了一个名为IPGlobalProperties的类,我们使用这个类可以获取所有的监听连接,然后判断端口是否被占用。

  1 //----------------------------------------------------------------------------- 
  2 // Filename: FreePort.cs 
  3 // 
  4 // Description: Helper methods to find the next free UDP and TCP ports. 
  5 // 
  6 // History: 
  7 // 28 Mar 2012    Aaron Clauson    Copied from http://www.mattbrindley.com/developing/windows/net/detecting-the-next-available-free-tcp-port/. 
  8 //-----------------------------------------------------------------------------
  9 
 10 using System; 
 11 using System.Collections.Generic; 
 12 using System.Linq; 
 13 using System.Net; 
 14 using System.Net.NetworkInformation; 
 15 using System.Text; 
 16 using System.Threading;
 17 
 18 namespace SIPSorcery.Sys.Net 
 19 { 
 20     public class FreePort 
 21     { 
 22         private const string PortReleaseGuid = "8875BD8E-4D5B-11DE-B2F4-691756D89593";
 23 
 24         /// <summary> 
 25         /// Check if startPort is available, incrementing and 
 26         /// checking again if it‘s in use until a free port is found 
 27         /// </summary> 
 28         /// <param name="startPort">The first port to check</param> 
 29         /// <returns>The first available port</returns> 
 30         public static int FindNextAvailableTCPPort(int startPort) 
 31         { 
 32             int port = startPort; 
 33             bool isAvailable = true;
 34 
 35             var mutex = new Mutex(false, 
 36                 string.Concat("Global/", PortReleaseGuid)); 
 37             mutex.WaitOne(); 
 38             try 
 39             { 
 40                 IPGlobalProperties ipGlobalProperties = 
 41                     IPGlobalProperties.GetIPGlobalProperties(); 
 42                 IPEndPoint[] endPoints = 
 43                     ipGlobalProperties.GetActiveTcpListeners();
 44 
 45                 do 
 46                 { 
 47                     if (!isAvailable) 
 48                     { 
 49                         port++; 
 50                         isAvailable = true; 
 51                     }
 52 
 53                     foreach (IPEndPoint endPoint in endPoints) 
 54                     { 
 55                         if (endPoint.Port != port) continue; 
 56                         isAvailable = false; 
 57                         break; 
 58                     }
 59 
 60                 } while (!isAvailable && port < IPEndPoint.MaxPort);
 61 
 62                 if (!isAvailable) 
 63                     throw new ApplicationException("Not able to find a free TCP port.");
 64 
 65                 return port; 
 66             } 
 67             finally 
 68             { 
 69                 mutex.ReleaseMutex(); 
 70             } 
 71         }
 72 
 73         /// <summary> 
 74         /// Check if startPort is available, incrementing and 
 75         /// checking again if it‘s in use until a free port is found 
 76         /// </summary> 
 77         /// <param name="startPort">The first port to check</param> 
 78         /// <returns>The first available port</returns> 
 79         public static int FindNextAvailableUDPPort(int startPort) 
 80         { 
 81             int port = startPort; 
 82             bool isAvailable = true;
 83 
 84             var mutex = new Mutex(false, 
 85                 string.Concat("Global/", PortReleaseGuid)); 
 86             mutex.WaitOne(); 
 87             try 
 88             { 
 89                 IPGlobalProperties ipGlobalProperties = 
 90                     IPGlobalProperties.GetIPGlobalProperties(); 
 91                 IPEndPoint[] endPoints = 
 92                     ipGlobalProperties.GetActiveUdpListeners();
 93 
 94                 do 
 95                 { 
 96                     if (!isAvailable) 
 97                     { 
 98                         port++; 
 99                         isAvailable = true; 
100                     }
101 
102                     foreach (IPEndPoint endPoint in endPoints) 
103                     { 
104                         if (endPoint.Port != port) 
105                             continue; 
106                         isAvailable = false; 
107                         break; 
108                     }
109 
110                 } while (!isAvailable && port < IPEndPoint.MaxPort);
111 
112                 if (!isAvailable) 
113                     throw new ApplicationException("Not able to find a free TCP port.");
114 
115                 return port; 
116             } 
117             finally 
118             { 
119                 mutex.ReleaseMutex(); 
120             } 
121         } 
122     } 
123 } 

 

[转] C# 获取本机可用端口

标签:des   style   blog   http   io   color   ar   使用   sp   

原文地址:http://www.cnblogs.com/XingchenStudio/p/4095974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!