'asio socket, split incoming data at a delimitator?

I am reading data from a asio socket in c++.

I need to parse the incoming data as json. To do this, i need to get a single json string entry. I am adding a character ';' at the end of the json string, now i need to split at that character on read. i am trying this:

int main()
{
    asio::io_service service;
    asio::ip::tcp::endpoint endpoint(asio::ip::address::from_string("127.0.0.1"), 4444);
    asio::ip::tcp::socket socket(service);
    std::cout << "[Client] Connecting to server..." << std::endl;
    socket.connect(endpoint);
    std::cout << "[Client] Connection successful" << std::endl;


    while (true)
    {

        std::string str;
        str.resize(2048);
        asio::read(socket, asio::buffer(str));

        std::string parsed;
        std::stringstream input_stringstream(str);


        if (std::getline(input_stringstream, parsed, ';'))
        {
                            
            std::cout << parsed << std::endl;
            std::cout<<std::endl;
        }

    

    }
}

But it gives me random sections of the string.

The full message is: (for testing, not json formatted)

this is the message in full, no more no less ;

and I get:

 full, no more no less

this is the message in full, no more no less

ull, no more no less

is is the message in full, no more no less

l, no more no less

 is the message in full, no more no less

 no more no less

Where am i going wrong here?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source