24

I'm starting a small project, basically a multiplayer (as in more than two players) variation of the classic Battleship game.

One problem that I'm trying to resolve before I dive into coding is the problem of communication between the multiple players. A current possibility is to use a central HTTP server as the central hub for communication (coupled with the Android C2DM API to allow push communication from the HTTP server to the devices). This seems a nice solution, because in theory as long as you have access to the Internet it should work perfectly, whether you are behind a NAT or not.

However, the proposed solution has the disadvantage of existing one single point of failure/extra load (the webserver). So I'd like to try other options. I thought of making direct connections using Sockets between the clients (with the webserver just being used as an initial meeting point), however this would only work well if all the devices were in the same network. Considering that today we are almost always behind the NAT of a router how can I achieve direct communication? I've been reading about hole punching but I can't find any good library that is well documented (containing good examples of use) and that works on Android for sure. Also most (if not all) hole punching techniques (STUN, ICE, etc...) widely available only work with UDP, which is fine for audio/video and real time multiplayer games that can lose some messages, but for a multiplayer turn-based game it's important to guarantee the delivery of the data of each turn (something that it's not possible directly with UDP).

So any ideas how to achieve a reliable hole punching (preferably over TCP) between Android devices behind NAT? It doesn't have to work on 100% of the cases (some stranger NATs may not be supported) but it would be nice if it worked on most cases.

petersaints
  • 1,899
  • 3
  • 18
  • 25
  • 1
    The solution presented by Win Myo Htet has a nice potential (it uses Google's own infrastructure). However, I'm still interested in a good solution for TCP and/or UDP hole punching on Android. – petersaints Apr 04 '12 at 16:34
  • 1
    what did you end up with @petersaints ? – kishu27 Apr 26 '17 at 21:08

3 Answers3

10

use xmpp via smack over gtalk. You don't have to worry about server and single point of failure. let google worry about that! I have written Tetris to make it play against two player using gtalk as a communication layer. http://code.google.com/p/tetrads-drop-lite/ You can try MUC if you want more player.

Win Myo Htet
  • 5,377
  • 3
  • 38
  • 56
  • I also thought of using XMPP. What version of Smack have you used? It seems that the original project is not built for Android but there are some ports. Also, is it easy to send data via XMPP? You can only send text or is it possible to send binary data? – petersaints Apr 04 '12 at 16:11
  • 1
    I don't use the binary but the source for that I have to make a few changes. You can send over the binary as a file transfer between two players not to the MUC. However at that time, the binary transfer is not that robust because of differences in how google has implemented the protocol and smack implementation itself it not very robust. Good news is that the new smack version is recently out and it has addressed the file transfer, I heard. I have not checked that yet tho. – Win Myo Htet Apr 04 '12 at 16:24
  • Well, yeah! That might work. Thanks for the tips. Could you just tell if the Smack version you used was from here (I think this is the official one): http://www.igniterealtime.org/downloads/index.jsp#smack If not, what Android port did you use? I'll mark the message as accepted later. Because I'm still interested in direct TCP / UDP connection but this is a nice workaround (and probably less hacky than hole punching). – petersaints Apr 04 '12 at 16:31
  • yeh, don't worry about answer. you still can wait for other to give you alternative advice. ping me when you get different answer and accepted it cause I am interested in it too. – Win Myo Htet Apr 04 '12 at 16:36
  • Well the option was to use Google infrastructure. A mix of XMPP and Push Notifications did the trick. However it's a shame that it's not possible to do more direct approaches. I just hope IPv6 eventually solves the problem of having to deal with NAT. – petersaints Nov 20 '12 at 15:56
  • @WinMyoHtet I have a few questions maybe you can answer from your personal experience on this. I'm not sure how to chat about those. If you're available for a small interchange of Q&A, please reply. – kishu27 Apr 26 '17 at 20:47
  • @kishu27 what do you wanna know – Win Myo Htet Apr 27 '17 at 04:35
1

UDP is not reliable delivery but a you can make it reliable by requiring that send UDP packets require acknowledgements be return. This, along with a few other requirements, is what makes TCP reliable over IP (which is unreliable to begin with).

As a note, this is possible to implement but will probably be time consuming and the cost/benefit may not work out in your situation.

0

You're pretty much forced to use an intermediary. You can look up Natblaster for a mechanism that will work for establishing TCP connections between some NATed devices, but it's not something that you can use in Android without rooting both devices. And even then, it's experimental.

The best is probably to use an existing federated messaging system like jabber.

vipw
  • 7,593
  • 4
  • 25
  • 48