8

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
John Smith
  • 95
  • 1
  • 6

2 Answers2

6

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.

But the primary concept to understand here is,

  1. 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.

  2. 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.

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.

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.

It seems to have been compiled for android as well here

fkl
  • 5,412
  • 4
  • 28
  • 68
  • It might be a dump question. I have read the link you had put. But I still do not get how the iphone game "Draw something" works. I don't think they go through a central server. Or the same thing with say facebook chat. I do not think they are push notifications either. My question is what is the technology they are using? Or my ultimate question is, is there a way to have two phones communicate without a central server behind two different routers? – John Smith Nov 15 '12 at 06:26
  • If you know which ports on both routers forward traffic to both end points and what external ips you should use which map to your destinations (of course assuming your both end points are listening), then you can connect directly without going through a central server. The central server or above protocols do some initial work to get this info. After that the connections are one to one. I am not sure about draw something, but in general many other p2p software which allows one to one communication via NAT use help from some external entity in the beginning. – fkl Nov 16 '12 at 06:05
  • facebook chat is in browser which means it goes over http. So of all others, i am quite positive that it does go through a central server as it is not a p2p application in the first place. Skype, torrents, gtalk chat client etc are better examples of p2p software. But even they might involve some degree of central server communication such as user authentication – fkl Nov 16 '12 at 06:08
  • Ok so you are saying that if I have the port number of which the router connected to my server to initially send some data to it for both endpoints and also their public and private IPs I can actually start a direct connection between them without STUN or ICE implementation? – John Smith Nov 18 '12 at 20:19
  • Yeah. Your router would have some NAT rule like "forward traffic received on port say 2550 to local ip 192.168.8.35 on port 4500" and your other peer socket application is listening on 192.168.8.35 then you can connect directly. Similar something for the way back. Whenever there is NAT, there is already some rule like above. You just need to know that for your both ends. – fkl Nov 19 '12 at 03:59
  • Another technology that can help with this is UPnP. – Sarel Botha Dec 21 '12 at 20:10
  • Oh thanks for adding that @Sarel Botha. I some how missed the comment earlier. I hadn't read much about it UPnP until now in the context of original question. It sure adds to my knowledge. Appreciate that. – fkl Oct 22 '13 at 06:06
  • any concrete example ? – Sagar Nayak Mar 10 '16 at 08:00
  • The question was about genuine guidance on approach. Do some effort yourself, then if you are stuck and can't make it work, put your code here, and would gladly help. – fkl Mar 12 '16 at 04:16
0

"My question is if that is true and if so how does LogMeIn or the other p2p applications work behind different routers?"

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.

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.

bobjandal
  • 2,313
  • 2
  • 15
  • 8
  • I would object to the wording. Although techniques to by pass NAT or other scenarios do some times use central servers, or authentication is often done via central servers, but still p2p by definition is peer to peer direct connections. Read about the techniques i mentioned in the answer. All torrents though initially advertised via trackers ultimately result in a lot of one to one connections. Same applies to the rest of p2p software – fkl Nov 11 '12 at 19:41
  • Moreover, if you know the ip and port to a machine which is reachable and is allowing incoming connections with a listener application, then you can connect via tcp or udp or any other protocol to any one across the whole internet. TCP has nothing to do with reachability. It just maintains the state of a connection and makes it reliable on top of ip by retransmissions, ordering, acks etc – fkl Nov 11 '12 at 19:44
  • "All torrents though initially advertised via trackers ultimately result in a lot of one to one connections" - yes, but you have to have a port forwarded on your router. I assumed John didn't want to have to reconfigure routers for his solution. What I was saying can't be done with TCP is hijacking a connection from the target machine to another machine like what John is trying to do here. – bobjandal Nov 12 '12 at 10:06
  • STUN automates doing that for him to a large extent. Your router is using at least some port to forward traffic to you already. It just figures that out for you without manually having to add the rule – fkl Nov 12 '12 at 10:18