UDP - Read data from the queue in chunks
Asked Answered
A

2

3

I'm implementing a small application using UDP (in C). A server sends to a client the data from a given file in chunks of given amount (ex. 100 bytes / call). The client downloads the file and saves it somewhere. The catch is that the client can receive a parameter saying how many bytes to read / call.
My problem is when the server sends 100 bytes / call, and the client is set to read only 15 bytes / call. The other 85 bytes are lost, because the message is removed from the UDP queue.

Is there a way to read these messages in chunks without removing them from the queue until they're completely read?

Alternation answered 16/3, 2013 at 7:46 Comment(5)
Change your client server protocol to avoid the two ends confusing each otherSough
That would be great, but I didn't create the protocol. I have to do this small app that for my computer networks course and the protocol was given.Alternation
ok let me put it another way. "when the server sends 100 bytes / call, and the client is set to read only 15 bytes / call" You must control one end or the other. Just don't set the client and server to read the wrong number of bytes!Sough
The real question is, why is the client only reading 15 bytes to begin with? Obviously the protocol demands a larger buffer than that be used.Binding
@AdiUlici - I found this answer to be usefull: #12411631Physicochemical
B
5

UDP does not allow for chunked reading like TCP does. Reading a UDP message is an all-or-nothing operation, you either read the whole message in full or none of it at all. There is no in-between. Because of that, UDP-based protocols either use fixed-sized messages, or require both parties to dynamically negotiate the message sizes (like TrivialFTP does, for example).

There is no reason for a UDP protocol to require sending a byte size for each message. The message size itself implicitly dictates the size of the data inside of the message.

If you absolutely must determine the message size before actually reading the message, you could try calling recvfrom() with the MSG_PEEK flag, and give it a large buffer to copy data into (at least 64K, which a UDP message will never exceed, unless you are using IPv6 Jumbograms, but that is a separate issue). The output will tell you the actual size of the message that is still in the queue. However, if you go this route, then you may as well just drop the MSG_PEEK flag and always read using 64K buffers so there is no possibility of dropping data due to insufficient buffer sizes.

Binding answered 16/3, 2013 at 8:26 Comment(1)
Thanks for your answer. This is the same conclusion I got while researching the problem. +1 for the good explaining.Alternation
B
1

You can create a Thread to read the data from UDP Buffer infinitely And save the data to a circle-buffer. Than the client consume the data with your speed. If the Buffer is overflow,You can do nothing.Because the server's sending speed is quicker than the client's.

Bailee answered 16/3, 2013 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.