Hello,
I installed protobuf 2.4.1 on Ubuntu 10.04 64bits. I executed the
example with C++ and Java and it worked fine.
I'm now trying to write my own program, which is composed of two
parts. I want to exchange a set of data (a message containing two
fields: phi and distance) between a C++ (server) program and a Java
(client) program.
The C++ part which is the server opens a TCP/IP socket and waits for
the Java part to send data. The Java part opens a socket on the same
port/address as the C++ part and sends data.
The socket seems to work, but I get the following error :
C++ part :
Start !
recv size:4
recv size:21
recv size:0
libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse
message of type "gpb.IRobotData" because it is missing required
fields: phi, distance
has not phi
has not distance
phi:0, distance:0
Quit !
Java part:
Start !
phi:0.5, distance:1.6
Quit !
The important parts of the code are the following :
IRobotData.proto :
----------------------------------------------------------------
package gpb;
option java_package = "gpb";
option java_outer_classname = "IRobotProto";
message IRobotData {
required double phi = 1;
required double distance = 2;
}
----------------------------------------------------------------
C++ :
----------------------------------------------------------------
#include <iostream>
#include <ostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "iRobot.pb.h"
gpb::IRobotData data;
char buf[1000];
string strbuf;
ssize_t recvSize;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock != INVALID_SOCKET)
{
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sock_err = bind(sock, (SOCKADDR*)&sin, recsize);
if(sock_err != SOCKET_ERROR)
{
sock_err = listen(sock, 5);
if(sock_err != SOCKET_ERROR)
{
csock = accept(sock, (SOCKADDR*)NULL, NULL);
string getData;
while( (recvSize = recv(csock, buf, sizeof(buf), 0)) > 0)
{
cout << "recv size:" << recvSize << endl;
if(recvSize < 0)
cerr << "recv() failed" << endl;
strbuf.append(buf, recvSize);
}
cout << "recv size:" << recvSize << endl;
data.ParseFromString(strbuf);
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;
}
else
{
cerr << "listen failed" << endl;
}
}
else
{
cerr << "bind failed" << endl;
}
shutdown(csock, 2);
close(csock);
close(sock);
}
else
{
cerr << "socket failed" << endl;
}
----------------------------------------------------------------
Java :
----------------------------------------------------------------
Socket sock;
try {
sock = new Socket("localhost", 10101);
ObjectOutputStream oos = new
ObjectOutputStream(sock.getOutputStream());
CodedOutputStream cos =
CodedOutputStream.newInstance(oos);
//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();
/*
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} */
sock.close();
} catch (UnknownHostException e1) {
System.err.println("Host inconnu !");
e1.printStackTrace();
} catch (IOException e1) {
System.err.println("I/O erreur !");
e1.printStackTrace();
}
----------------------------------------------------------------
Do you have any idea why I got the error ? I might not manage
correctly the serialisation/deserialisation thing, but I don't know
how to improve it.
Thanks.
--
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.