2

I am building a networked application using DatagramSocket.

I have a "network" thread whose sole job is to pull DatagramPacket objects from a DatagramSocket and place the data on a BlockingQueue. It does this as fast as it is able. The queue is then consumed by other threads.

Suppose my "network" thread is running slow and packets are building up at the socket.

  • Is it possible for the socket to "overflow" and packets get lost?
  • Is there any advantage to taking packets from the socket and storing them in a POJO queue early?
  • Is my "network" thread and queue setup really necessary - should I just work with DatagramSocket directly?
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

1 Answers1

4

Is it possible for the socket to "overflow"

It is possible for your socket receive buffer to fill to the point where no new datagrams can be accepted.

and packets get lost?

It is always possible for [UDP] packets to get lost, but yes this is another reason.

Is there any advantage to taking packets from the socket and storing them in a POJO queue early?

Not really. You're just moving the problem, from needing potentially infinite network and socket receive buffer capacity to needing potentially infinite input queue capacity. You should aim just to processs incoming datagrams as fast as you possibly can, and wear the losses however you may.

Is my "network" thread and queue setup really necessary - should I just work with DatagramSocket directly?

Yep.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for the response. Is an advantage of my `BlockingQueue` setup that I can control exactly how big my message buffer is? – sdgfsdh Oct 12 '15 at 09:04
  • 1
    You can control how big your socket receive buffer is too, within limits. I don't see any advantage of keeping a dog and barking yourself. – user207421 Oct 12 '15 at 09:05
  • @Am_I_Helpful What part of 'it is *always* possible for packets to get lost' didn't you understand? – user207421 Oct 12 '15 at 09:06
  • @EJP - It's just that a proper reason must be provided, because there can be a physical barrier too. I just wanted you to be more specific at that point. BTW, I've already upvoted your answer. – Am_I_Helpful Oct 12 '15 at 09:06
  • 1
    @Am_I_Helpful So why not say so in the first place? But it doesn't appear to me that this information is actually missing from the answer, or the question either. 'Always' is still always. – user207421 Oct 12 '15 at 09:07
  • If I might add: By design, because UDP packages do not get acknowledged, as TCP packages do. – Markus W Mahlberg Oct 12 '15 at 09:16