0

I am currently working on a Java-based game that can be played P2P, without a server. So far, I have been able to get it working, where one person enters the other's IP, which I get with the call

InetAddress.getLocalHost().getHostAddress());

However, this only works when two computers on the same network. How would I get an IP that works across networks? Thanks for you help!

edit: It is a 1v1 game, so multiple connections are not a problem. And I was going to have users send IPs over skype or something... this is just something I want to use with my friends. Ex. It displays the IP, you send it to your friend, he connects using it.

mattdee123
  • 213
  • 1
  • 5
  • 11

2 Answers2

1

Even the torrent P2P network uses servers to determine the ip addresses of peers. After getting the ip addresses the connections are between peers of course. So I suggest at least a web service to list the ip addresses of peers. (Or you can put something like listActivePeers.php/aspx/etc file to your website and ping to for every 5 minute and list the ip addresses that have ping'ed.)

And also I'd like to point out that every peer node needs to open a connection to another which is not so effective unless you have a few players. If you have 100 online players you will end up 100 connections for every peer and sometimes some access points or modems don't support that kind of active connection! They can become stuck or slow down.

In addition you will need to deal with massive amount of concurrency issues. Think that two data came to the two peers at the same time. Before making any move peers need to check each other "if I can make this move? Or any of you making any move should I be aware of?". So before every move you will lock other peers by sending some information over the connections you have to them, and every one of them will do this before making a move.

Update (from my comment above)
Check this link about fething your ip address. https://stackoverflow.com/a/2939223/1216609 Also google for "modem port open OR forward [brand/model of your access point / modem]" query to forwarding a port which is specific to your modem or router.

Community
  • 1
  • 1
Ata S.
  • 890
  • 7
  • 12
  • It is a 1v1 game, so multiple connections are not a problem. And I was going to have users send IPs over skype or something... this is just something I want to use with my friends. Ex. It displays the IP, you send it to your friend, he connects using it. – mattdee123 Apr 08 '12 at 09:38
  • Well displaying IP and sending it manually is the best solution if you don't use even some centalized connection listing system as I mentioned. But it is likely that the players will have problems because most of us are behind router firewalls nowadays and they need to open the port of your game, or your game can use UPnP, not a permanent solution but better than nothing. The player who sent the IP needs the port of the game accessible by router. – Ata S. Apr 08 '12 at 09:56
  • yes, my problem is that this approach only works one the same network. the address that the user enters is like "10.0.1.10" or something. How would I make this work across networks. I'm assuming id need another IP address? – mattdee123 Apr 08 '12 at 12:49
  • You are getting a local ip address, you need ip address of your internet connection. Just try to fetch it from sites like whatismyip.com. you can get the source code of page, store it in a string and fetch the ip string from it by regex or simple split functions. But still as I said the sender player of ip should forward the port from their modem or access point to their local ip addresses. also should set a static ip in network adapter of the operating system. – Ata S. Apr 08 '12 at 14:27
  • @user578602 Check this link about fething your ip address. http://stackoverflow.com/a/2939223/1216609 Also google for "modem port open OR forward [brand/model of your access point / modem]" query to forwarding a port which is specific to your modem or router. – Ata S. Apr 08 '12 at 18:49
0

This question is about finding the IP that you want: How to get the ip of the computer on linux through Java?

You will also need to use some kind of port forwarding to connect to computers on different networks.

Community
  • 1
  • 1
Richante
  • 4,353
  • 19
  • 23
  • This wouldn't work because there's no way to get the internet address if you are sending your packages via a gateway like a router or access point. You would get the same 10.0.0.something or 192.0.0.something local ip address. You need to access some server and let them tell you your ip. – Ata S. Apr 08 '12 at 18:46