EDIT!
Just read that read will block until the buffer is full. How on earth to I receive smaller packets with out having to send 1MB (my max buffer length) each time? What If I want to send arbitrarily length messages?
In Java you seem to be able to just send a char array without any worries. But in C++ with the boost sockets I seem to either have to keep calling socket.read(...) until I think I have everything or send my full buffer length of data which seems wasteful.
Old original question for context.
Yet again boost sockets has me completely stumped. I am using
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket;I
used the boost SSL example for guidance but I have dedicated a thread
to it rather than having the async calls.The first
socket.read_some(...)of the socket is fine and it reads
all the bytes. After that it reads 1 byte and then all the rest on the
nextsocket.read_some(...)which had me really confused. I then
noticed that read_some typically has this behaviour. So I moved to
boost::asio::readas socket does have a member function read which
surprised me. However noticed boost::asio has a read function that
takes a socket and buffer. However it is permanently blocking.//read blocking data method //now bytesread = boost::asio::read(socket,buffer(readBuffer, max_length)); << perminatly blocks never seems to read. //was //bytesread = socket.read_some(buffer(readBuffer, max_length)); << after the 1st read it will always read one byte and need anothersocket.read_some(…) call to read the rest.
What do I need to do make
boost::asio::read(...)work?note .. I have used wireshark to make sure that the server is not
sending the data broken up. The server is not faulty.
Read with
read_some()in a loop merging the buffers until you get a complete application message. Assume you can get back anything between 1 byte and full length of your buffer.Regarding “knowing when you are finished” – that goes into your application level protocol, which could use either delimited messages, fixed length messages, fixed length headers that tell payload length, etc.