如何使用asio :: UDP socket.send_to发送std :: vector <unsigned char =“”>

时间:2017-05-09 19:55:24

标签: c++ sockets c++11 udp boost-asio

我正在尝试从我的服务器应用程序同步发送 <span data-name="day"></span> <span data-name="day"></span> <span data-name="disabled"></span> <span data-name="disabled"></span> <span data-name="disabled"></span> <span data-name="disabled"></span> std::vector<unsigned char>到我的服务器应用程序,其代码如下:

boost::asio::ip::udp

这导致挂起&amp; 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

此外,如果我尝试发送如下的std :: string,它也可以。

在传递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); 时呼叫socket.send_to时应该做些什么? std::vector<unsigned char>是否可以使用socket.send_to或使用其他方式发送字符?

PS:我尝试发送的矢量大小为std::vector<unsigned char> &安培; 我在OSX上

1 个答案:

答案 0 :(得分:0)

如果您的buf_data包含至少一个元素,您可以使用以下内容:

\r\n

作为第一个论点。