如何解决创建带有Open.NAT问题的端口映射
我正在尝试使用库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,"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;
}
解决方法
此异常表示端口正在被另一个进程使用
您需要检查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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。