C++ boost asio : simple server/client with boost:asio::read and boost::asio::write passing a vector of int

Advertisements

server.cpp

#include <boost/asio.hpp>
#include <vector>
using boost::asio::ip::tcp;

int main()
{
    boost::asio::io_service ctx;
    std::vector<int> vc = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    tcp::acceptor s(ctx, tcp::endpoint({}, 1234));
    tcp::socket conn = s.accept();
    boost::asio::write(conn, boost::asio::buffer(vc));
} 

client.cpp

#include <boost/asio.hpp>
#include <iostream>
using boost::asio::ip::tcp;

int main()
{
    boost::asio::io_service ctx;
    tcp::socket s(ctx);
    s.connect(tcp::endpoint({}, 1234));
    std::vector<int> data(10);
    boost::asio::read(s, boost::asio::buffer(&data, sizeof(data)));

    for (auto x : data)
    {
        std::cout << x;
    }
}

What I expected: I expect server.cpp send a vector of int {} to the client

std::vector<int> vc = {1, 2, 3, 4, 5, 6, 7, 8, 9};

the client.cpp will then receive it and store it in data and print it out

Current outcome: It is printing random outcome and it did not end the program (infinite). Some stuff copy from the command line , but it wont stop

26710220016661817222889274343557-214234613021914980240162615587848787224662874348677-17396929467224662874344069-204168283435745930074342533275669381911258937235205-10518278365142660544104632-2123472756944701572-531734500513653821913629-431025833424607876961438854961439111-1605430441513807051429161632526724-514957158-1286708961-1722871465961441157961440647-10517823135587861975587910445420122923239035401615725572156135866-921828804-53346303354091785516346447661095676702-529630690162195379954202857416346304291095676702-5296301791134131916325264895177476-53175062851527985158940-514916202558825605-428968316-244381721-1052917621558784836644769668-2041704702-2039585146-244387042-1972796771370310219-227626210-1841446849-244403426-240316597-1972796411370309963-227626210-1841446813-244403426-944959413-244387041-23083408513630013831919857438-1303465186-1536266722-2276098271689063955722665261701735454-46249085116722869991632510750-4814189801664991319558789404-246504676163873306321934878-512852195-508750817540917819-4289364201756172547-164364287-173190433-491957361-18996792912092702721463582721756954624676495360-2143748096148180172812092702759437233-398605863227607747-1588838227121307139359768881-1556233763-1269955807-1049730683-445750395-398606325110167107-1488174931-95114723612151757815976888

I had tried to follow this How to receive a custom data type from socket read? , but it does not help.

>Solution :

you are messing up the buffer size while reading

boost::asio::read(s, boost::asio::buffer(&data, sizeof(data)));

is wrong, should probably be:

boost::asio::read(s, boost::asio::buffer(data));

you are passing the size of the vector object, not the size of the vector storage.

BTW with boost you should not need to set the size as it can read it from the vector itself.

NOTE: your code will still fail because you actually send 9 integer and try to receive 10, so you’ll get a "end of file" exception from read, but this is another story.

Leave a ReplyCancel reply