Android P2P(直接连接)通过互联网(在 NAT 后面) [英] Android P2P (direct-connection) over the Internet (behind NAT)

查看:80
本文介绍了Android P2P(直接连接)通过互联网(在 NAT 后面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始一个小项目,基本上是经典战舰游戏的多人游戏(如超过两个玩家)变体.

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

在深入编码之前,我试图解决的一个问题是多个玩家之间的沟通问题.当前的一种可能性是使用中央 HTTP 服务器作为通信的中央集线器(与 Android C2DM API 结合以允许从 HTTP 服务器到设备的推送通信).这似乎是一个不错的解决方案,因为理论上只要您可以访问 Internet,它就应该可以完美运行,无论您是否在 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.

然而,所提出的解决方案存在单点故障/额外负载(网络服务器)的缺点.所以我想尝试其他选择.我想过在客户端之间使用 Sockets 建立直接连接(网络服务器仅用作初始会面点),但是只有当所有设备都在同一网络中时,这才能很好地工作.考虑到今天我们几乎总是在路由器的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 后面的 Android 设备之间实现可靠的打孔(最好通过 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.

推荐答案

通过 smack 在 gtalk 上使用 xmpp.您不必担心服务器和单点故障.让谷歌担心!我编写了俄罗斯方块,让它与使用 gtalk 作为通信层的两个玩家对战.http://code.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.

这篇关于Android P2P(直接连接)通过互联网(在 NAT 后面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆