Search code examples
c#network-programmingxnaportforwardingupnp

UPnP and Port Forwarding with Lidgren fails


I'm fairly new to networking and lidgren has made it much easier for me to begin adding multiplayer capability to my XNA PC game. I've been testing across the network setting up my laptop right next to me and it has been working great. The problem is I sent a copy of my game to a friend in the Netherlands and she cannot connect to me. I have it set up to where one person is the host and other people are clients who connect to that host.

The host sets up the server as follows:

 Config = new NetPeerConfiguration("game");
 Config.Port = 14242;
 Config.EnableUPnP = true;
 Config.MaximumConnections = 3;
 Config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
 Server = new NetServer(Config);
 Server.Start();
 Server.UPnP.ForwardPort(14242, "Forlorn Forest");

Here's the exception ForwardPort throws as it fails and gives "Bad Request": enter image description here

And two web exceptions that are thrown: enter image description here enter image description here

It says something about the connection being closed by the remote host and unable to read data from the transport connection in the exception details: enter image description here

Any thoughts as to what might be going on here? UPnP is enabled on my router. I've taken a look at network traffic with WireShark and I can see it display malformed packets though I didn't mess with any of the Lidgren code so I'm not sure why it would.


Solution

  • The UPnP protocol specification has unfortunately not been properly implemented on a huge number of the home routers out there. The effect of this is that all UPnP frameworks (Even the Intel spec framework) will fail to properly communicate UPnP with some local routers.

    You should find that not all of your friends routers will successfully open the ports you request. The best you can do is make sure your code has no failure scenarios and uncaught exceptions relating to the UPnP framework you're using and that you have it well documented how users who fail UPnP can manually setup port forwards for your game.