Hi there,
I recently started working with Protocol Buffers. I used the
"Addressbook" Example to become acquainted with the PBs. (http://
code.google.com/intl/de-DE/apis/protocolbuffers/docs/
javatutorial.html)
The only difference is that I use an OutputStream to write the address
book instance (in the example they used a FileOutputStream).
Everything works fine, I compiled the proto file and imported it to my
Java project and even that compiles without errors, but when my client
code tries to get (parse) the addressbook instance from the server the
following merror message appears:
com.google.protobuf.InvalidProtocolBufferException: Protocol message
end-group tag did not match expected tag.
at com.google.protobuf.InvalidProtocolBufferException.invalidEndTag
(InvalidProtocolBufferException.java:73)
at com.google.protobuf.CodedInputStream.checkLastTagWas
(CodedInputStream.java:105)
at com.google.protobuf.AbstractMessageLite$Builder.mergeFrom
(AbstractMessageLite.java:202)
at com.google.protobuf.AbstractMessage$Builder.mergeFrom
(AbstractMessage.java:664)
at protoc.Example$AddressBook.parseFrom(Example.java:929)
at protoc.Proto.createConnection(Proto.java:33)
at protoc.Proto.main(Proto.java:24)
That's the code:
(Server)
...
Person.Builder person = Person.newBuilder();
person.setName("Peter");
person.setId(5);
AddressBook.Builder addressbook = AddressBook.newBuilder();
addressbook.addPerson(person.build());
addressbook.build().writeTo(client.getOutputStream()); //
client is a Socket object
client.close();
(Client)
...
public static void createConnection() {
server = null;
try {
server = new Socket("192.168.1.30", 4141);
System.out.println("Connected to server");
AddressBook mission2 = AddressBook.parseFrom(server.getInputStream
());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
closeConnection();
}
The proto file ...
package protoc;
option java_package = "protoc";
option java_outer_classname = "Example";
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
message AddressBook {
repeated Person person = 1;
}
Can anyone tell me what is wrong?
I can't find my mistake ... :(
Thank you very much in advance!
Ramon