服务器代码

//  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文件。

最佳答案

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

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

只需更换

./your_cmd arg1 arg2




valgrind ./your_cmd arg1 arg2



http://valgrind.org/docs/manual/quick-start.html

关于c - Protocol Buffer 未知类型名称错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21571038/

10-08 21:28