Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:59:59+00:00 2026-06-04T12:59:59+00:00

I’m trying to write some example of client-server application which uses linux messages. Here

  • 0

I’m trying to write some example of client-server application which uses linux messages. Here is my code:

#include <mqueue.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#define MSG_SIZE 4096
#define MSG_MAX_COUNT 1024
#define MSG_TYPE_TO_UPPER 0
#define MSG_TYPE_EXIT 1
#define MQ_NAME "msg_queue"

namespace {
    int str_toupper(char *str)
    {
        int len = 0;
        for(; str[len]; ++len) {
            str[len] = toupper(str[len]);
        }
        return len;
    }
}

int main(int argc, char** argv)
{
    if(argc != 2) {
        fprintf(stderr, "Usage: msg_queue (client|server)\n");
        exit(EXIT_FAILURE);
    }

    struct mq_attr attr;    // MQueue attributes
    mqd_t mqd;      // MQueue descriptor
    char buf[MSG_SIZE]; // Msg buffer
    unsigned int type;  // Msg type(priority)

    // Set up MQueue attributes
    attr.mq_maxmsg = MSG_MAX_COUNT;
    attr.mq_msgsize = MSG_SIZE;
    attr.mq_flags = 0;
    attr.mq_curmsgs = 0;

    mqd = mq_open(MQ_NAME, O_RDWR | O_CREAT, 0664, &attr);
    if(mqd == -1) {
        fprintf(stderr, "mq_open() failed for \""MQ_NAME"\": %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    if(strcmp(argv[1], "server") == 0) {
        while(mq_receive (mqd, buf, MSG_SIZE, &type) != -1) {
            if(type == MSG_TYPE_EXIT) {
                mq_unlink(MQ_NAME);
                mq_close(mqd);
                break;
            } else if(type == MSG_TYPE_TO_UPPER) {
                int len = str_toupper(buf);
                if(mq_send (mqd, buf, len, MSG_TYPE_TO_UPPER) == -1) {
                    fprintf(stderr, "Server: mq_send() failed: %s", strerror(errno));
                }
            }
        }
    } else if(strcmp(argv[1], "client") == 0) {
        while(1) {
            printf("Input a message: <type>(0 - TO_UPPER, 1 - EXIT) <message>\n");
            scanf("%u %s", &type, buf);

            if(mq_send (mqd, buf, strlen(buf), type) == -1) {
                fprintf(stderr, "Client: mq_send() failed: %s", strerror(errno));
            }

            if(type == MSG_TYPE_TO_UPPER) {
                if(mq_receive (mqd, buf, MSG_SIZE, &type) == -1) {
                    fprintf(stderr, "Client: mq_receive() failed: %s", strerror(errno));
                }
                printf("\"%s\" received\n", buf);
            } else if(type == MSG_TYPE_EXIT) {
                mq_unlink(MQ_NAME);
                mq_close(mqd);
                break;
            }
        }
    } else {
        fprintf(stderr, "Usage: msg_queue (client|server)\n");
        exit(EXIT_FAILURE);
    }
    return 0;
}

What is my mistake? It always prints error from the line 47 – fprintf(stderr, "mq_open() failed for \""MQ_NAME"\": %s\n", strerror(errno)); with errno = EINVAL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:00:01+00:00Added an answer on June 4, 2026 at 1:00 pm

    I see two issues:

    • Your message queue name must begin with a / on Linux. See mq_overview(7):

    Each message queue is identified by a name of the form
    /somename; that is, a null-terminated string of up to NAME_MAX (i.e., 255)
    characters consisting of an initial slash, followed by one or more characters,
    none of which are slashes.

    • MSG_MAX_COUNT is most likely above your system limits. It must be less than (or equal to) the /proc/sys/fs/mqueue/max_size. See mq_open(3):

    EINVAL: O_CREAT was specified in oflag, and attr was not NULL, but
    attr->mq_maxmsg or attr->mq_msqsize was invalid. Both of these fields
    must be greater than zero. In a process that is unprivileged (does not
    have the CAP_SYS_RESOURCE capability), attr->mq_maxmsg must be less
    than or equal to the msg_max limit
    , and attr->mq_msgsize must be less
    than or equal to the msgsize_max limit. In addition, even in a
    privileged process, attr->mq_maxmsg cannot exceed the HARD_MAX limit.
    (See mq_overview(7) for details of these limits.)

    The other limit is probably ok, but you should verify it too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.