问题
I have a function in C# that finds an open port :
var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
Is it possible, once I have opened that port, to check if this port can be mapped with UPnP? Because I'm about to try to forward it and I want to know in advance if it's going to fail.
回答1:
Yes you certainly can, however I must warn you that unfortunately many home internet routers and gateways have a poor implementation of the UPnP protocol and it doesn't always work very well. It sucks but that's the world we live in. :)
How to map or open a port using UPnP
The UPnP protocol specification is very big and complex so I strongly suggest you use a framework. There are a couple of great open source C# frameworks to use:
- Mono.NAT
- Open.NAT
I personally recommend Open.NAT as it has a more modern async implementation.
Here is an example of using Open.NAT to attempt to open or map UDP port 1600:
public static void Main(string[] args) {
// Setup and initialize Open.NAT:
NatUtility.PortMapper = Upnp; // Search only for Upnp devices
NatUtility.TraceSource.Switch.Level = SourceLevels.Information;
NatUtility.DeviceFound += DeviceFound;
NatUtility.Initialize();
// Start looking for UPnP devices:
NatUtility.StartDiscovery();
while(true) {}
}
// This method is called every time a new UPnP device is found:
private static async void DeviceFound (object sender, DeviceEventArgs args) {
var deviceIP = await device.GetExternalIPAsync();
Console.WriteLine("Found a UPnP device with IP " + deviceIP);
Console.WriteLine("Attempting to map UDP port 1600...");
await device.CreatePortMapAsync(new Mapping(Protocol.Udp, 1600, 1600, 10000, "Open.Nat Testing"));
}
Simple! You can of course do a lot more such as asking the router for all the UPnP ports it currently has mapped. You should check the Open.NAT documentation for that.
来源:https://stackoverflow.com/questions/24501597/check-if-port-can-be-mapped-with-upnp