Andry
Andry

Reputation: 16855

C++ Boost TCP serialized objects on channel, bidirectional communication does not work

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?

Upvotes: 1

Views: 1010

Answers (1)

micdelt
micdelt

Reputation: 21

You need to call flush on stream in client

oa << data_send;
data_stream.flush();

Upvotes: 2

Related Questions