使用cling进行路由器端口转发

Ped*_*lis 6 java port router forwarding

我正在为一款名为GTA的游戏开发Match Maker,问题是游戏服务器使用7777端口,我需要打开这个端口到世界各地允许玩家加入服务器,我不想要用户对其路由器进行任何更改.

注意:游戏服务器不是我的,我无法修改其源代码,我只是启动它.

所以,我发现Cling可以处理端口转发,但我不能让它工作!

我正在使用的代码:

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());
}
Run Code Online (Sandbox Code Playgroud)

有谁有任何想法让它工作?

小智 6

您可以使用以下代码实现目标

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

    }
Run Code Online (Sandbox Code Playgroud)