2014-02-05 78 views
0

服务器代码协议缓冲区未知类型名称错误?

// 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文件,而服务器正在链接。

+0

也许看看大小写敏感度:在proto中你有“AMessage”,在代码“Amessage”中...... – Ralf

+0

哦,是的..非常感谢让我的生活变得有点容易,我只是这么做了。 ..它确实编译成功,但是当我运行代码./hwserver和./client时,显示hello消息后,它给出错误“分段错误(核心转储)” – user3273894

回答

0

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

一般来说我建议你尝试valgrind内存示踪剂检测无效的内存操作。

只需更换

./your_cmd arg1 arg2 

valgrind ./your_cmd arg1 arg2 
+0

感谢您的回复。 PLZ告诉我你是什么意思的ARG1和ARG2,我只是通过执行代码./mycode – user3273894

+0

我试着用它与./valgrind但程序保持执行,永不停止 – user3273894

+0

具体来说,你应该替换'./ hwserver'与'valgrind。/ hwserver'和'。/ client'与'valgrind。/ client'。 你在客户端代码中是否修复了(len)'len'的初始化? – nodakai

0

这条线(其中包括)不正确:

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

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

此外,作为nodakai笔记,len未初始化在这里。