Reputation: 259
I am trying to figure out how to implement a client that is constantly 'ready' or 'listening' for UDP packets from a server.
Do I essentially need to be constantly keeping socket.receive(packet)
going until I receive one and then once I do, open it again?
Should I set a significant socket.setSoTimeout()
time so that it loops minimal times?
What I need to do:
Be able to request particular packets from the server and then, in an indefinite amount of time later receive any number of packets
Upvotes: 1
Views: 2118
Reputation: 77177
Depending on how high-volume your traffic is, you might look at NIO, though it's a significantly more complicated option to understand and employ.
If you have just a basic application, then start a thread that just blocks on receive(packet)
. Whenever it returns (a packet has arrived), dispatch the packet to some sort of handler. If there's low traffic, this might be a BlockingQueue
that some other thread is listening to; if you have high traffic, you might send a job to an ExecutorService
to be handled in parallel.
Upvotes: 1