如何在 C++ 中拆分和连接数组对于UDP?
我有一个像这样的字节数组:
lzo_bytep out; // my byte array
size_t uncompressedImageSize = 921600;
out = (lzo_bytep) malloc((uncompressedImageSize +
uncompressedImageSize / 16 + 64 + 3));
wrkmem = (lzo_voidp) malloc(LZO1X_1_MEM_COMPRESS);
// Now the byte array has 802270 bytes
r = lzo1x_1_compress(imageData, uncompressedImageSize,
out, &out_len, wrkmem);
如何将其分割成 65,535 字节以下的较小部分(字节数组是一个大图像,我想通过 UDP 发送,其上限为 65,535 字节),然后将这些小块重新连接成一个连续数组?
I have a byte array like this:
lzo_bytep out; // my byte array
size_t uncompressedImageSize = 921600;
out = (lzo_bytep) malloc((uncompressedImageSize +
uncompressedImageSize / 16 + 64 + 3));
wrkmem = (lzo_voidp) malloc(LZO1X_1_MEM_COMPRESS);
// Now the byte array has 802270 bytes
r = lzo1x_1_compress(imageData, uncompressedImageSize,
out, &out_len, wrkmem);
How can I split it into smaller parts under 65,535 bytes (the byte array is one large image which I want to sent over UDP which has upper limit 65,535 bytes) and then join those small chunks back into a continuous array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这样做的问题是 UDP 数据包可能会到达、排序或被丢弃。为此使用 TCP;这就是它的用途。
The problem with doing this is that the UDP packets can arrive out or order, or be dropped. Use TCP for this; that's what it's for.
您不必“拆分”数组。你只需要指出它的不同部分。
假设您使用典型的 UDP write() 函数,它需要几个参数。其中之一是指向缓冲区的指针,另一个是长度。
如果您想获取前 65535 个字节,则缓冲区位于
wrkmem
,长度为 65535。对于第二个 65535 字节,缓冲区位于
wrkmem + 65535
, 长度为 65535。长度是 65535。第三个 65535 字节,您的缓冲区位于
wrkmem + 2 * 65535
,您的长度是 65535。明白了吗?
(也就是说,其他海报是正确的。您应该使用 TCP)。
另一方面,当您想要重新加入数组时,必须为整个数组分配足够的内存,然后使用像 memcpy() 这样的复制函数将到达的块复制到正确的位置。请记住,UDP 可能不会按顺序传送数据块,也可能不会传送所有数据块。
You don't have to "split" the array. You just have to point into different parts of it.
Assuming you're using a typical UDP write() function, it takes several arguments. One of them is a pointer to the buffer and the other is the length.
If you want to get the first 65535 bytes, your buffer is at
wrkmem
and the length is 65535.For the second 65535 bytes, your buffer is at
wrkmem + 65535
and your length is 65535.The third 65535 bytes, your buffer is at
wrkmem + 2 * 65535
and your length is 65535.Get it?
(That said, the other posters are correct. You should be using TCP).
On the other side, when you want to re-join the array, you must allocate enough memory for the whole thing, then use a copy function like memcpy() to copy the arriving chunks into their correct position. Remember that UDP may not deliver the pieces in order and may not deliver all of them.
您可能希望尝试基于消息的中间件,例如 ØMQ 并将整个压缩图像作为一条消息提供并使用中间件异步运行并以尽可能最快的速度管理重新交付。它提供了 BSD 套接字兼容的 API,因此可以轻松迁移代码,并允许您根据需要轻松地在各种底层传输协议之间进行交换。
还可以使用其他消息系统。
You might wish to try a message based middleware like ØMQ and feed the entire compressed image as one message and have the middleware run asynchronously and manage redelivery at the fastest speed possible. It provides a BSD socket compatible API and so can be easy to migrate code over and allows you to easily swap between various underlying transport protocols as required.
Other message systems are available.