如何使两款Android设备通过TCP通信 [英] How to make two android devices to communicate through TCP

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

问题描述

我们要建立两个Android设备之间的TCP / IP连接。 现在我们认为它会更简单,如果我们进行连接设备到设备。因此,有没有服务器是在两部手机之间。

大部分的时间(如果不是总是)一种没有实际的IP地址(NAT和等等)。这对于创建一个TCP套接字有问题吗? 我没能找到这个任何确切信息。有什么建议和意见将非常AP preciated。

感谢

解决方案
  

这是创建一个TCP的问题   插座?

回答是否定的,它并不能使它不可能的(除非NAT的有映射IP地址的未predictable方式)。然而,这是不容易做到。

简短的回答是,你永远需要有公网IP地址的服务器状等,以方便位于NAT之后同龄人之间的初始通信。在同龄人连接到它。一个告诉服务器就是了TCP连接与服务器B. B.通知NAT行为分析被执行,如果可能的话,A和B试图通过$ P $打开与对方的TCP通信pdicting的的之外的一个映射的NAT。如果一切顺利的罚款(通常它)一个触摸基地B或反之亦然。他们关闭其他连接尝试,并与运营TCP连接进行通信。这种技术被称为STUN的TCP。

现在,还有更复杂的原因,事情都可能出错。如果你想知道更多,我已经写了一本书,叫做实用JXTA II这可以在Scribd在线阅读。有一个专门为NAT穿越章。

希望这有助于。

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.

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.

Thanks

解决方案

Is this a problem for creating a TCP socket?

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.

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.

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.

Hope this helps.

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

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