2

OK, I am aware that UDP doesn't guarantee delivery, but I had hoped to catch all by having the RecvFrom in a thread with TimeCritical priority and just quickly moving the incoming messages into a buffer. However, when the rate of messages get up to about 1000 1500 bytes messages per seconds a few are missed. I have verified with WireShark that the messages actually are received by the computer.

I am pretty sure that the messages are lost in the extremely short time from the RecvFrom returns and until it is called again.

Is there any way to "catch all", since the messages apparently are received?

Thanks.

Jens
  • 37
  • 1
  • 4
  • No. UDP is not a reliable protocol. If you want reliability you have to build it in to your application protocol. – user207421 Dec 08 '19 at 04:56

1 Answers1

3

Is there any way to "catch all", since the messages apparently are received?

No. If you are not fast enough reading messages from the socket buffer and this receive buffer fills up, the messages simply get dropped. It does not matter if they were received on the computer and are visible with Wireshark, all what matters is if they end up in the sockets receive buffer.

You might try to increase this buffer in order to make a loss less likely but it can still happen. Unreliability of delivery is one of the trade-offs you have with UDP and there is no magic which will fix it. Either you can cope with packet loss or you have to keep track of loss and somehow request that the message gets send again.

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • Thanks. This is what I feared. However, you say that increasing the buffer size may help. So does this mean that if I increase the buffer size given to recvfrom, then I may receive more than one incoming message at the time? I was under the impression that recvfrom only returned one message at the time. – Jens Dec 08 '19 at 12:35
  • @Jens: The OS kernel puts the messages into the receive buffer. `recvfrom` takes a message out of the buffer. There can be multiple messages in the receive buffer at the same time. If the buffer is larger there is more decoupling between putting something into the buffer (kernel) and taking it out (`recvfrom`). – Steffen Ullrich Dec 08 '19 at 12:59
  • I increased the Windows buffer size with setsockopt(xSocket, SOL_SOCKET, SO_RCVBUF, @optVal, OptLen); and it cures the problem. All incoming messages are now pulled out and added to my own receive buffer. – Jens Dec 08 '19 at 18:04
  • @Jens: Glad to hear. But remember that it only makes the problem less likely and does not prevent it fully. – Steffen Ullrich Dec 08 '19 at 18:18