2014-02-05 108 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未初始化在這裏。