2013-03-10 26 views
6

Attualmente sto sviluppando un Match Maker per un gioco chiamato GTA, il problema è che il server di gioco utilizza la porta 7777 e ho bisogno di aprire questa porta al mondo per consentire ai giocatori di unirsi al server e non voglio che gli utenti apportino modifiche ai loro router.Port forwarding del router usando cling

Nota: il server di gioco non è il mio, non posso modificare il suo codice sorgente, l'ho appena lanciato.

Così, ho scoperto che Cling può gestire con port forwarding, ma non riesco a farlo funzionare!

codice che sto usando:

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

qualcuno ha qualche idea per farlo funzionare?

risposta

5

È possibile raggiungere il tuo obiettivo utilizzando il codice qui sotto

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

    } 
4

Cling ha alcuni problemi quando si desidera che le porte portforwad siano così. Si dovrebbe usare questo codice:

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

Non dimenticare di attivare UPnP sul router.

E quando la vostra comunicazione si conclude si dovrebbe spegnere così:

upnpService.shutdown();