numpydtypebuffersizemustbeamultipleofelementsize错误
使用C++启动zmq服务器,python端作为客户端接收数据,遇到了buffer size must be a multiple of element size错误。
其中,C++的结构体如下:struct A { int i; int c; int d; char x; char xxxx[10]; int xx[3]; }; struct A a = {1, 2, 3, "d", "xyz", {1, 2, 3}}; // 查看结构体对应的字节数 cout << sizeof(A) << endl;
python端接收数据时如下:import numpy as np dtype = np.dtype( [ ("a", " #include #include #include #include #include #include #include using namespace std; int s_send(void *socket) { zmq_msg_t msg; zmq_msg_init_size(&msg, 20); memcpy(zmq_msg_data(&msg), "AA Hello World", 20); zmq_sendmsg(socket, &msg, ZMQ_SNDMORE); zmq_msg_close(&msg); struct A { int i; int c; int d; char x; char xxxx[10]; int xx[3]; }; struct A a = {1, 2, 3, "d", "xyz", {1, 2, 3}}; zmq_msg_t msg2; zmq_msg_init_size(&msg2, sizeof(A)); memcpy(zmq_msg_data(&msg2), &a, sizeof(A)); zmq_sendmsg(socket, &msg2, 0); zmq_msg_close(&msg2); } int main() { // 初始化上下文 void *context = zmq_ctx_new(); // 创建、绑定套接字 void *publisher = zmq_socket(context, ZMQ_PUB); // 绑定到tcp节点 int rc = zmq_bind(publisher, "tcp://*:5555"); assert(rc == 0); std::cout << "start send data" << std::endl; char const *string = "hello world"; while(1) { s_send(publisher); sleep(1); } zmq_close(publisher); zmq_ctx_destroy(context); }
python端:import numpy as np import zmq context = zmq.Context() sub = context.socket(zmq.SUB) sub.connect("tcp://localhost:5555") sub.setsockopt(zmq.SUBSCRIBE, b"AA") dtype = np.dtype( [ ("a", "