using System; using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; namespace FastGithub.UI { /// /// udp日志工具类 /// static class UdpLoggerPort { /// /// 获取日志端口 /// public static int Value { get; } = GetAvailableUdpPort(38457); /// /// 获取可用的随机Udp端口 /// /// /// /// private static int GetAvailableUdpPort(int minValue, AddressFamily addressFamily = AddressFamily.InterNetwork) { var hashSet = new HashSet(); var tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners(); foreach (var endpoint in tcpListeners) { if (endpoint.AddressFamily == addressFamily) { hashSet.Add(endpoint.Port); } } for (var port = minValue; port < IPEndPoint.MaxPort; port++) { if (hashSet.Contains(port) == false) { return port; } } throw new ArgumentException("当前无可用的端口"); } } }