Android Question What is the best way to automatically find the server?

rtek1000

Active Member
Licensed User
Longtime User

Computersmith64

Well-Known Member
Licensed User
Longtime User
Similar to the link the fixit30 directed you to, I use UDP broadcast, then connect via TCP. The difference between what I do & Erel's example is that I don't need the user to tap a "Connect" button as the connection happens automatically once they have started the process. It goes something like this:

1) All users choose the "Start network game" option on their device;
2) All devices initially listen for a UDP broadcast message that identifies the server;
3) If they don't receive the UDP broadcast message within a certain time, they start sending one themselves identifying themselves as the server, plus they initialize a TCP server socket;
4) If they do receive a UDP broadcast message, they initialize a TCP client socket & connect to the IP address in the UDP message.

So the end result is that 1 device ends up being the server & all the others automatically connect as clients.

In cases where UDP broadcast is blocked on the network, I provide a manual connect via IP addresses option.

- Colin.
 
Upvote 0
Top