互联网上的 Android P2P(直接连接)(在 NAT 之后)

Android P2P (direct-connection) over the Internet (behind NAT)(互联网上的 Android P2P(直接连接)(在 NAT 之后))

本文介绍了互联网上的 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.

但是,所提出的解决方案的缺点是存在单点故障/额外负载(网络服务器).所以我想尝试其他选择.我曾想过在客户端之间使用套接字进行直接连接(网络服务器仅用作初始会面点),但是只有在所有设备都在同一个网络中时这才能正常工作.考虑到今天我们几乎总是在路由器的 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 之后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:互联网上的 Android P2P(直接连接)(在 NAT 之后)

基础教程推荐