如何让两个android设备通过TCP进行通信 [英] How to make two android devices to communicate through TCP

查看:37
本文介绍了如何让两个android设备通过TCP进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想在两个安卓设备之间建立 TCP/IP 连接.现在我们认为如果我们将设备连接到设备会更简单.所以两部手机之间没有服务器.

We want to establish TCP/IP connection between two android devices. For now we thought that it would be simpler if we make the connection device to device. So there is no server that is between the two phones.

大多数时候(如果不是总是)没有真实的 IP 地址(NAT 等).这是创建 TCP 套接字的问题吗?我没有找到任何确切的信息.任何建议和意见都将受到高度赞赏.

Most of the time (if not always) one has no real IP address (NAT and so on). Is this a problem for creating a TCP socket? I didn't manage to find any exact information for this. Any advice and opinion will be highly appreciated.

谢谢

推荐答案

这是创建 TCP 的问题吗插座?

Is this a problem for creating a TCP socket?

答案是否定的,这并不意味着不可能(除非 NAT 具有不可预测的 IP 地址映射方式).但是,要做到这一点并不容易.

Answer is no, it does not make it impossible (unless NAT's have an unpredictable way of mapping IP addresses). However, it is not simple to accomplish.

简短的回答是,您总是需要一个具有公共 IP 地址的类似服务器的对等点,以促进位于 NAT 后面的对等点之间的初始通信.对等点连接到它.A 告诉服务器它想与 B 建立 TCP 连接.服务器通知 B.执行 NAT 行为分析,如果可能,A 和 B 通过预测 other 的下一个映射来尝试打开彼此的 TCP 通信NAT.如果一切顺利(通常情况下),A 与 B 接触基地,反之亦然.它们关闭其他连接尝试并与可操作的 TCP 连接进行通信.这种技术称为 TCP 的 STUN.

The short answer is you'll always need a server-like peer with a public IP address to facilitate initial communication between peers located behind NATs. The peers connect to it. A tells server it wants to TCP connect with B. Server notifies B. NAT behavior analysis is performed and if possible, A and B attempt to open TCP communications with each other by predicting the next mapping of the other NAT. If all goes fine (and usually it does) A touches base with B or vice-versa. They close the other connection attempts and communicate with the operational TCP connection. This technique is called STUN for TCP.

现在,出现问题的原因更为复杂.如果您想了解更多信息,我已经写了一本名为 Practical JXTA II 的书,可以在 Scribd 在线阅读.有一章专门介绍 NAT 穿越.

Now, there are more complicated reason why things can go wrong. If you want to know more, I have written a book called Practical JXTA II which available for reading online at Scribd. There is a chapter dedicated to NAT traversal.

希望这会有所帮助.

这篇关于如何让两个android设备通过TCP进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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