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

  • SEARCH
  • Home
  • 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 8155237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:36:46+00:00 2026-06-06T16:36:46+00:00

Here I have to send and receive dynamic data using a SysV message queue.

  • 0

Here I have to send and receive dynamic data using a SysV message queue.

so in structure filed i have dynamic memory allocation char * because its size may be varies.

so how can i receive this type of message at receiver side.

Please let me know how can i send dynamic length of data with message queue.

I am getting problem in this i posted my code below.

send.c

/*filename   : send.c
 *To compile : gcc send.c -o send
 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct my_msgbuf {
    long mtype;
    char *mtext;
};

int main(void)
{
    struct my_msgbuf buf;
    int msqid;
    key_t key;
    static int count = 0;
    char temp[5];
    int run = 1;
    if ((key = ftok("send.c", 'B')) == -1) {
        perror("ftok");
        exit(1);
    }

    printf("send.c Key is = %d\n",key);

    if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {
        perror("msgget");
        exit(1);
    }

    printf("Enter lines of text, ^D to quit:\n");

    buf.mtype = 1; /* we don't really care in this case */
    int ret = -1;
    while(run) {
        count++;
        buf.mtext = malloc(50);
        strcpy(buf.mtext,"Hi hello test message here");
        snprintf(temp, sizeof (temp), "%d",count);
        strcat(buf.mtext,temp);
        int len = strlen(buf.mtext);
        /* ditch newline at end, if it exists */
        if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';
        if (msgsnd(msqid, &buf, len+1, IPC_NOWAIT) == -1) /* +1 for '\0' */
        perror("msgsnd");
        if(count == 100)
            run = 0;
        usleep(1000000);
    }

    if (msgctl(msqid, IPC_RMID, NULL) == -1) {
        perror("msgctl");
        exit(1);
    }

    return 0;
}

receive.c

/* filename   : receive.c
 * To compile : gcc receive.c -o receive
 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct my_msgbuf {
    long mtype;
    char *mtext;
};

int main(void)
{
    struct my_msgbuf buf;
    int msqid;
    key_t key;

    if ((key = ftok("send.c", 'B')) == -1) {  /* same key as send.c */
        perror("ftok");
        exit(1);
    }

    if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
        perror("msgget");
        exit(1);
    }

    printf("test: ready to receive messages, captain.\n");

    for(;;) { /* receive never quits! */
        buf.mtext = malloc(50);
        if (msgrcv(msqid, &buf, 50, 0, 0) == -1) {
            perror("msgrcv");
            exit(1);
        }
        printf("test: \"%s\"\n", buf.mtext);
    }

    return 0;
}
  • 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-06T16:36:47+00:00Added an answer on June 6, 2026 at 4:36 pm

    A couple of ways to solve your problem are:

    1. Make the messages fixed length.
    2. Send a fixed length “header” that includes the message length.
    3. Send a terminator, since you seem to send strings include the terminating '\0'.

    Edit: How to use msgsnd and msgrcv:

    Your usage of the structure and msgsnd is wrong, as the function expects the whole message to be one continuous memory area. Examples such as this use a structure with normal fields in it, or like this (at the bottom) which uses a fixed length string array.

    You can send dynamic data having the structure size being dynamic as well. The trick here is to use a small fixed-size structure, and allocate more data than is needed.

    Lets rewrite parts of your example sender code:

    struct my_msgbuf {
        long   mtype;     /* Message type, must be > 0 */
        char   mtext[1];  /* Some compilers allow `char mtext[0]` */
    };
    
    /* ... */
    
    
    int count = 0;
    while (count < 100) {
        count++;
    
        /* Put string in a temporary place */
        char tmp[64];
        snprintf(tmp, sizeof(tmp), "Hi hello test message here %d", count);
    
        /* +1 for the terminating '\0' */
        size_t msgsz = strlen(tmp) + 1;
    
        /* Allocate structure, and memory for the string, in one go */
        struct my_msgbuf *buf = malloc(sizeof(struct my_msgbuf) + msgsz);
    
        /* Set up the message structure */
        buf->mtype = 1;
        memcpy(buf->mtext, tmp, msgsz);
    
        /* And send the message */
        msgsnd(msgid, buf, msgsz, IPC_NOWAIT);
    
        /* Remember to free the allocated memory */
        free(buf);
    }
    

    The above code handles sending of dynamic strings, as long as the are less than 63 characters (the size of the temporary string minus one).

    Unfortunately msgrcv doesn’t really support receiving of dynamically sized data. This can be helped by not using the MSG_NOERROR flag, and check for error E2BIG and then using realloc to get a bigger message buffer.

    Something like this for receiving:

    /* Should start with larger allocation, using small just for example */
    size_t msgsz = 8;
    struct my_msgbuf *buf = NULL;
    
    for (;;) {
        /* Allocate if `buf`  is NULL, otherwise reallocate */
        buf = realloc(buf, msgsz);
    
        /* Receive message */
        ssize_t rsz = msgrcv(msgid, buf, msgsz, 1, 0);
    
        if (rsz == -1) {
            if (errno == E2BIG)
                msgsz += 8;  /* Increase size to reallocate and try again */
            else {
                perror("msgrcv");
                break;
            }
        } else {
            /* Can use `buf->mtext` as a string, as it already is zero-terminated */
            printf("Received message of length %d bytes: \"%s\""\n", rsz, buf->mtext);
            break;
        }
    }
    
    if (buf != NULL)
        free(buf);
    

    The above code for receiving only receives one single message. If you want it to match the sender which sends lots of messages, then put the receiving code in a function, and call it in a loop.

    DISCLAIMER: This code is written directly in the browser, only reading the manual pages. I have not tested it.

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

Sidebar

Related Questions

I have a 'message' table, where users send and receive messages, pretty straight forward.
Here is what I have currently, I'm doing this: current_account.send(object).search(params[:search]).user_id_equals_any(users).visibility_is_any(visibilities) but thats not very
Here I have declared another virtual function in Derived class. #include <iostream> using namespace
Here I have an arbitrary IEnumerable<T> . And I'd like to page it using
I'm using the excellent OpenIso8583Net to send/receive ISO messages. However, since every organization has
I am trying to send and receive SMS via kannel. I have set everything,
Here's my trouble: I have a udp class that allow me to send and
I am currently working on a basic send and receive program using UDP in
I am writing an iPhone application that needs to send and receive data over
I was searching how to send and receive SMS and I have searched a

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.