Android的P2P(直接连接)通过互联网(NAT后面)互联网、后面、直接、Android

2023-09-12 11:18:27 作者:我身上有wifi

我开始一个小项目,基本上是一个多人(如在两个以上的玩家)的经典战舰游戏的变化。

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

这是我试图解决之前,我潜入编码的一个问题是多个玩家之间的沟通问题。的电流可能是使用一个中央HTTP服务器作为用于通信的中心毂(加上机器人C2DM API,允许从HTTP服务器推送的通信的设备)。这似乎是一个很好的解决方案,因为从理论上讲,只要你能上网就应该很好地工作,不管你是在NAT之后还是不行。

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.

然而,所提出的解决方案具有现有的单点故障/额外的负担(Web服务器)中的缺点。所以我想尝试其他的选择。我想使用的客户端(与只是被用来作为初始交汇点的网络服务器)之间的插座直接连接的,然而,这只会工作,如果所有设备都在同一个网络。考虑到今天我们几乎都是路由器的NAT之后我怎么能实现直接沟通?我一直在阅读有关打孔,但我找不到任何好的库,是有据可查(含使用很好的例子),以及适用于Android的肯定。另外大部分(如果不是全部)打孔技术(STUN,ICE等)广泛仅适用于UDP,这是好的音频/视频和实时多人游戏,可能会失去一些消息,但对于一个多人转基于游戏里,以保证每一个回合的数据的传递是很重要(的东西,它不可能直接与UDP)。

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).

因此​​,任何想法如何实现机器人 NAT设备后面之间的可靠打孔(preferably通过TCP)?它没有对的情况下,100%的工作(有些陌生的NAT可能不支持),但它会很好,如果它的工作在大多数情况下。

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.

推荐答案

使用XMPP通过了咂嘴,每增加。您不必担心服务器和单点故障。让谷歌担心!我写的俄罗斯方块,使之使用,每增加一个通信层对两名球员发挥。 HTTP://$c$c.google.com/p/tetrads-drop-lite/ < /一>您可以尝试MUC如果你想要更多的球员。

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.