How do you configure a router with Java to make it...

I have started working with making a game meant to be played by several persons across the web.
I have it working and all, you can open it and play in a LAN but I have a problem. When I want to connect to another computer through the internet, not a LAN, the NAT in the router gets in the way (of course, it doesn’t know that an incomming call is for my computer and not my brother’s).
So, I know there are ways to configure a router, from your computer, so that it sends all calls to a certain Port to the same computer in the house. What I’m wondering is if there is a way to do this with Java and in that case I’d love to see some code with a function that takes an Integer (a port number) and makes the router send all received messages with that destination port to the computer that used this function. That way I’ll be able to set up a connection and let the game clients communicate with each other.

I hope this wasn’t confusing, I don’t know how to write it better I’m afraid.

I dont know why you’d have to configure your router to send/receive packets(messages).

You should look into UDP (user datagram protocol) and TCP (transmission control protocol) if you are interested
in learning how networking works

But if your more interested in just jumping right in and writing applications, you should really take a look at Kryonet (https://github.com/EsotericSoftware/kryonet)
Its a library that makes writing great networked (if thats the right word?) applications very easy and intuitive.

Some code examples from their github :
This code starts a server on TCP port 54555 and UDP port 54777:

    Server server = new Server();
    server.start();
    server.bind(54555, 54777);

This code connects to a server running on TCP port 54555 and UDP port 54777:

    Client client = new Client();
    client.start();
    client.connect(5000, "192.168.0.4", 54555, 54777);

    SomeRequest request = new SomeRequest();
    request.text = "Here is the request";
    client.sendTCP(request);
client.addListener(new Listener() {
       public void received (Connection connection, Object object) {
          if (object instanceof SomeResponse) {
             SomeResponse response = (SomeResponse)object;
             System.out.println(response.text);
          }
       }
    });

For more great code bits or example projects, check out their github.

To send and receive packets not in your local network you have to port forward. I have no idea if that’s what OP wants though.

Only the guy who hosts the server will need to open the ports on the routers configuration page. The players won’t need to do anything.

Especially, if your game doesn’t allow anyone but you to host a server, there is no reason to worry about, because you are the only one who needs to configurate his ports.

@Pauler
Well, it is my hope to be able to publish the game so that anyone can download it and host a server so it’d be really good if I could include some code in the server to open a port in the router automatically when the server starts up. After all, it’s good if your game can be used easily by anyone, even those who aren’t good at computers.

@saucymeatman
That library really looks good when it comes to sending and receiving stuff. I wonder if it solves my problem though. I couldn’t quite make out if it allows you to bypass the problem with the router not knowing which incoming messages that are supposed to be sent to the server on my computer. Do you know if it has an option for “port forwarding” on the router (or what else it might be called, I want my program to be able to tell the router that any incoming message with destination port 6677 (for example) are to be sent to my computer).

Haha that’s quite funny, a software that opens ports on any router. Noone will give you his router pw. If you get it working without knowing the password you are on the dark side and quite good at being bad :wink:
But after all you shouldn’t need to open ports.

You can’t open ports like that.

You said that you want people who aren’t good at computers to be able to host their own servers. Directing a port to a certain computer on the network isn’t being good at computers. Its not like you’re making them program something. To open a port all you have to do is change 1 option in router options, which in my opinion, is really easy.

Few things I would like to mention about my past problems with port forwarding:

  • After you restart your router/modem or whatever, internal ip addresses change;
  • Not all routers can ‘target’ their own IP address. So for example, if you’re hosting a server on your computer, and you try to join your IP (public) address, you might not be able to join it, simply because your router doesn’t have ability to connect to itself, so keep this in mind if you are unable to connect to your server;
  • Open Port checking sites which check if your port is open only work when there is a service running on that port. So even if you port forward certain port, it will be displayed as closed until you start a service on that port;

You can’t control this through Java. A prominent use of NAT’s is to divide a single IP into potentially unlimited clients/connections (NAT’s within a NAT - NATception) and are ill served when using these connections to host content since the IP could be shared by hundreds of devices.

Your ISP additionally might not like/allow hosting, especially if you haven’t paid extra.

In general, however, all of the networking mumbo jumbo sorts itself out if you stick with something basic like TCP/UDP and configure your stuff on your own end. Basically it looks like you need to (re)configure your own router.

There is no solution that will work 100% of the time to automatically open ports but many routers support the UPnP protocal and if that is enabled in the router config you can use a java library to use UPnP to open ports. See these links:

https://code.google.com/p/weupnp/
http://upnp-portmapper.sourceforge.net/

There are also other java libraries that attempt to find a way thorough routers without forwarding see here:

The best solution is of course to get a real server with a public IP address and to host the game there.

OP I think you need to learn a little more about networking first before you make a networked game. Sounds like you don’t know all too much yet, no offense.

@opiop65
No, I’m afraid I don’t know very much at all. The only knowledge I have about it is what I got from a single course in my university program and it dealt with networks as a whole so it didn’t go too deep into routers in particular. Still, at least it made me know why it never worked if I tried to host a minecraft server XD

Make a game that someone wants to play and host servers and worry about port forwarding later…