Pregunta

I have an application where multiple servers could exist. There are heaps of examples of how to use UDP to discover servers but it seems this only works with a single server.

What happens if multiple responses exist? Are they queued, corrupted (with UDP pitfalls) or something else ?

I would like to find out how to receive multiple responses from a UDP broadcast sent from an Android Device. If this isn't viable, is there any other recommended approach for multiple server discovery for Android clients..

Thank you

¿Fue útil?

Solución 2

If you broadcast the message and the servers all return you should see all the responses as they come back.

However be aware that UDP is a potentially lossy protocol and makes no guarantees at all. Over a non-wireless LAN with decent switches it is pretty safe but as soon as it goes further than that (wireless, over multiple networks, etc) you can expect to lose at least some packets and any packet loss is a message loss on UDP.

The usual solution to this is to send each message a few times. So for example when you first start up you might broadcast at 1 second, 10 second, 30 seconds, and then every 10 minutes thereafter. This will find servers immediately, then sweep up any it missed fairly fast, and then finally detect any new ones that appear on the network.

I've not worked with this sort of system for quite a few years, but last time we did there was a single server acted as the center point. Everything when it started up broadcasted out to find the central server (retrying at increasing intervals until it found it) and when the central server started up it broadcasted out to find everything - retrying 3 times.

All communication after that was done by registering with that central server and getting the list of apps etc from there. The server essentially acted as a network directory so anything could get a list of anything else on the network by querying it.

Otros consejos

I would first send a packet to all servers you want to ask if they are there, then let all servers respond. Since you want to find out how to receive the packages, here is how i would do that:

long responseTimeout = 4000;
long start = System.currentTimeMillis();
while(true){
    long now = System.currentTimeMillis();
    if(now - start < responseTimeout){
        datagramSocket.setSoTimeout((int) (responseTimeout - (now - start));
    }else{
        break;
    }
    try{
        datagramSocket.receive(packet);
        addOnlineServer(packet.getAddress());
    }catch(SocketTimeoutException e){
        break;
    }
}

For a certain amount of time your android client should wait for responses, and add each ip of the received package to a list of online servers.

Sure some of the packages could get lost as you are using UDP, but that's what you get. If you want to be sure that no packages get lost, use TCP instead.

You should be doing the following to receive and probably also send broadcast packets (which is what you are asking for):

  1. Make sure that network mask is correct

  2. When you bind the socket, be sure to bind it to INADDR_ANY

  3. Set the socket's option to BROADCAST via setsockopt

  4. Call the function sendto(..) with sendaddr.sin_addr.s_addr = inet_addr("your_interface_broadcast_address"), or call sento(..) several times for each interface with its broadcast IP address.

  5. Call the function recvfrom(..), inside a while(..) loop, until you are certain "enough time has passed", usually 1 second should suffice on a LAN network

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top