[英]How to separate message zmq server?
I am trying to send a video through a ZeroMQ server and I successfully got it with a 1 GB
video, but when I try to send a movie with, with a very larger size ( 6.7 GB
) I get the following error:我正在尝试通过 ZeroMQ 服务器发送视频,并成功获得了
1 GB
视频,但是当我尝试发送具有非常大尺寸( 6.7 GB
)的电影时,出现以下错误:
cliente: /usr/local/include/zmq.hpp:1958: zmq::recv_result_t zmq::detail::socket_base::recv(zmq::message_t&, zmq::recv_flags): Assertion `msg.size() == static_cast<size_t>(nbytes)' failed.
make: *** [Makefile:7: cliente] Aborted
make: *** Deleting file 'cliente'
So I tried to break the file on the server and send it separately to the client, but I got the same error.所以我试图打破服务器上的文件并将其单独发送给客户端,但我得到了同样的错误。 Does someone know how to separate the
message_t
to send it separately to the client?有人知道如何将
message_t
分开来分别发送给客户端吗?
This is the server code:这是服务器代码:
#include <fstream>
#include <sstream>
#include <chrono>
#include <thread>
#include <iostream>
#include <zmq.hpp>
using namespace std::chrono_literals;
int main()
{
zmq::context_t context(1);
zmq::socket_t socket(context, zmq::socket_type::rep);
socket.bind("tcp://*:5555");
std::ifstream img("video.mkv", std::ifstream::in | std::ios::binary);
for (;;)
{
zmq::message_t message;
socket.recv(message, zmq::recv_flags::none);
std::ostringstream os;
os << img.rdbuf();
std::string oss(os.str());
socket.send(zmq::buffer(oss.substr(0, (oss.size() / 2))), zmq::send_flags::sndmore);
std::this_thread::sleep_for(1s);
socket.send(zmq::buffer(oss.substr((oss.size() / 2), oss.size() - 1)), zmq::send_flags::none);
std::this_thread::sleep_for(1s);
}
}
This is my client:这是我的客户:
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <zmq.hpp>
int main()
{
std::ofstream img("teste.mkv", std::ios::out | std::ios::binary);
zmq::context_t context(2);
zmq::socket_t socket(context, zmq::socket_type::req);
socket.connect("tcp://localhost:5555");
zmq::message_t message_1, message_2;
socket.send(zmq::buffer("ok\n"), zmq::send_flags::none);
socket.recv(message_1, zmq::recv_flags::none);
std::string data_1(message_1.to_string());
img << data_1;
data_1.clear();
socket.recv(message_2, zmq::recv_flags::none);
std::string data_2(message_2.to_string());
img << data_2;
data_2.clear();
}
Q : "How to separate message zmq server?"
问: “如何分离消息zmq服务器?”
Well, avoid attempts to move 6.7 GB
at once.好吧,避免尝试一次移动
6.7 GB
。 Doing a sndmore
-trick to send that huge volume of data as a multi-part structured message does not save the problem, as such a structured message will (almost) never fit into RAM-constraints.使用
sndmore
技巧将大量数据作为多部分结构化消息发送并不能解决问题,因为这样的结构化消息(几乎)永远不会适合 RAM 约束。
Best setup an indexing data-reader, move the huge data-file in reasonably sized solo-blocks ( avoid using a multi-part kind of messages ) and make the receiver keep track of what index-numbers have been received so far ( confirming POSACK'd receptions to the sender + re-requesting missing index-blocks to get ev. resent ).最好设置一个索引数据读取器,在合理大小的单独块中移动巨大的数据文件(避免使用多部分类型的消息)并使接收器跟踪到目前为止收到的索引号(确认 POSACK 'd 接收到发送者 + 重新请求丢失的索引块以获得 ev. resent )。
ZeroMQ does not provide warranty of delivering any message, yet it makes you sure, that if you have .recv()
-ed a payload, it is a binary-identical replica of the .send()
-dispatched data. ZeroMQ 不提供传递任何消息的保证,但它可以让您确定,如果您有
.recv()
-ed 有效载荷,它是.send()
-dispatched 数据的二进制相同副本。 So, prepend an index-number and send payloads as described & you have a reasonably robust BLOB-parse/send/re-assemble with a few possible re-send requests.因此,预先添加一个索引号并按照描述发送有效负载,并且您有一个相当健壮的 BLOB 解析/发送/重新组装,并带有一些可能的重新发送请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.