I'm new in protobuf
我是protobuf的新手
When I am sending a protobuf variable through socket communication on recv end I am trying to display the string variable of protobuf I got segmentation in this remaining Data type other than String they are working fine but string variable case I got segmentation How can I over come in Protobuf string datatype segmentation fault other than we have any other data type for store the string data type.
当我在recv端通过套接字通信发送protobuf变量时我试图显示protobuf的字符串变量我在除了String之外的剩余数据类型中得到了分段它们工作正常但是字符串变量情况我得到了分段我怎么能过来在Protobuf字符串数据类型分段故障以外我们有任何其他数据类型用于存储字符串数据类型。
I create a proto_socket.proto with in string variable name is there I am compile proto_socket.proto with protoc compiler (protoc proto_socket.proto --c_out <path>
) it create two files two files proto_socket.pb-c.h
, proto_socket.pb-c.c
. By using these files I create a proto_server.c
and proto_client.c
. And compile it. but at the time of both programms runing I sent a protobuf variable on recv side it give segmentation fault due to trying to display string variable.
我创建了一个带有字符串变量名的proto_socket.proto,我用protoc编译器编译proto_socket.proto(protoc proto_socket.proto --c_out
proto_socket.proto
proto_socket.proto
syntax="proto2";
message messages{
optional int32 age=1;
optional int32 year=2;
optional string name=3;
}
proto_client.c
proto_client.c
void *clientThread(void *arg)
{
Messages t_message;
//while(1)
{
int32_t s32_send_status;
t_message.age =70;
t_message.year=2018;
t_message.name="anu";
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");
}
/*gs32_valread=read( gs32_clientSocket , gars32_buffer , 1024);
if(gs32_valread==0){
printf("connection loss\n");
exit(1);
}
else if(gs32_valread<0){
printf("error:read\n");
exit(1);
}
else{
printf("server replay:%s\n",gars32_buffer);
}*/
}
sleep(2);
return NULL;
}
proto_server.c
proto_server.c
void *socketThread(void *arg)
{
Messages t_message;
//while(1)
{
gs32_valread = read( gs32_new_socket,(unsigned char *)
(&t_message), 1024);
if(gs32_valread==0){
printf("disconnected\n");
exit(1);
}
else if(gs32_valread<0){
printf("error\n");
exit(1);
}
else{
printf("age=%d\n",t_message.age );
printf("year=%d\n",t_message.year);
gs32_birth_year=(t_message.year)-(t_message.age);
printf("birth year=%d\n",gs32_birth_year);
printf("name=%s\n",t_message.name);
}
sleep(1);
close(gs32_new_socket);
}
return NULL;
}
and the result,
结果,
age=70
year=2018
birth year=1948
Segmentation fault (core dumped)
1 个解决方案
#1
-1
Change your string to a pointer like below:-
将您的字符串更改为如下所示的指针: -
char name*;
t_message.name = malloc(50);
strcpy(t_message.name,"anu");
string name
is a object which doesn't not support in this case hence change it to a char array(dynamic).
字符串名称是一个在这种情况下不支持的对象,因此将其更改为char数组(动态)。
#1
-1
Change your string to a pointer like below:-
将您的字符串更改为如下所示的指针: -
char name*;
t_message.name = malloc(50);
strcpy(t_message.name,"anu");
string name
is a object which doesn't not support in this case hence change it to a char array(dynamic).
字符串名称是一个在这种情况下不支持的对象,因此将其更改为char数组(动态)。