Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    System.out.println(i.getHostAddress());

    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?

like image 665
Pedro Papadópolis Avatar asked Mar 10 '13 16:03

Pedro Papadópolis


People also ask

How do I forward ports on my router?

To forward ports on your router, log into your router and go to the port forwarding section. Next, enter the port numbers and your device's IP address. Choose a forwarding protocol and save your changes. Note: If you don't see a port forwarding option in your router's settings, you might have to upgrade.

Can you port forward with a static IP?

First, Set Up a Static IP Address In order for port forwarding to work, you'll need to set a static internal IP address (ipv4) for your device. By default, your ipv4 address is probably dynamic, which means it's always changing, so the port forwarding won't be able to pin down your device on your home network.

Do hackers use port forwarding?

Port forwarding usually means leaving a gap in your security. This can potentially be dangerous because hackers could also use this to penetrate your network. Consequently, there are some documented cases when an opened port was used as an attack vector.

How to forward a port on a router?

Now that you have a static IP address (or DHCP reservation) on your device, you are ready to forward a port. To start, we need to log in to your router. Earlier, one of the settings that you wrote down in your Default Gateway. That is the IP address of your router. Routers use a locally hosted web page for management.

What is port forwarding on TP-Link wireless router?

Port forwarding: how to set up virtual server on TP-Link wireless router? Port forwarding: how to set up virtual server on TP-Link wireless router? What is Port Forwarding? Port forwarding is a way of making your router use a specific port to communicate with certain devices.

What ports do I need to port forward my website?

What you have to do is that port forward HTTP (port 80) or HTTPS (port 443) from your router to your computer on which your website is deployed. Hurray Knows anyone with your public IP address can access your website.

Why can't I port forward my server to another IP address?

Because if you create a port forwarding rule saying your game server is on a particular IP address, and then it is given a new IP by your router, other players would not be able to connect to it. To avoid that, you’ll need to assign a static IP address to every device you’re trying to port forward to.


2 Answers

You can achieve your goal by using below code

private void doPortForwarding() {

        PortMapping[] desiredMapping = new PortMapping[2];
         desiredMapping[0] = new PortMapping(8123, InetAddress.getLocalHost().getHostAddress(),
                PortMapping.Protocol.TCP, " TCP POT Forwarding");

         desiredMapping[1] = new PortMapping(8123, InetAddress.getLocalHost().getHostAddress(),
                    PortMapping.Protocol.UDP, " UDP POT Forwarding");


         UpnpService upnpService = new UpnpServiceImpl();
         RegistryListener registryListener = new PortMappingListener(desiredMapping);
         upnpService.getRegistry().addListener(registryListener);

        upnpService.getControlPoint().search();

    }
like image 62
chandan Avatar answered Oct 04 '22 21:10

chandan


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));            

    upnpService.getControlPoint().search();         

Do not forget to turn on UPnP on the router.

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

upnpService.shutdown();
like image 45
Delirante Avatar answered Oct 04 '22 21:10

Delirante