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.
I create a example.proto with in string variable name is there
I am compile example.proto with protoc compiler (protoc example.proto --cpp_out <path>
)
it create two files two files example.pb.h, example.pb.cc
By using these files I create a test_server.cpp and test_client.cpp
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.
How can I solve this problem?
example.proto
package data;
message star
{ optional string name=1; }
server.cpp
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<strings.h>
#include<string.h>
#include <arpa/inet.h>
#include"example.pb.h"
#include"example.pb.cc"
int main()
{
int sd,csd;
sd=socket(AF_INET, SOCK_STREAM,0);
perror("socket");
sockaddr_in ser,cli;
ser.sin_family=AF_INET;
ser.sin_port=htons(7878);
ser.sin_addr.s_addr=inet_addr("X.Y.Z.A");
bzero(ser.sin_zero, 8);
size_t s=16;
if(bind(sd,(struct sockaddr *)&ser, s)==-1)
cout<<"Bind FAIL\n";
else
cout<<"Bind Success\n";
if((csd=accept(sd,(struct sockaddr *)&cli, &s))==-1)
cout<<"Connection Accept FAIL\n";
else
cout<<"ConnectioN Accept Success\n";
star pkt;
recv(csd,&pkt,sizeof(pkt),0);
cout<<"\t String Name: "<<pkt.name<<endl; //Here only i get segmentation
close(sd);
close(csd);
}
client.cpp
#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<strings.h>
#include<string.h>
#include <arpa/inet.h>
#include"example.pb.h"
#include"example.pb.cc"
int main()
{
int sd;
sd=socket(AF_INET, SOCK_STREAM,0);
perror("socket");
sockaddr_in ser;
ser.sin_family=AF_INET;
ser.sin_port=htons(7878);
ser.sin_addr.s_addr=inet_addr("X.Y.Z.A");
bzero(ser.sin_zero, 8);
if(connect(sd,(struct sockaddr *)&ser, 16)==-1)
cout<<"connect FAIL\n";
else
cout<<"connect Success\n";
star pkt;
pkt.set_name("Pratap");
cout<<"Send Data without help of another variable....\n";
send(sd,&pkt,sizeof(pkt) ,MSG_CONFIRM);
close(sd);
}