我在 protobuf-c 中是全新的
我正在尝试创建一个套接字连接并使用 c 语言通过套接字发送 protobuf 打包消息
我将消息打包在客户端代码中并将其发送到服务器。但是服务器未能接收到打包数据并将其解包
我的客户端代码是
客户端.c
void *clientThread(void *arg)
{
Messages t_message = MESSAGES__INIT;
//while(1)
{
void *buf;
unsigned len;
int32_t s32_send_status;
t_message.name="anu";
len=messages__get_packed_size(&t_message);
buf=malloc(len);
messages__pack(&t_message,buf);
s32_send_status=send(gs32_clientSocket ,buf,len,0);
//s32_send_status=send(gs32_clientSocket ,(unsigned char *)
//(&t_message),sizeof(t_message) ,0);
if(s32_send_status>0){
printf("data send to server.\n");
}
else{
printf("failed to send data to server\n");
我的服务器代码是
服务器.c
void *socketThread(void *arg)
{
Messages *t_message;
uint8_t buf[1024];
int32_t len;
// unsigned length;
//while(1)
{
gs32_valread = read( gs32_new_socket,buf,len);
if(gs32_valread==0){
printf("disconnected\n");
exit(1);
}
else if(gs32_valread<0){
printf("error\n");
exit(1);
}
else{
t_message = messages__unpack(NULL,len,buf);
printf("name=%s\n",t_message->name);
messages__free_unpacked(t_message,NULL);
}
sleep(1);
close(gs32_new_socket);
}
return NULL;
}
如果我的包装是正确的,我该如何在服务器端打开它。如果有人知道这个答案,请帮我解决它。谢谢