0

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 .

user1503944
  • 387
  • 2
  • 16
  • How does the server read these 2 bytes? And, what does the receive read handler say for bytes transferred? My guess is that this read gets all of the data, not just two bytes. – Ton Plooij May 11 '17 at 09:51
  • 2 bytes received correctly, "bytes transferred" contain exactly 2 bytes as requested by _socket.async_receive_from() . So I need someone who confirm my concerns , and it's look like where are some explanations already . Thanks . – user1503944 May 11 '17 at 09:57

1 Answers1

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for explanation. In my case "one datagram per time" is much better solution, than partial reading . Because each packet of protocol is self-sufficient and size of protocol < MTU, final code implementation will be not so complicated, in comparison of partial reading . Anyway thanks for confirmation . Going to mark this answer as solution. – user1503944 May 11 '17 at 10:01