C++在通道上Boost TCP序列化对象,双向通信不起作用
我有一堂课,一堂非常愚蠢的课。此类的对象,我想使用 asio by boost 通过 tcp 发送它们。我的类正确地与 boost 东西建立了友谊并实现了方法序列化...
好吧,我希望客户端连接到服务器,向其发送我的对象,然后服务器发回同一类的另一个对象。
我尝试这样做:
在服务器中:
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 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();
嗯,这不起作用。它以某种方式阻塞。
这很奇怪,因为问题是双向方案,如果我消除 Q、W、X、Y 线,它就可以工作! 你知道如何解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在客户端中调用流上的刷新
You need to call flush on stream in client