how to send classes defined in .proto (protocol-buffers) over a socket

Posted by make on Stack Overflow See other posts from Stack Overflow or by make
Published on 2010-03-12T17:29:11Z Indexed on 2010/03/12 17:47 UTC
Read the original article Hit count: 575

Filed under:
|
|
|
|

Hi,

I am trying to send a proto over a socket, but i am getting segmentation error. Could someone please help and tell me what is wrong with this example?

file.proto

message data{  
    required string x1 = 1;
    required uint32 x2 = 2;
    required float x3 = 3;
}

client.cpp

...
    // class defined in proto
    data data_snd; 
    data data_rec;

    char *y1 = "operation1";
    uint32_t y2 = 123 ;
    float y3 = 3.14;

    // assigning data to send()
    data_snd.set_x1(y1);
    data_snd.set_x2(y2);   
    data_snd.set_x3(y3);

    //sending data to the server
    if (send(socket, &data_snd, sizeof(data_snd), 0) < 0) {
       cerr << "send() failed" ;
       exit(1);
     }  

     //receiving data from the client
     if (recv(socket, &data_rec, sizeof(data_rec), 0) < 0) {
        cerr << "recv() failed";
        exit(1);
     }  

     //printing received data
     cout << data_rec.x1() << "\n";
     cout << data_rec.x2() << "\n";
     cout << data_rec.x3() << "\n";
...

server.cpp

 ...

     //receiving data from the client
     if (recv(socket, &data_rec, sizeof(data_rec), 0) < 0) {
        cerr << "recv() failed";
        exit(1);
     }  

     //printing received data
     cout << data_rec.x1() << "\n";
     cout << data_rec.x2() << "\n";
     cout << data_rec.x3() << "\n"; 

   // assigning data to send()
    data_snd.set_x1(data_rec.x1());
    data_snd.set_x2(data_rec.x2());   
    data_snd.set_x3(data_rec.x3());

    //sending data to the server
    if (send(socket, &data_snd, sizeof(data_snd), 0) < 0) {
       cerr << "send() failed" ;
       exit(1);
     }       
...

Thanks for help and replies-

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++