Computers connected to the internet through a NAT (basically anyone using an ADSL modem and the like) are generally unable to accept connections from the internet. This is rubbish if you want them to be able to host games and so on.
Happily, most home routers are UPNP devices, and so can have their NAT tables manipulated to allow incoming connections to be routed to the correct address on the local network.
Here’s a little lib that’ll make that process easy. Jar here, javadoc zip here, svn repo here.
Using it to add a port mapping looks like this:
InternetGatewayDevice dev = InternetGatewayDevice.getDevices( 1000 )[ 0 ];
String thisWillBeHandy = dev.getExternalIPAddress();
String connType = "TCP"; // or "UDP"
String remoteHost = null; // allow any remote host to connect
int internetSidePort = 1337;
String localHost = "192.168.1.whatever";
int localPort = 7331;
dev.addPortMapping( "A mapping for my game", connType, remoteHost, internetSidePort, localHost, localPort, 0 );
after this, any TCP connections made to the external IP and port will be forwarded to the local IP and port. Remember to clean up after yourself:
dev.deletePortMapping( null, internetSidePort, connType );
Please note that this is just the library found over here, but brutally cut down so it’s 60KB rather than 2MB. If it works, gratitude should be directed towards SuperBonBon, if it doesn’t work, it’s probably because I broke it :persecutioncomplex: