使用boost buffer序列化并使用UDP协议发送
Using boost buffer to serialize and send using UDP protocol
我想序列化并通过 UDP 协议发送一些数据。代码如下所示:
typedef struct{
uint8_t a[2];
uint8_t b;
}example;
std::vector<example> ex_vector; //Filling part of this vector is notgiven here
void sendUDP(){
int sockfd;
sockfd = socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in serv;
serv.sin_family = AF_INET;
serv.sin_port = htons(5005);
serv.sin_addr.s_addr = inet_addr("127.0.0.1");
//const uint8_t buffer[2] = {0x12, 0x13}; -> Works well with this
auto mutable_buffer = boost::asio::buffer(ex_vector);
socklen_t m = sizeof(serv);
sendto(sockfd,mutable_buffer,sizeof(mutable_buffer),0,(struct sockaddr *)&serv,m);
}
如何使用 boost 序列化 POD 元素 (example
) 的向量? UDP 代码取自 (http://www.cplusplus.com/forum/unices/76180/).
当前弹出错误:
no matching function for call to 'sendto'
sendto(sockfd,mutable_buffer,sizeof(mutable_buffer),0,(struct sockaddr *)&serv,m);
来自 boost::asio::buffer
的文档:
The contents of a buffer may be accessed using the data() and size() member functions:
boost::asio::mutable_buffer b1 = ...;
std::size_t s1 = b1.size();
unsigned char* p1 = static_cast<unsigned char*>(b1.data());
(不过,我想知道你为什么混合使用 boost 和 posix,boost 有 boost::asio::ip::udp::socket
应该可以完美地与缓冲区一起工作,并且互联网上应该可以找到示例)
我想序列化并通过 UDP 协议发送一些数据。代码如下所示:
typedef struct{
uint8_t a[2];
uint8_t b;
}example;
std::vector<example> ex_vector; //Filling part of this vector is notgiven here
void sendUDP(){
int sockfd;
sockfd = socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in serv;
serv.sin_family = AF_INET;
serv.sin_port = htons(5005);
serv.sin_addr.s_addr = inet_addr("127.0.0.1");
//const uint8_t buffer[2] = {0x12, 0x13}; -> Works well with this
auto mutable_buffer = boost::asio::buffer(ex_vector);
socklen_t m = sizeof(serv);
sendto(sockfd,mutable_buffer,sizeof(mutable_buffer),0,(struct sockaddr *)&serv,m);
}
如何使用 boost 序列化 POD 元素 (example
) 的向量? UDP 代码取自 (http://www.cplusplus.com/forum/unices/76180/).
当前弹出错误:
no matching function for call to 'sendto'
sendto(sockfd,mutable_buffer,sizeof(mutable_buffer),0,(struct sockaddr *)&serv,m);
来自 boost::asio::buffer
的文档:
The contents of a buffer may be accessed using the data() and size() member functions:
boost::asio::mutable_buffer b1 = ...; std::size_t s1 = b1.size(); unsigned char* p1 = static_cast<unsigned char*>(b1.data());
(不过,我想知道你为什么混合使用 boost 和 posix,boost 有 boost::asio::ip::udp::socket
应该可以完美地与缓冲区一起工作,并且互联网上应该可以找到示例)