Search code examples
javaandroidsocketslibgdxserversocket

Getting IP address of host


I created a little game and now I am trying to make a multiplayer game out of it. One android phone should host the game and the other phone should be able to connect to this server. My question is how can I tell the Client to which IP he should connect. Is there a way to scan for hosts?


Solution

  • For Android games there is a perfect (and simple) library called kryonet.

    It has a method - discoverHost - for scanning local network for devices that are listening for incoming connections.

    Also, it contains classes such as:

    that makes implementing simple communication protocol (on both UDP and TCP protocols) really easy.

    As many people uses this library (including me) - you can find a lot of examples:

    ... and many others. :)

    Update 1:

    There is also a great library from Google called Nearby Connections API that allows to autodiscover devices in local network and allows to create multiplayer (or multiscreen) games.

    Furthermore, Google has created nice (multiplatform) APIs for multiplayer games over the WAN:

    Sample for Nearby Connections and Real-time Multiplayer APIs you can find here.