lontivero / Open.NAT

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

UPnP and .NET Sockets #71

Open phcs93 opened 6 years ago

phcs93 commented 6 years ago

I am making a simple chat application using .NET Sockets.

The host socket needs a specific TCP port (let's say 8501) to be forwarded in order to properly accept any incoming connections.

When I manually go to my router and open that port, the host socket works perfectly. Client sockets can connect to it and begin sending packets.

Now I want to automatically forward that port, so I found out about Open.NAT.

The sample code provided on GitHub works fine and the mappings are created without any exceptions or timeouts.

But the host socket fails to accept any incoming connections.

I've even tried to use a windows COM called NATUPnP 1.0 Type Library. I made it work, the mappings are created but, same results, the host socket won't accept any incoming connections.

Some information:

Am I missing something? If you need code I can provide my full simple test projects.

Thanks in advance!

lontivero commented 6 years ago

I could help you only if there is a problem with the library (it seems is not the case). Anyway, modify the ConsoleTest project to forward the port and share with me the network.log file in .\bin\debug

Also, use an online tool for verifying if the port is really open. It could sound redundant but just to be sure.

phcs93 commented 6 years ago

Hello, thanks for the quick answer.

Here is the network.log: https://gist.github.com/phcs93/b14caee405faf0b1684aec28f360b29d

I simply modified one line in ConsoleTest.NatTest.Main

From var t = nat.DiscoverDeviceAsync(PortMapper.Pmp, cts);

To var t = nat.DiscoverDeviceAsync(PortMapper.Upnp, cts);

Also, the online tools for checking port forwarding (canyouseeme.org for example) doesn't seem to be working. Even when I manually forward the ports and get my sockets working, those tools says that said ports are closed.

lontivero commented 6 years ago

I cannot see anything wrong in the log file. It seems the mappings were created okay, with no errors and you were able to list them after creation. The router could be misbehaving or there is another problem but the library seems to be working as expected.

Do yo have more that a router?

phcs93 commented 6 years ago

Yes, I have a modem and a router. But the modem is simply configured as bridge.

lontivero commented 6 years ago

Is your modem IP 192.168.0.1? If yes then the library is forwarding the ports in your modem instead of doing it in your router.

phcs93 commented 6 years ago

Nope, my modem is configured as bridge and its IP is 192.168.0.100

192.168.0.1 points to my router

PhantomGamers commented 3 years ago

I know this is an old post but did you ever find a solution to this issue?

I'm trying to use Open.NAT along with websocket-sharp for networking in a Unity game and I'm running into the same issue.

The port is properly forwarded, I see the entry on my router just fine, but I cannot connect to the websocket server. If I manually forward the port, so other than the entry not being marked UPNP it looks identical, I can connect fine.

I imagine this is an issue with .net sockets/websockets and not with Open.NAT though

EDIT: It seems this happens when you specify a lifetime when creating the port mapping with Open.NAT. When I leave the lifetime out, the connection works fine. Hm...

lontivero commented 3 years ago

All what I see is Open.NAT behaving correctly. Something has to be broken before I can fix it and there is not evidence about that.