android socket programming behind a router

后端 未结 2 1632
攒了一身酷
攒了一身酷 2021-01-12 08:09

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) a

相关标签:
2条回答
  • 2021-01-12 08:26

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

    0 讨论(0)
  • 2021-01-12 08:36

    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

    0 讨论(0)
提交回复
热议问题