I have written this protobuf message in c#
-
C# client:
public AddressBook InitializeAdressBook() { Person newContact = new Person(); AddressBook addressBookBuilder = new AddressBook(); Person john = new Person(); john.id=1234; john.name="John Doe"; john.email="jdoe@example.com"; Person.PhoneNumber nr = new Person.PhoneNumber(); nr.number="5554321"; john.phone.Add(nr); addressBookBuilder.person.Add(john); TextBox.Text += ("Client: Initialisiert? " + addressBookBuilder.ToString()) + "\t" + "\n"; TextBox.Text += " Erster Person " + addressBookBuilder.person.First().name + "\t" + "\n"; return addressBookBuilder; }
Problem
I am trying to send a protobuf message from a c# client to this java server…
-
Java server
public ControllerThread(Socket s){ this.s = s; try { AddressBook adb = AddressBook.parseFrom(s.getInputStream()); System.out.println("Server: Addressbook:" + adb.getPersonCount()); } catch (IOException e) { System.out.println("Server: BufferedReader oder PrintWriter von ThermoClient konnte nicht erstellt werden"); e.printStackTrace(); } }}
Question:
I should serialize this message to a byte array, so that i can send it to the java server…
Unfortunately the method ProtoBuf.Serializer.Serialize dont return a byte array back.
So how can i serialize it as a byte array and send it to my Java Server? Any help appreciated thanks!
protobuf-net (aka
ProtoBuf.Serializer.Serialize) writes to streams. If you have the socket available as aNetworkStream, you can just write directly to that. If you really want abyte[], then useMemoryStream: