2016-02-27 62 views
0

我不知道为什么我得到错误Message too long at mq_receive。我查看了所有与我的问题类似的帖子,我尝试了他们所说的话,但仍无法解决问题。有人能帮助我吗?mq_receive的留言太长了

#include <stdio.h> 

int main(int argc, char **argv) { 
    struct peticion pet; 
    struct respuesta res; 
    struct mq_attr attr1; 

    attr1.mq_msgsize = sizeof(pet); 
    attr1.mq_maxmsg = 10; 

    mqd_t cS = mq_open("/servidor", O_CREAT | O_RDONLY, 0700, attr1); 
    if (cS == -1) { 
     printf("ERROR: No se ha podido abrir la cola del servidor\n"); 
     exit(-1); 
    } 

    while (1) { 
     if (mq_receive(cS, (char*)&pet, sizeof(pet), 0) == -1) { 
      printf("ERROR: El servidor no ha sido capaz de recibir peticiones\n"); 
      exit(-1); 
     } 
     switch (pet.cod) { 
      case 0: 
      init(pet.colaCliente); 
      break; 
      case 1: 
      introducirPar(pet.key, pet.value, pet.colaCliente); 
      break; 
      case 2: 
      obtenerValor(pet.key, pet.colaCliente); 
      break; 
      case 3: 
      borrarPar(pet.key, pet.colaCliente); 
      break; 
     } 
    } 

    mq_close(cS); 
    mq_unlink((const char*)&cS); 

    return 0; 
} 

回答

1

问题是struct mq_attr应作为指针在mq_open处传递。出于某种原因,我不明白,该同事没有警告我。这解决了我的问题。所以解决方案是在mq_open中替代attr1并使用&attr1

+0

好抓!你应该编译更多的警告:'gcc -Wall -Wextra'或'clang -Weverything'。 – chqrlie