我目前正在开发一个名为GTA的游戏的配对器,问题是游戏服务器使用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());
}有没有人有办法让它工作?
发布于 2016-04-19 01:01:54
您可以使用以下代码来实现您的目标
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();
}发布于 2015-04-26 20:11:29
当你想要像这样的端口时,Cling有一些问题。您应该使用以下代码:
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(); 不要忘记在路由器上打开UPnP。
当你的通信结束时,你应该像这样关闭它:
upnpService.shutdown();https://stackoverflow.com/questions/15324689
复制相似问题