lontivero / Open.NAT

Lightweight and easy-to-use class library to allow port forwarding in NAT devices with UPNP and/or PMP
MIT License
421 stars 99 forks source link

How to open more then one port? #32

Closed beBoss closed 8 years ago

beBoss commented 8 years ago

Well, I can't figure out how to open more then one port. The code I used is from the example, but when run it, it opens only the last port (in my case 6626) and I THINK that it opens the two before the last one and then they stop working again but the last one is still working. My code:

    public static void Main()
    {
        OpenPort().Wait();

        Console.WriteLine("\nPress any key to exit...");
        Console.ReadKey();
    }

    private static async Task OpenPort()
    {
        var nat = new NatDiscoverer();
        var cts = new CancellationTokenSource(5000);
        var device = await nat.DiscoverDeviceAsync(PortMapper.Upnp, cts);
        var ip = await device.GetExternalIPAsync();

        Console.WriteLine("Your IP: {0}", ip);

        await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 6622, 6622, 0, "6622"));
        await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 6625, 6625, 0, "6625"));
        await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 6626, 6626, 0, "6626"));
        Console.WriteLine("Done!");
    }
lontivero commented 8 years ago

I really sorry for the delay in this answer, I didn't see this issue report. Could you try with a non-zero time-to-live value and send me the logs?

beBoss commented 8 years ago

Well, I don't know how to create a log file, but it opens only the last port (it seems like only the last line of code in my example 6626 works, for the other two don't). And how I check it, hmm when I check for the port only 6626 works and it's opened. Then I am swapping the ports in different order not 6622, 6625, 6626, but 6626,6622, 6625, and every time the last one is opened. And yeah I tried without 0 time/ time parameter.

So in this example: await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 6622, 6622, 0, "6622")); <- not working await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 6625, 6625, 0, "6625")); <- not working await device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 6626, 6626, 0, "6626")); <- always this port works.

No matter which port is here, only the last line. And sorry I'm not good developer to tell you more info and good examples. But I think the code is okay? Thanks anyway.

beBoss commented 8 years ago

Hmm maybe depends on the router or IP. I'm not sure, but I'm now on a new place where I think my IP address is static, but yesterday on my old place, the IP was dynamic for sure. Now the code opens the three ports for few sec, almost immediately and all works.

lontivero commented 8 years ago

@beBoss please let me know if you can reproduce it.