[Asio] [Boost.ASIO] async_read cannot read full data from client
** I have a server created using Boost.ASIO (thanks to sehe from Stackoverflow). The server is supposed to received message from client, parse it as json and correspondingly send the message back to client. To make life easier, i have considered very simple cases: (i) Client can send {"hello":"client"}, server should send response {"hello":"server"} (ii) Client can send {"message" : "Some Message"}, server should send {"response" : "OK"} The server runs, accepts data from client but cannot call the handler of async_read. The doc states that The asynchronous operation will continue until one of the following conditions is true: 1. The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes. 2. An error occurred. I think in my case the buffer is not full, thus the handler function is not getting called. How should I change my code so that server can read the message sent by client, process it and send back correct response.? PS: If the code is not attached, http://pastebin.com/pi192t6i is the link to file. *-- Sarvagya PantKathmandu, Nepal+9779803468257*
On 07/29/2015 06:28 AM, Sarvagya Pant wrote:
I think in my case the buffer is not full, thus the handler function is not getting called. How should I change my code so that server can read the message sent by client, process it and send back correct response.?
It is easiest to introduce a message separator into your wire protocol. For instance, you could terminate each message with a newline. Then you can use async_read_until().
Hi Bjorn,
thanks for your response. While sending message from client I used \q\q as
message separator and used async_read_until using the same as message
separator. Now while client received the message as expected, the server
crashed giving the error:
**Debug Assertion Failed! Expression: string iterator not deferenceable"
Thank you.
On Wed, Jul 29, 2015 at 2:57 PM, Bjorn Reese
On 07/29/2015 06:28 AM, Sarvagya Pant wrote:
I think in my case the buffer is not full, thus the handler function is
not getting called. How should I change my code so that server can read the message sent by client, process it and send back correct response.?
It is easiest to introduce a message separator into your wire protocol. For instance, you could terminate each message with a newline. Then you can use async_read_until().
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- *Sarvagya Pant* *Kathmandu, Nepal* *+9779803468257*
On 07/29/2015 11:42 AM, Sarvagya Pant wrote:
separator. Now while client received the message as expected, the server crashed giving the error:
**Debug Assertion Failed! Expression: string iterator not deferenceable"
This is likely due to your messageTemp variables. When you pass a buffer to async_write, then you must make sure that it exits until the write operation is done.
Thanks Bjorn, I have solved the problem by making a shared pointer inside
tcp_connection class **boost::shared_ptrstd::string messageSend;** .
While the server now works fine, but for the client created by me, the
client received gets correct response the first time but it will not get
response afterwards and gives error **system:10053 receive: An established
connection was aborted by the software in your host machine**
The client will attempt to connect to socket and kept on loop forever to
communicate with client. Doesn't the boost keep reference of current alive
connection? Please correct me.
Thanks.
On Wed, Jul 29, 2015 at 5:59 PM, Bjorn Reese
On 07/29/2015 11:42 AM, Sarvagya Pant wrote:
separator. Now while client received the message as expected, the server
crashed giving the error:
**Debug Assertion Failed! Expression: string iterator not deferenceable"
This is likely due to your messageTemp variables. When you pass a buffer to async_write, then you must make sure that it exits until the write operation is done.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- *Sarvagya Pant* *Kathmandu, Nepal* *+9779803468257*
participants (2)
-
Bjorn Reese
-
Sarvagya Pant