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 3407296
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:44:01+00:00 2026-05-18T05:44:01+00:00

I have a requirment to have communication between two processes using message queues. One

  • 0

I have a requirment to have communication between two processes using message queues. One process sends the request to another process and other process sends response.

For example one of the request is list all the filenames opened by the process. I have made interface structure as

 #define LIST_NAMES   1
 #define LIST_FILE_NAMES_RESP  2

 struct sFileStruct {
  unsigned int  uiCommand;
  unsigned long ulNoOfBytes; // Number of bytes to be parsed in cha* pointer below
  char*         pRecvData;   // file names packed here in response.
 };


 sFileStruct inData;
 // I am filling the data.
 int inSize = sizeof(inData);

 mq_send(m_qSendDesc, (char*)&inData, inSize, inPriority);

I think problem with above design is that as file names legth is changing char* pointer pointing to data is different, but size of structure is always constant, so receiver is not receving all the data and receiver is crashing while accessing the char* pointer.

I want to sent full data in one mq_send, and don’t want to have static array inside structure. Is there any other way using message queues we can achieve this.

Please provide your inputs.
Thanks

  • 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-05-18T05:44:01+00:00Added an answer on May 18, 2026 at 5:44 am

    You could make a wrapper over mq_send() having char buffer allocated.

    Every message you send will be serialized into this buffer and then you will pass it to mq_send with appropriate length – sizeof(int) + sizeof(long) + strlen(pRecvData).

    The receiver will read this data and deserialize it into sFileStruct.

    IMHO using std::string instead of char* would be better.

    Sample code (not tested):

    class MQWrapper
    {
        std::string m_strBuffer;
    public:
        int Send( const sFileStruct& sfs )
        {
            m_strBuffer.clear();
            m_strBuffer.append( static_cast<const char*> & sfs.uiCommand, sizeof(sfs.uiCommand) );
            m_strBuffer.append( static_cast<const char*> & sfs.ulNoOfBytes, sizeof(sfs.ulNoOfBytes) );
            m_strBuffer.append( sfs.pRecvData ); // only if it's zero-terminated string!!!
    
            return mq_send(m_qSendDesc, m_strBuffer.c_str(), m_strBuffer.length(), inPriority);
        }
    
    
        char m_receiveBUffer[ BUFFER_SIZE ];
    
        int Receive( sFileStruct& sfs )
        {
            int res = mq_receive( m_qSendDesc, receiveBUffer, BUFFER, outPriority );
            if( res < 0 )
                return res;
            if( res < sizeof(int) + sizeof(long) )
                return -1000; // malformed message
            sfs.uiCommand = * ( static_cast<unsigned int*> (m_receiveBUffer[0]) );
            sfs.ulNoOfBytes = * ( static_cast<long*> (m_receiveBUffer[ sizeof(int) ] ) );
    
            // I don't really use this style in my work and not sure how to use it
            // str::string would be much easier
            int stringLen = res - sizeof(int) - sizeof(long);
            sfs.pRecvData = new char [ stringLen + 1 ]; // you have to delete [] it later
            if( stringLen > 0 )
                strcpy( sfs.pRecvData, receiveBUffer + sizeof(int) + sizeof(long), stringLen );
            ss.pRecvData[ stringLen ] = '\0'; // null terminator
            return 0;
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.