C++通过套接字连接发送矢量

C++ sending a vector across a socket connection

本文关键字:接发 连接 套接字 C++      更新时间:2023-10-16

我正在尝试通过套接字发送向量,但遇到一系列错误,我不确定如何解决。

矢量和通过套接字发送如下所示:

int code = 52;
std::vector<uint8_t> data1 = { 4, 1, 0, 0, 0, 0, static_cast<uint8_t>(224 + code / 16), static_cast<uint8_t>(code % 16) };
comm_send1 = send(sock, data1.data(), data1.length(), 0);

错误是:

Error (active)  E0167   argument of type "uint8_t *" is incompatible with parameter of type "const char *"  
Error (active)  E0135   class "std::vector<uint8_t, std::allocator<uint8_t>>" has no member "length"    
Error   C2039   'length': is not a member of 'std::vector<uint8_t,std::allocator<_Ty>>' 
Error   C2660   'send': function does not take 3 arguments  

谁能帮忙?

谢谢

send 函数除了 uint8_t* 之外,所以你应该像这样强制转换向量的内容:(const char*(data1.data((; 此外,向量类没有长度((方法,您应该使用size((