user1503944
user1503944

Reputation: 397

UDP packet bytes read granularity?

I have UDP client and server apps, and custom protocol over UDP.

Each "protocol packet" contains header with size of payload, and payload by itself .

Each "protocol packet" not exceed MTU size, with expectation of lack of fragmentation .

Currently I'm using ASIO library and experiencing some problem:

Time diagram :

  1. client send header (2 bytes) and payload (N < MTU-2 bytes) ------>

  2. server reads only 2 bytes, to be sure about payload size.

  3. server receive header with size of payload

  4. server TRIED to receive N bytes of payload ..... and nothing . Completion handler never occurs .

If client send (for debug purposes) one more packet, server completion handler is fired - what's why I think my async loop of asio is ok .

Also if server tried to read whole transmission 2+N bytes per one read , all data received .

So I'm little bit confused . It is possible to read separate bytes of one UDP datagram sequentially by executing _socket.async_receive_from() sequentally.

Will be glad for help, Thanks in advance .

Upvotes: 0

Views: 309

Answers (1)

user207421
user207421

Reputation: 310913

It is possible to read separate bytes of one UDP datagram sequentially by executing _socket.async_receive_from() sequentally.

If this is a statement it is incorrect, and if it's a question the answer is 'no'. UDP is a datagram protocol.You get the entire datagram or nothing at all. If you read part of it the remainder is discarded.

Possibly you are looking for readv() or recvmsg(), which allow you to scatter-read.

Upvotes: 1

Related Questions