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?