0
votes

I am trying to write to my message queue (mq_send).

Here is my code for opening the queue first and then for writing to it.

Opening:

int MQconnect (mqd_t * mq, char * name)
{
    //printf("hello from MQconnect\n");
    do{
        mq=mq_open(name, O_WRONLY);  //O_RDONLY
    }while(mq==-1);

    if(mq== -1){
        return 0;
    }
    else
        return 1;
    // Connects to an existing mailslot for writing Uses mq as reference pointer, so that you can     reach the handle from anywhere/
    // Should return 1 on success and 0 on fail*/
}

Writing:

int MQwrite (mqd_t mq, void * sendBuffer) // (const char) sendBuffer
{
    int nrOfBytes = mq_send(mq, (const char)sendBuffer, 1024, 10);

    printf("\n%d", nrOfBytes);

    return nrOfBytes; //nrOfBytes;

    // Write a msg to a mailslot, return nr Uses mq as reference pointer, so that you can reach the handle from anywhere
    // should return number of bytes read         */
}

The opening works fine but I cant write to the message queue.

mq_send return -1 as return value and the error message is:

Value of errno: 9 Error printed by perror: Bad file descriptor Error: Bad file descriptor

The call to the above functions happens from this function:

void * mqClient(void * arg){
    pthread_mutex_lock(&mutex);

    char answer[20];
    mqd_t mq_on_server;
    usleep(1000);
    int response = MQconnect(&mq_on_server, "/servermq");
    if(response==0){
        printf("something went wrong with MQconnect\n");
    }
    else{
        //This loop continously scans planets given by the user
        //while(!(0)){
    printf("\nWrite to mailbox: ");
    scanf("%s", answer);


    MQwrite (mq_on_server, &answer);
    int c;
    while ( (c = getchar()) != '\n' && c != EOF);
    //fflush(stdout);
        //}

    }

    pthread_mutex_unlock(&mutex);
}

Anyone have an idea why I am getting this kind of error? My friend has the exact same code and for him it is working.

1
How can (mq== -1) be true after while(mq==-1)?Gnoom
My bad. The code is not perfect yet, will go through it.Saef Jasem
regarding: int nrOfBytes = mq_send(mq, (const char)sendBuffer, 1024, 10); The second parameter should be a pointer, not a char array. Suggest: `int nrOfBytes = mq_send(mq, (const char*)sendBuffer, 1024, 10);user3629249
regarding: scanf("%s", answer); The variable answer is only 20 bytes, so very easy to overflow the array answer[] To avoid that problem, use a 'max characters' modifier of 19. (19 because %s always appends a NUL byte to the input. Suggest: scanf("%19s", answer);user3629249
regarding: MQwrite (mq_on_server, &answer); in C, a bare reference to an array degrades to the address of the first byte of the array, so that & needs to be removeduser3629249

1 Answers

1
votes

You forgot to dereference the given pointer in MQconnect.

int MQconnect (mqd_t * mq, char * name)
{
    //printf("hello from MQconnect\n");
#if 0
    /* wrong part */
    do{
        mq=mq_open(name, O_WRONLY);  //O_RDONLY
    }while(mq==-1);

    if(mq== -1){
#else
    /* fixed code */
    do{
        *mq=mq_open(name, O_WRONLY);  //O_RDONLY
    }while(*mq==-1);

    if(*mq== -1){
#endif
        return 0;
    }
    else
        return 1;
    // Connects to an existing mailslot for writing Uses mq as reference pointer, so that you can     reach the handle from anywhere/
    // Should return 1 on success and 0 on fail*/
}