Hey guys I have a question or problem. I am trying to make a p2p connection between two android phones. I have each phone connect to my server and I get their private (192.168.1.1) and public (76.123.288.22) IP and along with the ports that they connect to my server. I send a response when the phone connects to the server to open a server socket with a specific port. I also send the other phone the public IP and port of the open socket, but it does not connect. I have read multiple threads on here that it is not possible to do p2p connection if both phones or computers are behind two different routers. My question is if that is true and if so how does LogMeIn or the other p2p applications work behind different routers? Is it a programming error or the network architecture does not allow it?
嘿家伙,我有一个问题或问题。我想在两个Android手机之间建立一个p2p连接。我将每部手机连接到我的服务器,我得到他们的私人(192.168.1.1)和公共(76.123.288.22)IP以及他们连接到我的服务器的端口。当手机连接到服务器以打开具有特定端口的服务器套接字时,我会发送响应。我还发送另一部手机的公共IP和开放式插座的端口,但它没有连接。我在这里读过多个线程,如果手机或计算机都在两个不同的路由器后面,就不可能进行p2p连接。我的问题是,如果这是真的,如果是这样,LogMeIn或其他p2p应用程序如何在不同的路由器后面工作?是编程错误还是网络架构不允许?
2 个解决方案
#1
5
There are several techniques to achieve this - STUN, TURN, ICE to name a few. You can read about each of them and softwares such as skype, gtalk (now google talk Mar 2018) etc. use these techniques among others.
有几种技术可以达到这个目的--STUN,TURN,ICE等等。你可以阅读他们每个人和软件,如skype,gtalk(现在google talk Mar 2018)等。使用这些技术等。
But the primary concept to understand here is,
但这里要理解的主要概念是,
-
You need an ip which is publicly reachable so that you can connect to it. If it is behind a router on a private network, then you need port forwarding on the router i.e. you need to add a rule to that router to forward traffic received on a particular port to your server behind the router. To a certain extent, the above techniques achieve this implicitly or by involving an external 3rd party server.
您需要一个可公开访问的IP,以便您可以连接到它。如果它位于专用网络上的路由器后面,那么您需要在路由器上进行端口转发,即您需要向该路由器添加规则,以将特定端口上收到的流量转发到路由器后面的服务器。在某种程度上,上述技术隐含地或通过涉及外部第三方服务器来实现这一点。
-
You need to allow incoming connections on the machine which is connected to at first. Generally windows or linux firewalls block all incoming connections unless an exception is added. This would probably be needed for both of your nodes.
您需要首先允许连接到的计算机上的传入连接。除非添加了异常,否则Windows或Linux防火墙通常会阻止所有传入连接。这两个节点可能都需要这样做。
For the last part, i don't program on android so am not really sure if it allows adding rules for incoming connections etc. But i do know that gtalk has a client for android and gtalk uses XMPP which internally can use any of the techniques i pointed out above. So there is no reason to believe why it can't be done for android phone.
对于最后一部分,我不在Android上编程所以我不确定它是否允许为传入连接等添加规则但是我知道gtalk有一个用于android的客户端和gtalk使用XMPP在内部可以使用任何技术我在上面指出。所以没有理由相信为什么它不能为Android手机做。
Adding some more useful references:
添加一些更有用的参考:
libjingle (updated the link Mar 2018) is an open source library by google which can be used to write p2p applications including text, audio and video.
libjingle(更新链接Mar 2018)是谷歌的一个开源库,可用于编写包括文本,音频和视频在内的p2p应用程序。
It seems to have been compiled for android as well here
它似乎也已经为Android编译了
#2
0
"My question is if that is true and if so how does LogMeIn or the other p2p applications work behind different routers?"
“我的问题是,如果这是真的,如果是这样,LogMeIn或其他p2p应用程序如何在不同的路由器后面工作?”
The difference here is that the connections for both machines keep going thru the central server - they don't connect directly to each other (except for special cases).
这里的区别在于两台机器的连接都通过*服务器 - 它们不直接相互连接(特殊情况除外)。
What you want to do does not work, by design. If TCP could do that, then anyone could connect to anyone.
根据设计,你想做的事情是行不通的。如果TCP可以做到这一点,那么任何人都可以连接到任何人。
You need to make your central server pass the traffic from one phone to the connection the other phone has made with the server.
您需要让*服务器将流量从一部手机传递到另一部手机与服务器的连接。
Or set up a VPN, or use Google Chrome to phone (name may have changed) which is a means to send small messages to phones, regardless of network topology.
或设置VPN,或使用谷歌浏览器拨打电话(名称可能已更改),这是向电话发送小消息的一种方式,无论网络拓扑如何。
#1
5
There are several techniques to achieve this - STUN, TURN, ICE to name a few. You can read about each of them and softwares such as skype, gtalk (now google talk Mar 2018) etc. use these techniques among others.
有几种技术可以达到这个目的--STUN,TURN,ICE等等。你可以阅读他们每个人和软件,如skype,gtalk(现在google talk Mar 2018)等。使用这些技术等。
But the primary concept to understand here is,
但这里要理解的主要概念是,
-
You need an ip which is publicly reachable so that you can connect to it. If it is behind a router on a private network, then you need port forwarding on the router i.e. you need to add a rule to that router to forward traffic received on a particular port to your server behind the router. To a certain extent, the above techniques achieve this implicitly or by involving an external 3rd party server.
您需要一个可公开访问的IP,以便您可以连接到它。如果它位于专用网络上的路由器后面,那么您需要在路由器上进行端口转发,即您需要向该路由器添加规则,以将特定端口上收到的流量转发到路由器后面的服务器。在某种程度上,上述技术隐含地或通过涉及外部第三方服务器来实现这一点。
-
You need to allow incoming connections on the machine which is connected to at first. Generally windows or linux firewalls block all incoming connections unless an exception is added. This would probably be needed for both of your nodes.
您需要首先允许连接到的计算机上的传入连接。除非添加了异常,否则Windows或Linux防火墙通常会阻止所有传入连接。这两个节点可能都需要这样做。
For the last part, i don't program on android so am not really sure if it allows adding rules for incoming connections etc. But i do know that gtalk has a client for android and gtalk uses XMPP which internally can use any of the techniques i pointed out above. So there is no reason to believe why it can't be done for android phone.
对于最后一部分,我不在Android上编程所以我不确定它是否允许为传入连接等添加规则但是我知道gtalk有一个用于android的客户端和gtalk使用XMPP在内部可以使用任何技术我在上面指出。所以没有理由相信为什么它不能为Android手机做。
Adding some more useful references:
添加一些更有用的参考:
libjingle (updated the link Mar 2018) is an open source library by google which can be used to write p2p applications including text, audio and video.
libjingle(更新链接Mar 2018)是谷歌的一个开源库,可用于编写包括文本,音频和视频在内的p2p应用程序。
It seems to have been compiled for android as well here
它似乎也已经为Android编译了
#2
0
"My question is if that is true and if so how does LogMeIn or the other p2p applications work behind different routers?"
“我的问题是,如果这是真的,如果是这样,LogMeIn或其他p2p应用程序如何在不同的路由器后面工作?”
The difference here is that the connections for both machines keep going thru the central server - they don't connect directly to each other (except for special cases).
这里的区别在于两台机器的连接都通过*服务器 - 它们不直接相互连接(特殊情况除外)。
What you want to do does not work, by design. If TCP could do that, then anyone could connect to anyone.
根据设计,你想做的事情是行不通的。如果TCP可以做到这一点,那么任何人都可以连接到任何人。
You need to make your central server pass the traffic from one phone to the connection the other phone has made with the server.
您需要让*服务器将流量从一部手机传递到另一部手机与服务器的连接。
Or set up a VPN, or use Google Chrome to phone (name may have changed) which is a means to send small messages to phones, regardless of network topology.
或设置VPN,或使用谷歌浏览器拨打电话(名称可能已更改),这是向电话发送小消息的一种方式,无论网络拓扑如何。