Boost logo

Boost Users :

Subject: [Boost-users] Basic UDP socket question
From: Nguyen, Tai (tai.nguyen_at_[hidden])
Date: 2012-04-09 09:11:58


All,

I am using boost for the first time. I am seeing something that I don't understand. I hope someone can explain this to me.

I have a basic sender/receiver setup.

The sender send a message similar to the following format.

Header: contain size of the message
Body: contain the payload;

The receiver reads the Header into a tempbuffer. Then check the size, and read more into the tempbuffer with an offset of sizeof( Header ).

This works fine for TCP socket. When I switch to use UDP socket, weird behaviours happen.

Read the header into the tempbuffer. However, when I try to read the body, I also get the header again. So the temp buffer turns out to look like this.

Header
Header
Body.

Its seems as if the first read doesn't get the Header off the network stack. Does someone have an explaination for this?

Some background, pseudo code:
1) start a thread for the io_service
2) set up socket;
3) socket->async_recieve_from( buffer, sizeof(header ) ), handle_read_header )
4) handle_read_header()
        PayloadSize = header.size;
      Socket->asynch_receive_from( buffer[ sizeof( Header )], payloadSize ), handle_read_body );

If I don't give it an index offset on the second read, when finish reading the payload, the tempbuffer also contain the correct header. This seems as if the Header was never clear off the network stack after the first read.

Thanks,
tai


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net