Can you provide a wrapper OutputStream in your Java code so you can
intercept the exact bytes that are written to the stream? It wasn't clear if
in your last attempt you had removed the ObjectOutputStream issue or not. I
would suggest first making sure that the bytes you receive on the C++ side
are the same as what is getting sent to the socket in Java; if you also know
the size that you are writing you can verify that the bytes written to the
stream matches the varint-encoded value.

On Thu, Oct 6, 2011 at 1:00 AM, yorick <yorick.bru...@gmail.com> wrote:

> After reading
> http://code.google.com/apis/protocolbuffers/docs/techniques.html#streaming
> that we suggested to me, I added a CodedInputStream in the C++ part.
>
> I have now for the Java (this is unchanged) :
> ----------------------------------
> IRobotData data = getRobotData();
> System.out.println("phi:"+data.getPhi()+",
> distance:"+data.getDistance());
> int serDataSize = data.getSerializedSize();
> System.out.println("serDataSize:"+serDataSize);
> cos.writeRawVarint32(serDataSize);
> data.writeTo(cos);
> cos.flush();
> oos.flush();
> ----------------------------------
> and for the C++ :
> ----------------------------------
> // Get size
> recvSize = recv(csock, buf, sizeof(buf), 0);
> if(recvSize < 0)
> cerr << "recv() failed" << endl;
> cout << "recv size:" << recvSize << endl;
> google::protobuf::io::CodedInputStream* cis = new
> google::protobuf::io::CodedInputStream(buf, recvSize);
> buf[recvSize] = '\0';
>
> unsigned int size;
> cis->ReadVarint32(&size);
> cout << "size:" << size << endl;
> delete cis;
>
> // Get data
> recvSize = recv(csock, buf, sizeof(buf), 0);
> if(recvSize < 0)
> cerr << "recv() failed" << endl;
> buf[recvSize] = '\0';
> cout << "recv size:" << recvSize << endl;
> cis = new google::protobuf::io::CodedInputStream(buf, recvSize);
> cis->ReadString(&strbuf, recvSize);
> cout << "string size:" << strbuf.size() << endl;
> delete cis;
> data.ParseFromString(strbuf);
> if(data.IsInitialized()) {
> cout << "is initialized" << endl;
> } else {
> cout << "is NOT initialized" << endl;
> }
>
> if(data.has_phi()) {
> cout << "has phi" << endl;
> } else {
> cout << "has not phi" << endl;
> }
>
> if(data.has_distance()) {
> cout << "has distance" << endl;
> } else {
> cout << "has not distance" << endl;
> }
> cout << "phi:" << data.phi() << ", distance:" << data.distance() <<
> endl;
> ----------------------------------
> The second "cis = new google::protobuf::io::CodedInputStream(buf,
> recvSize);" is not totally correct as I should use the size in
> variable "size", but as the "size" variable is not correct I kept
> "recvSize" for now.
>
> The output is the following for Java :
> ----------------------------------
> Start !
> phi:0.5, distance:1.6
> serDataSize:18
> Quit !
> ----------------------------------
> and for C++ :
> ----------------------------------
> Start !
> recv size:4
> size:13996
> recv size:21
> string size:21
> is NOT initialized
> has not phi
> has not distance
> phi:0, distance:0
> Quit !
> ----------------------------------
> So the size value is not the same... I'm suspecting that the Java
> "CodedOutputStream::writeRawVarint32" and the C++
> "CodedInputStream::ReadVarint32" do not encode int in the same way...
>
> What should I do to get the same int value on both sides ?
>
> Thanks.
>
>
>
> On 6 oct, 09:30, Christopher Head <hea...@gmail.com> wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: RIPEMD160
> >
> > On Wed, 5 Oct 2011 02:48:39 -0700 (PDT)
> >
> > yorick <yorick.bru...@gmail.com> wrote:
> >
> > [snip]
> >
> > > ----------------------------------------------------------------
> > > Java :
> > > ----------------------------------------------------------------
> > > Socket sock;
> > >            try {
> > >                    sock = new Socket("localhost", 10101);
> > >                    ObjectOutputStream oos = new
> > > ObjectOutputStream(sock.getOutputStream());
> > >                    CodedOutputStream cos =
> > > CodedOutputStream.newInstance(oos);
> >
> > Why are you using an ObjectOutputStream? You're not using Java
> > serialization for anything, so don't do that. It's probably adding
> > headers itself which your C++ code doesn't expect. For that matter,
> > there doesn't seem to be much reason to even create a CodedOutputStream
> > in this case, since AbstractMessageLite.writeTo() accepts an arbitrary
> > java.io.OutputStream, such as the one returned from
> > sock.getOutputStream().
> >
> >
> >
> > >                    //for(int i=0 ; i<10 ; i++){
> > >                            IRobotData data = getRobotData();
> >
> > >                            System.out.println("phi:"+data.getPhi()+",
> > > distance:"+data.getDistance());
> > >
>  cos.writeRawVarint32(data.getSerializedSize());
> > >                            data.writeTo(cos);
> > >                            cos.flush();
> > >                            oos.flush();
> >
> > Here, you're writing a Varint32 to the stream before writing the data
> > object itself, but I don't see anything in your C++ code that reads the
> > varint before reading the data object.
> >
> > Chris
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v2.0.17 (GNU/Linux)
> >
> > iEYEAREDAAYFAk6NWSoACgkQXUF6hOTGP7fW+ACffCrOhAiZ42hmX98OSbdYwIQK
> > 1q4AniBG7I9nGOoJRnBMyop4aZ9b32AR
> > =lmqD
> > -----END PGP SIGNATURE-----
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to protobuf@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to