using System.Collections; using System.Net; using System.Net.NetworkInformation;
using System.Net.Sockets; namespace Core.Util { /// <summary> /// Ip地址帮助类 ///
</summary> public class IpHelper { #region 外部接口 /// <summary> /// 获取本地IP地址 ///
</summary> /// <returns></returns> public static string GetLocalIp() {
UnicastIPAddressInformation mostSuitableIp = null; var networkInterfaces =
NetworkInterface.GetAllNetworkInterfaces(); foreach (var network in
networkInterfaces) { if (network.OperationalStatus != OperationalStatus.Up)
continue; var properties = network.GetIPProperties(); if (properties.
GatewayAddresses.Count == 0) continue; foreach (var address in properties.
UnicastAddresses) { if (address.Address.AddressFamily != AddressFamily.
InterNetwork) continue; if (IPAddress.IsLoopback(address.Address)) continue;
return address.Address.ToString(); } } return mostSuitableIp != null ?
mostSuitableIp.Address.ToString() : ""; } /// <summary> /// 获取第一个可用的端口号 ///
</summary> /// <returns></returns> public static int GetFirstAvailablePort() {
int BEGIN_PORT = 1024;//从这个端口开始检测 int MAX_PORT = 65535; //系统tcp/udp端口数最大是65535
for (int i = BEGIN_PORT; i < MAX_PORT; i++) { if (PortIsAvailable(i)) return i;
} return -1; } /// <summary> /// 检查指定端口是否已用 /// </summary> /// <param
name="port"></param> /// <returns></returns> public static bool PortIsAvailable(
int port) { bool isAvailable = true; IList portUsed = PortIsUsed(); foreach (int
pin portUsed) { if (p == port) { isAvailable = false; break; } } return
isAvailable; } #endregion #region 私有成员 /// <summary> /// 获取操作系统已用的端口号 ///
</summary> /// <returns></returns> private static IList PortIsUsed() {
//获取本地计算机的网络连接和通信统计数据的信息 IPGlobalProperties ipGlobalProperties =
IPGlobalProperties.GetIPGlobalProperties(); //返回本地计算机上的所有Tcp监听程序 IPEndPoint[]
ipsTCP= ipGlobalProperties.GetActiveTcpListeners(); //返回本地计算机上的所有UDP监听程序
IPEndPoint[] ipsUDP = ipGlobalProperties.GetActiveUdpListeners();
//返回本地计算机上的Internet协议版本4(IPV4 传输控制协议(TCP)连接的信息。 TcpConnectionInformation[]
tcpConnInfoArray= ipGlobalProperties.GetActiveTcpConnections(); IList allPorts =
new ArrayList(); foreach (IPEndPoint ep in ipsTCP) allPorts.Add(ep.Port);
foreach (IPEndPoint ep in ipsUDP) allPorts.Add(ep.Port); foreach (
TcpConnectionInformation conn in tcpConnInfoArray) allPorts.Add(conn.
LocalEndPoint.Port); return allPorts; } #endregion } }

技术
©2020 ioDraw All rights reserved
一个学生关于鸿蒙系统的一些看法今天你抱怨了吗?JS基础重点知识实验总结(全)OpenCV之IplImage详解数据分析之交叉分析特朗普的"VIP疗法":正接受一种尚未获批的药物治疗大数据环境---数据仓库(hive+mysql+hadoop)的构建少儿编程孩子的学习路线网络安全第五讲 网络攻击技术分析网上赚钱的门路方法,大部分人都是利用这三种方法!