Re: [protobuf] How to send classes defined in proto over a socket
Wait. On Fri, Mar 12, 2010 at 9:29 AM, mk wrote: > 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? > > 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) { > You can't do that. You have to use one of the Serialize methods. > cerr << "send() failed" ; > exit(1); > } > > //receiving data from the client > if (recv(socket, &data_rcv, sizeof(data_rcv), 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_rcv, sizeof(data_rcv), 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- > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To post to this group, send email to proto...@googlegroups.com. > To unsubscribe from this group, send email to > protobuf+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
Re: [protobuf] How to send classes defined in proto over a socket
You're trying to send the raw C++ memory representation of a protocol object send(socket, &data_snd, sizeof(data_snd), 0) That doesn't work. And is the reason why protocol buffers are there in the first place: they provide serialization techniques. You need to use the serialization methods that SerializeToStream or SerializeToString that generate a platform independent string serialization from the content of the protocol buffer. On the other side deserialize with ParseFromString/ParseFromStream http://code.google.com/apis/protocolbuffers/docs/overview.html -h On Fri, Mar 12, 2010 at 09:29, mk wrote: > 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? > > 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_rcv, sizeof(data_rcv), 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_rcv, sizeof(data_rcv), 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- > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To post to this group, send email to proto...@googlegroups.com. > To unsubscribe from this group, send email to > protobuf+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
Re: [protobuf] How to send classes defined in proto over a socket
On what line do you get the error? On Fri, Mar 12, 2010 at 9:29 AM, mk wrote: > 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? > > 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_rcv, sizeof(data_rcv), 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_rcv, sizeof(data_rcv), 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- > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To post to this group, send email to proto...@googlegroups.com. > To unsubscribe from this group, send email to > protobuf+unsubscr...@googlegroups.com > . > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.