协议缓冲区未知类型名称错误?

时间:2014-02-05 07:24:11

标签: c ubuntu protocol-buffers rpc zeromq

服务器代码

//  Hello World server
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "amessage.pb-c.h"
#include <google/protobuf-c/protobuf-c-rpc.h>

int main (void)
{

AMessage msg = AMESSAGE__INIT; // AMessage
void *buf;                     // Buffer to store serialized data
unsigned len;                  // Length of serialized data

// Initializing Socket Communication via zeromq
void *Context = zmq_ctx_new ();   
void *responder = zmq_socket (Context, ZMQ_REP);
void *requester = zmq_socket (Context, ZMQ_REQ);

int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);

// Main Code    
while (1) {
    char buffer [10];
    zmq_recv (responder, &msg, len, 0); // Defining arguments via zeromq
    printf ("Server Responding-->\n");
    sleep (1);          //  Do some 'work'
pid_t childpid;
childpid = fork();
// Check for input  
if (childpid == 0)
{
printf ("I am here");
// Entering Required output command, in this case 'ls' is the command
FILE *proc=popen("/bin/ls -al","r");
char buf2[1024];
    while(!feof(proc) && fgets(buf2,sizeof(buf2),proc))  // reading the output

    {    
        printf("%s",buf2);

        //Defining buffer via Protobuf-c
        strcpy (buf, (void *)buf2);
        amessage__pack(&msg,buf); // Packing / serializing the message 
        void *requester = zmq_socket (Context, ZMQ_REQ);
        len=amessage__get_packed_size(&msg);    // Defining length 
        zmq_send (requester, &msg, len, 0);
    }

}
  free(buf);    
  }
   return 0;
  }

客户代码

//  Hello World client
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "amessage.pb-c.h"
#define MAX_MSG_SIZE 1024
#include "google/protobuf-c/protobuf-c-rpc.h"

// Defining Buffer to save output file till it runs.

AMessage msg = AMESSAGE__INIT; // AMessage
void *buf;                     // Buffer to store serialized data
unsigned len;                  // Length of serialized data

static size_t
read_buffer (unsigned max_length, uint8_t *out)
{
size_t cur_len = 0;
uint8_t c;
int nread =0;
void *Context = zmq_ctx_new ();   
    void *responder = zmq_socket (Context, ZMQ_REP);
    while ((nread=zmq_recv (responder, &msg, len, 0)) !=0) // Reading the output
{
cur_len += nread;
if (cur_len == max_length)
    {
        fprintf(stderr, "max message length exceeded\n"); // Buffer length
        exit(1);
    }
}
  return cur_len;
  }

  //Main Function
  int main (void)

 {
 printf ("Connecting to hello world server...\n"); //Testing Connection
 void *Context = zmq_ctx_new ();
 void *requester = zmq_socket (Context, ZMQ_REQ);
 zmq_connect (requester, "tcp://localhost:5555");  //zeromq socket connection

  int request_nbr;

    char buffer [10];

    zmq_send (requester, &msg, len, 0);

Amessage *msg; // Defining protocol buffer
//Read packed message
uint8_t buf[MAX_MSG_SIZE];
size_t msg_len = read_buffer (MAX_MSG_SIZE, buf);

//Unpacking the message using protobuf
msg = amessage__unpack(NULL, msg_len, buf);
if (msg==NULL)
  {
    fprintf(stderr, "error unpacking\n");
    exit(1);
  }

//Display the message fields
printf("Received: a=%d", msg->a);   //required output
if (msg->has_b)
    printf("  b=%d", msg->b);
    printf("\n");

//Free the unpacked message
amessage__free_unpacked(msg, NULL);
    zmq_close (requester);
    zmq_ctx_destroy (Context);
    return 0;
    }

和我的.proto文件

   message AMessage
   {
   required int32 a=1; 
   optional int32 b=2;
   }

编译客户端文件时,我收到此错误

“未知类型名称'Amessage'

请在这里帮助我,为什么客户端文件在服务器链接时没有链接到.proto文件。

2 个答案:

答案 0 :(得分:0)

在客户端代码中,len在哪里初始化?

一般来说,我建议您尝试使用valgrind内存跟踪器来检测无效的内存操作。

只需替换

./your_cmd arg1 arg2

valgrind ./your_cmd arg1 arg2

答案 1 :(得分:0)

此行(以及其他行)不正确:

while ((nread=zmq_recv (responder, &msg, len, 0)) !=0) // Reading the output

msg这里是一个protobuf对象,但你试图像字节缓冲区一样读入它。这不是protobuf的工作原理。您需要读入一个char数组,然后使用amessage__unpack来解析它。

此外,正如nodakai所说,len在这里未初始化。