I encountered a similar problem with reading the bytes in C++ from data written in Java. Solved it by swapping the bytes : uint32_t magic_number = ntohl(magic_number_);
http://github.com/h4ck3rm1k3/OSM-Osmosis/commit/714be97af12698f83152d2c1d0e407337e82803b mike On Sat, Jul 10, 2010 at 1:47 PM, Maxim Leonovich <[email protected]> wrote: > Few days I'm trying to send a message from C++ to Java over the > network but unsuccessfully. > I needed to send a several messages over one connection. I'm doing > this: > > //C++ side > > .... > void MainWindow::client_connected() { > GPSChatMessage msg = GPSChatMessage(); > msg.set_type(proto::GPSChatMessage::AUTHORIZATION_REQUEST); > msg.mutable_auth_req_msg()->set_login("bsod"); > msg.mutable_auth_req_msg()->set_password("*****"); > client->send(msg); > } > .... > .... > void Client::send(const GPSChatMessage & msg) { > QDataStream to(socket); > char * buffer = new char[msg.ByteSize() + 4]; > ZeroCopyOutputStream * os = new > ArrayOutputStream(buffer,msg.ByteSize() + 4,sizeof(char)); > CodedOutputStream * cos = new CodedOutputStream(os); > cos->WriteLittleEndian32(msg.ByteSize()); //Tryed > "WriteVariant32", didn't help > msg.SerializeToCodedStream(cos); > delete cos; > delete os; > to.writeBytes(buffer,msg.ByteSize() + 4); > delete buffer; > } > > //Java side: > //(I have a thread, that represents user connection. "from" - is an > InputStream from socket. > > public void run() { > while (!interrupted()) { > try { > GPSChatMessage msg = GPSChatMessage.parseDelimitedFrom(from); > handler.handle(msg); > } catch (IOException ex) { > > Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, > null, ex); > } > > > When I sending a message from C++, I'm getting 4 exceptions in Java: > > 10.07.2010 14:30:42 net.ClientThread run > SEVERE: null > com.google.protobuf.InvalidProtocolBufferException: Message missing > required fields: type > at > com.google.protobuf.UninitializedMessageException.asInvalidProtocolBufferException(Unknown > Source) > at proto.GPSChatProtocol$GPSChatMessage > $Builder.buildParsed(GPSChatProtocol.java:299) > at proto.GPSChatProtocol$GPSChatMessage$Builder.access > $200(GPSChatProtocol.java:247) > at proto.GPSChatProtocol > $GPSChatMessage.parseDelimitedFrom(GPSChatProtocol.java:218) > at net.ClientThread.run(ClientThread.java:54) > 10.07.2010 14:30:43 net.ClientThread run > SEVERE: null > com.google.protobuf.InvalidProtocolBufferException: Message missing > required fields: type > at > com.google.protobuf.UninitializedMessageException.asInvalidProtocolBufferException(Unknown > Source) > at proto.GPSChatProtocol$GPSChatMessage > $Builder.buildParsed(GPSChatProtocol.java:299) > at proto.GPSChatProtocol$GPSChatMessage$Builder.access > $200(GPSChatProtocol.java:247) > at proto.GPSChatProtocol > $GPSChatMessage.parseDelimitedFrom(GPSChatProtocol.java:218) > at net.ClientThread.run(ClientThread.java:54) > 10.07.2010 14:30:43 net.ClientThread run > SEVERE: null > com.google.protobuf.InvalidProtocolBufferException: Message missing > required fields: type > at > com.google.protobuf.UninitializedMessageException.asInvalidProtocolBufferException(Unknown > Source) > at proto.GPSChatProtocol$GPSChatMessage > $Builder.buildParsed(GPSChatProtocol.java:299) > at proto.GPSChatProtocol$GPSChatMessage$Builder.access > $200(GPSChatProtocol.java:247) > at proto.GPSChatProtocol > $GPSChatMessage.parseDelimitedFrom(GPSChatProtocol.java:218) > at net.ClientThread.run(ClientThread.java:54) > 10.07.2010 14:30:43 net.ClientThread run > SEVERE: null > com.google.protobuf.InvalidProtocolBufferException: Protocol message > end-group tag did not match expected tag. > at > com.google.protobuf.InvalidProtocolBufferException.invalidEndTag(Unknown > Source) > at > com.google.protobuf.CodedInputStream.checkLastTagWas(Unknown Source) > at com.google.protobuf.AbstractMessageLite > $Builder.mergeFrom(Unknown Source) > at com.google.protobuf.AbstractMessage > $Builder.mergeFrom(Unknown Source) > at com.google.protobuf.AbstractMessage > $Builder.mergeFrom(Unknown Source) > at com.google.protobuf.AbstractMessageLite > $Builder.mergeDelimitedFrom(Unknown Source) > at com.google.protobuf.AbstractMessage > $Builder.mergeDelimitedFrom(Unknown Source) > at proto.GPSChatProtocol > $GPSChatMessage.parseDelimitedFrom(GPSChatProtocol.java:218) > at net.ClientThread.run(ClientThread.java:54) > > I'm googling a lot, but I really don't know what to do. What I'm doing > wrong? > > -- > You received this message because you are subscribed to the Google Groups > "Protocol Buffers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/protobuf?hl=en. > > -- James Michael DuPont Member of Free Libre Open Source Software Kosova and Albania flossk.org flossal.org -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
