Pedro Papadópolis Pedro Papadópolis - 2 years ago 124
Java Question

Router port forwarding using cling

I'm currently developing an Match Maker for a game called GTA, the problem is that the game server uses 7777 port and I need to open this port to the world to allow players to join in the server, and I don't want the users to make any changes on their routers.

Note: The game server is not mine, I can not modify its source code, I just launch it.

So, I discovered that Cling can handle with port forwarding, but I can't make it to work!

Code I'm using:

public static void openports() throws UnknownHostException {
InetAddress i = InetAddress.getLocalHost();

UpnpService upnpServiceTCP = new UpnpServiceImpl(new PortMappingListener(new PortMapping(7777, i.getHostAddress(), PortMapping.Protocol.TCP)));
upnpServiceTCP.getControlPoint().search(new STAllHeader());

UpnpService upnpServiceUDP = new UpnpServiceImpl(new PortMappingListener(new PortMapping(7777, i.getHostAddress(), PortMapping.Protocol.UDP)));
upnpServiceUDP.getControlPoint().search(new STAllHeader());

anyone has any idea to make it work?

Answer Source

Cling have a few problems when you want portforwad ports like this. You should use this code:

UpnpServiceImpl upnpService = null;
PortMapping[] arr = new PortMapping[2];

    arr[0] = new PortMapping(7777, InetAddress.getLocalHost().getHostAddress(), PortMapping.Protocol.TCP,"My Port Mapping1");       
    arr[1] = new PortMapping(7777, InetAddress.getLocalHost().getHostAddress(), PortMapping.Protocol.UDP,"My Port Mapping2");

    upnpService = new UpnpServiceImpl(new PortMappingListener(arr));            


Do not forget to turn on UPnP on the router.

And when your communication ends you should turn it off like this:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download