Skip to content
Advertisement

android socket programming behind a router

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement