1
votes

I have a class, a very stupid class. Objects of this class, I want to send them via tcp using asio by boost. My class correctly makes friendship with boost stuff and implements method serialize...

Well I want that a client connects to a server, sends it my object and then the server sends back another object of the same class.

I tried to do this:

In server:

Data data;
int port = 2040;
boost::asio::io_service io_s;
tcp::acceptor data_acceptor(io_s, tcp::endpoint(tcp::v4(), port));
tcp::iostream data_stream;
Data data_recv;
data_acceptor.accept(*(data_stream.rdbuf())); /* Accepting */
boost::archive::binary_iarchive ia(data_stream);
ia >> data_recv; 
boost::archive::binary_oarchive oa(data_stream); /* LINE Y */
oa << data; /* LINE X */
data_stream.close();

Data is the serializable class.

In client:

Data data_send;
Data data_recv;
tcp::iostream data_stream("127.0.0.1", "2040"); /* Creating TCP stream */
boost::archive::binary_oarchive oa(data_stream);
oa << data_send;
boost::archive::binary_iarchive ia(data_stream); /* LINE Q */
ia >> data_recv; /* Receive LINE W */
data_stream.close();

Well, it does not work. It blocks somehow.

It's curios because the problem is this bidirectional scheme, If I eliminate line Q, W, X, Y IT WORKS!!! Do you know how to solve this?

1
Where is data defined in the server? I only see data_recv there. I'm referring to line X. - chrisaycock
Yeah sorry, I mistake of mine in copying... I posted the correction... - Andry
Do you know if the client is blocking on line Q or W? Similar for server. - chrisaycock
Well it seems it blocks on LINE Q, the same for server - Andry

1 Answers

2
votes

You need to call flush on stream in client

oa << data_send;
data_stream.flush();