【问题标题】:Create a port mapping with Open.NAT problem使用 Open.NAT 问题创建端口映射
【发布时间】:2020-09-29 16:26:19
【问题描述】:

我正在尝试使用库 Open.NAT 创建端口映射,但收到错误

Open.Nat.MappingException: '未能创建端口映射(协议=Tcp,私有端口=12630)'

这是我的代码。哪里出了问题?

        private static async Task Main()
        {
            NatDiscoverer discoverer = new NatDiscoverer();
            NatDevice device = await discoverer.DiscoverDeviceAsync();

            int port = GetAvailablePort(1000);

            Mapping mapping = new Mapping(Protocol.Tcp, port, port, "Test mapping");

            await device.CreatePortMapAsync(mapping);    

            Console.WriteLine("The external IP Address is: {0} ", await device.GetExternalIPAsync());

            Console.ReadLine();

            await device.DeletePortMapAsync(mapping);
        }        

此函数用于查找空闲端口

        public static int GetAvailablePort(int startingPort)
        {
            List<int> portArray = new List<int>();

            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            
            TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
            portArray.AddRange(from n in connections
                               where n.LocalEndPoint.Port >= startingPort
                               select n.LocalEndPoint.Port);

            
            IPEndPoint[] endPoints = properties.GetActiveTcpListeners();
            portArray.AddRange(from n in endPoints
                               where n.Port >= startingPort
                               select n.Port);

            
            endPoints = properties.GetActiveUdpListeners();
            portArray.AddRange(from n in endPoints
                               where n.Port >= startingPort
                               select n.Port);

            portArray.Sort();

            for (int i = startingPort; i < ushort.MaxValue; i++)
            { 
                if (!portArray.Contains(i))
                {
                    return i;
                }
            }
            return 0;
        }

【问题讨论】:

  • 您的问题中暗示您的计算机实际上已连接到 NAT 路由器,并且 NAT 路由器确实支持 UPnP,并且路由器的 UPnP 支持实际上已启用。坦率地说,如果没有minimal reproducible example,实际上不可能为这样的问题提供有用的答案,但您至少需要确保网络配置也是正确的(这不是编程问题,而是网络管理问题)。
  • 如果假设我的网络配置有问题,但 UTorrent 如何在我的计算机上工作并下载文件? )
  • 您必须询问 UTorrent 开发人员,或者自己检查代码。也就是说,除了 UPnP 之外,还有其他方法可以导航 NAT 路由器。参见例如“打孔”。

标签: c# port portforwarding


【解决方案1】:

此异常表明端口正被另一个进程使用

你需要检查12630端口是否已经被另一个进程使用,如果是你需要杀死它或更改端口号。

另外我建议您检查 innerException 和/或 ErrorCode 以了解详细信息

可能的错误代码:

InvalidArguments = 402
ActionFailed = 501
Unathorized = 606
SpecifiedArrayIndexInvalid = 713        
NoSuchEntryInArray = 714
WildCardNotPermittedInSourceIp = 715
WildCardNotPermittedInExternalPort = 716
ConflictInMappingEntry = 718
SamePortValuesRequired = 724
OnlyPermanentLeasesSupported = 725
RemoteHostOnlySupportsWildcard = 726
ExternalPortOnlySupportsWildcard = 727
NoPortMapsAvailable = 728
ConflictWithOtherMechanisms = 729
WildCardNotPermittedInIntPort = 732

【讨论】:

  • InnerException 为空且错误代码等于 0。正如您在代码中看到的,我有一个代码可以找到空闲 TCP 端口
  • 你用的是什么发现?没有循环。您的开始和停止端口号是多少?
  • 我已经编辑了查找免费可用帖子的功能,请在我的第一个帖子中检查功能GetAvailablePort,但问题仍然存在
猜你喜欢
  • 2010-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多