如何发送std :: vector< unsigned char>使用asio :: UDP socket.send_to [英] How to send an std::vector<unsigned char> using asio::UDP socket.send_to
问题描述
我正在尝试通过 boost :: asio 同步发送一个
从我的服务器应用到客户端应用,其代码如下: std :: vector< unsigned char>
: :ip :: udp
I am trying to synchronously send an std::vector<unsigned char>
through boost::asio::ip::udp
from my server app to client app with following code like this:
boost::system::error_code ignored_error;
std::vector<unsigned char> buf_data;
std::size_t len = socket.send_to(boost::asio::buffer(buf_data), remote_endpoint, 0, ignored_error);
这会导致挂起& socket.send_to
永不返回。
This causes a hang & socket.send_to
never returns.
但是,如果我尝试发送如下所示的结构,则效果很好
struct MyStruct
{
char name[1024];
int age;
};
boost::system::error_code ignored_error;
MyStruct send_data = {"something", 123 };
std::size_t len = socket.send_to(boost::asio::buffer(&send_data, sizeof(send_data)), remote_endpoint, 0, ignored_error);
此外,如果我尝试发送如下所示的std :: string,
调用 socket.send_to
应该做些什么?传递 std :: vector< unsigned char>
时? socket.send_to
是否与 std :: vector< unsigned char>
一起使用,还是应该使用其他方式发送字符?
What is different that I should do with call to socket.send_to
when passing an std::vector<unsigned char>
? Does socket.send_to
work with std::vector<unsigned char>
or should use some other way to send the chars ?
PS:我要发送的向量的大小为 buf_data.size()是140050
&
我在OSX上
PS: The size of vector I am trying to send is buf_data.size() is 140050
&
I am on OSX
推荐答案
如果buf_data包含至少一个元素,则可以使用以下内容:
If your buf_data contains at least one element, you can use the following:
boost::asio::buffer(&buf_data[0], buf_date.size())
作为第一个参数。
这篇关于如何发送std :: vector< unsigned char>使用asio :: UDP socket.send_to的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!