代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
获取本机可用端口
作者:
dezai
/ 发布于
2014/7/28
/
589
当我们要创建一个Tcp/UDP Server connection ,我们需要一个范围在1000到65535之间的端口 。但是本机一个端口只能一个程序监听,所以我们进行本地监听的时候需要检测端口是否被占用。命名空间System.Net.NetworkInformation下定义了一个名为IPGlobalProperties的类,我们使用这个类可以获取所有的监听连接,然后判断端口是否被占用.
//----------------------------------------------------------------------------- // Filename: FreePort.cs // // Description: Helper methods to find the next free UDP and TCP ports. // // History: // 28 Mar 2012 Aaron Clauson Copied from http://www.mattbrindley.com/developing/windows/net/detecting-the-next-available-free-tcp-port/. //----------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Text; using System.Threading; namespace SIPSorcery.Sys.Net { public class FreePort { private const string PortReleaseGuid = "8875BD8E-4D5B-11DE-B2F4-691756D89593"; /// <summary> /// Check if startPort is available, incrementing and /// checking again if it's in use until a free port is found /// </summary> /// <param name="startPort">The first port to check</param> /// <returns>The first available port</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> /// Check if startPort is available, incrementing and /// checking again if it's in use until a free port is found /// </summary> /// <param name="startPort">The first port to check</param> /// <returns>The first available port</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(); } } } }
试试其它关键字
本机
可用端口
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3