Paul
unread,Oct 13, 2010, 3:13:14 PM10/13/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Protocol Buffers
Hi,
I am new to protocol buffers, and I am trying to send a protocol
buffers message over TCP from a client computer running C++ to a
server computer running Java. Both computers are 64 bit.
On the server side (in Java), I open a ServerSocket on a port, and get
a Socket after accepting the client's connection. From the client
Socket (liveSock), I get an inputstream, which I pass to the handle
function, which calls parseDelimitedFrom on the inputstream.
On the client side (in C++), I open a TCP socket connection on the
same port with the server's IP address. I serialize the message using
SerializeToCodedStream into an array using ArrayOutputStream. After
serializing it, I send it over the TCP connection using my sendTCP
method which uses C++ sockets.
When I start running the two sides, the connection gets established.
However, when the client tries to send the data over TCP, the server
handle code crashes at parseDelimitedFrom with an
InvalideProtocolBufferException saying that the Message is missing
required fields.
I'm sure that my C++ TCP sockets work correctly, because I was able to
send text from C++ to Java without any problems. If it helps, I can
also post the TCP code I have.
I am not sure what I am doing wrong. I don't know if I am doing
things correctly on the server (Java) side. Please let me know if you
see any problems! Thanks a lot!
Paul
SERVER SIDE CODE:
ServerSocket sock = new ServerSocket(7003);
Socket liveSock = sock.accept();
handle(liveSock.getInputStream(), liveSock.getOutputStream()); //
handle defined below
HANDLE METHOD:
*** the protocol buffers message is called Snapshot ***
public static void handle(InputStream in, OutputStream output) {
try {
Snapshot snapshot = Snapshot.parseDelimitedFrom(in);
while(snapshot != null) {
System.out.println("SNAPSHOT: " + snapshot.getId()
snapshot = Snapshot.parseDelimitedFrom(in);
}
}
catch (IOException e) {
System.out.println("exception");
}
}
CLIENT SIDE CODE:
*** the protocol buffers message is called snap1 ***
int sock = openSocketClientTCP("127.100.100.100", 7003);
char snap_buf2[snap1.ByteSize()+1];
ZeroCopyOutputStream* raw_output = new
ArrayOutputStream(snap_buf2,snap1.ByteSize()+1);
CodedOutputStream* coded_output = new CodedOutputStream(raw_output);
coded_output->WriteVarint64(snap1.ByteSize());
snap1.SerializeToCodedStream(coded_output);
delete coded_output;
delete raw_output;
sendTCP(sock, snap_buf2, snap1.ByteSize() + 1);