Message too long at mq_receive

I don't know why I'm getting the error Message too long at mq_receive. I have looked at all the posts that are similar to my question and I have tried what they said and still I cannot fix the problem. Can someone help me please?

#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;
}

Upvotes: 0

Views: 1534

Answers (2)

The problem was that the struct mq_attr should be passed at mq_open as a pointer. For some reason I don't understand, the compilar didn't warn me. That solve my problem. So the solution is to subtitute attr1 at mq_open and use &attr1.

Upvotes: 1

chqrlie
chqrlie

Reputation: 144750

Quoting the manual page at http://man7.org/linux/man-pages/man3/mq_receive.3.html

EMSGSIZE msg_len was less than the mq_msgsize attribute of the message queue.

The process that posted the message passed a length that is larger than sizeof(pet). The structure definitions (that you did not post) do not match, or the message payload is of a different type.

Upvotes: 0

Related Questions