1
votes

Imagine a UDP server:

while (true) {
    try {
        socket.receive(packet);
        // handle packet (potentially CPU-intensive)
    } catch (IOException e) {
        e.printStackTrace();
    }
}

What troubles me is that I only have 1 thread to handle all packets from all clients. I assume my server can't start multiple threads to receive packets on the same socket? While of course I could handle the packets in separate threads, I still only have 1 thread to receive datapackets, and no matter how little time it takes to forward a packet to another thread, it's still a window where the server won't hear incoming packets.

So my question is: Do packets send to the server get lost if the server isn't currently blocked in server.receive(), or do they get queued for the next time the method is called? If they get lost, how can I handle multiple client?

1

1 Answers

1
votes

Do packets send to the server get lost if the server isn't currently blocked in server.receive()

No.

or do they get queued for the next time the method is called?

They are queued in the socket receive buffer, if there is room, otherwise they are lost.

how can I handle multiple client[s]?

Either process each packet fast enough so that the socket receive buffer never overflows, or start a new thread to handle each datagram.