C++在通道上Boost TCP序列化对象,双向通信不起作用

发布于 2024-10-06 05:12:02 字数 1066 浏览 9 评论 0原文

我有一堂课,一堂非常愚蠢的课。此类的对象,我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2024-10-13 05:12:02

您需要在客户端中调用流上的刷新

oa << data_send;
data_stream.flush();

You need to call flush on stream in client

oa << data_send;
data_stream.flush();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文