Here's my sample code. In this code, everything works for me -however
when I change the GPBClient to use Data IO streams (just comment out
the Object IO references and uncomment the Data IO references in
GPBClient), that's when I get the
"com.google.protobuf.InvalidProtocolBufferException: Protocol message
end-group tag did not match expected tag." error.
I call the 2 threads to use it:
Thread server = new Thread(new GPBServer());
server.start();
Thread client = new Thread(new GPBClient());
client.start();
2 classes -GPBServer and GPBClient:
public class GPBServer implements Runnable{
ServerSocket serverSocket;
Socket socket = null;
ObjectOutputStream objectOutputStream;
ObjectInputStream objectInputStream;
public GPBServer(){}
private void runThis(){
try{
//create server socket
serverSocket = new ServerSocket(2004, 10);
//get connection
System.out.println("server: getting connection");
socket = serverSocket.accept();
System.out.println("server: Connection received from " +
socket.getInetAddress().getHostName());
//create IO streams
objectOutputStream = new
ObjectOutputStream(socket.getOutputStream
());
objectOutputStream.flush();
objectInputStream = new
ObjectInputStream(socket.getInputStream());
//send my GPB object
sendMyGPBObject();
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
//Close connections and socket
try{
objectInputStream.close();
objectOutputStream.close();
serverSocket.close();
System.out.println("server: Closed all
connection and streams.");
}catch(IOException ioException){
ioException.printStackTrace();
}
}
}
public void sendMyGPBObject(){
CarObject.Car car = null;
try{
//used a factory to create my car instance
using GPB builder stuff (did all GPB compilation successfully)
car = CarFactory.instance.createCar();
//print it out to see what its like (I used a wrapper
to get useful
stuff to tostring but
//for sake of brevityhaven't included it here)
System.out.println("server: The original server side
object is \n"
+ car.toString());
car.writeTo(objectOutputStream);
objectOutputStream.flush();
}catch(IOException e){
System.out.println("serverside: Error is " + e);
}
}
@Override
public void run() {
while(true){
runThis();
}
}
}
public class GPBClient implements Runnable{
Socket socket;
ObjectInputStream objectInputStream;
ObjectOutputStream objectOutputStream;
//DataInputStream dataInputStream;
//DataOutputStream dataOutputStream;
public GPBClient(){
}
private void runThis(){
try{
//creating socket to server
socket = new Socket("localhost", 2004);
System.out.println("Connected to localhost in port
2004");
//IO streams
objectInputStream = new
ObjectInputStream(socket.getInputStream());
objectOutputStream = new
ObjectOutputStream(socket.getOutputStream
());
objectOutputStream.flush();
//dataInputStream = new
DataInputStream(socket.getInputStream());
//dataOutputStream = new
DataOutputStream(socket.getOutputStream
());
//dataOutputStream.flush();
//talk with server
try{
CarObject.Car car =
CarObject.Car.parseFrom(objectInputStream);
//CarObject.Car car =
CarObject.Car.parseFrom(dataInputStream);
//again, I used a wrapper in my actual
code to get good stuff in the tostring, left this out for
//brevity
System.out.println("client: the transmitted
object from the server
is... \n" + car.toString());
}catch(ClassCastException classCastException){
System.err.println("object parsed is not a
car.");
}catch(Exception exception){
System.err.println(exception);
}
System.exit(0);
}catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an
unknown
host!");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
//close connections and socket
try{
objectInputStream.close();
objectOutputStream.close();
//dataInputStream.close();
//dataOutputStream.close();
socket.close();
System.out.println("client: Closed all
connection and streams.");
}catch(IOException ioException){
ioException.printStackTrace();
}
}
}
@Override
public void run() {
runThis();
}
}
--
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.