且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的TCP监听器不工作在3G / 4G?

更新时间:2022-12-28 15:52:13

您正试图两台设备在连接到两个不同子网的TCP连接的。这不可能。你是pretty多问一个问题:我的朋友,为什么能听到我,当他站在我的面前,但是如果他不是在另一个房间为使这项工作的唯一方法是打开大门。但是,你必须实现这个门,这就是所谓的NAT穿越或打孔,真的是一个不同的通信概念(检查Android P2P(直接连接)通过互联网(NAT后面)和安卓?NAT穿越)。

You are trying to connect two devices over TCP connected to two different subnets. That's impossible. You are pretty much asking the question: "Why can my friend hear me when he's standing in front of me, but not if he's in another room?" The only way to make that work is to open the door. But you'd have to implement this "door", which is called NAT traversal or "hole punching" and really is a different communication concept (check Android P2P (direct-connection) over the Internet (behind NAT) and Android: NAT Traversal?).

在年底,有三种解决方案,您的问题:

In the end, there are three solutions to your problem:


  1. 实施自己的服务器要连接(非常乏味)的设备之间放。

  2. 使用第三方的库如 sip2peer 或其他任何你能找到适合你。

  3. 请不要打扰。有时,它只是不值得麻烦。

  1. Implement your own server to put between those devices you want to connect (very tedious).
  2. Use a 3rd-party library like sip2peer or whatever else you can find that suits you.
  3. Don't bother. Sometimes it's just not worth the trouble.

编辑:结果这听起来像你正试图连接到运行code以上的Andr​​oid设备的Web服务器。这将是选项1,不过,Android设备无法通过互联网到达。据我所知,你必须把周围:设备将自身注册服务器上,使服务器可以种其他设备连接到它


It sounds like you have a web server that is trying to connect to an Android device running the code above. That would be option 1, but still, an Android device can't be reached over the Internet. As far as I know, you have to turn that around: The device has to register itself on the server so that the server can kind of "connect" other devices to it.