I have two applications that communicate over TCP/IP written in C++ that use protobuf library to exchange data defined into a proto message.
floatExample.proto
syntax = "proto3";
message FloatMessage {
optional float value = 1;
}
serverFloatExample.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdint>
#include <iomanip>
#include "floatExample.pb.h"
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[1024];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
// create a socket
// socket(int domain, int type, int protocol)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
// clear address structure
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
error("ERROR on binding");
return -1;
}
listen(sockfd,5);
// The accept() call actually accepts an incoming connection
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
error("ERROR on accept");
return -1;
}
printf("server: got connection from %s port %d\n",
inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port));
while(true)
{
sleep(2);
bzero(buffer,1024);
n = read(newsockfd,buffer,1024);
if (n < 0) error("ERROR reading from socket");
{
printf("Here is the message size: <%d>\n", n);
}
FloatMessage receivedMsg = FloatMessage();
receivedMsg.ParseFromString(buffer);
printf("VALUE: <%.2f>\n", receivedMsg.value());
receivedMsg.PrintDebugString();
}
close(newsockfd);
close(sockfd);
return 0;
}
clientFloatExample.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "floatExample.pb.h"
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting");
FloatMessage float_message;
float_message.set_value(1.0);
float_message.PrintDebugString();
std::string serialized_message = float_message.SerializeAsString();
n = write(sockfd, serialized_message.c_str(), serialized_message.size());
if (n < 0)
{
error("ERROR writing to socket");
}
close(sockfd);
return 0;
}
The bug is the following: sometimes some of the float values received by server application are not the same as the values transmitted by client application.
For example (using protobuf serialization):
Can anyone please suggest me where is the problem or help with this?
This line contains the problem:
receivedMsg.ParseFromString(buffer);
ParseFromString
is documented as taking a std::string&
, but you give it a char*
. This invokes the std::string::string(char*)
constructor that only reads up to the first NUL byte.
The correct solution is to just use the ParseFromArray
method that takes a pointer and a size:
receivedMsg.ParseFromArray(buf, n);