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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:58:17+00:00 2026-05-23T22:58:17+00:00

A program written in C++ reads from a network socket and then writes to

  • 0

A program written in C++ reads from a network socket and then writes to local socket. It uses a separate thread for the “read”.

When a message is read, it is put into a char * queue (using mutex from the boost libraries to make it thread-safe).

Meanwhile the queue is checked to see if it is empty, if it isn’t, then the first message is popped from the queue (again using mutex) and written to the local socket as a char *.

My issue is: when a message of 4 bytes is pushed onto the queue, the queue saves it without issue, however when writing the message back out again, it has increased the message to eight bytes! The “new” four bytes are zero.


Example A

Message in: {4,0,0,0}
Saved to queue as; <4>, <0>, <0>, <0>
Read from queue as:  <4>, <0>, <0>, <0>, <0>, <0>, <0>, <0>

Example B

Message in: {4,0,0,0,8,0,0,0}
Saved to queue as; <4>, <0>, <0>, <0>, <8>, <0>, <0>, <0>
Read from queue as:  <4>, <0>, <0>, <0>, <8>, <0>, <0>, <0>

Any ideas as to the reason for this? Can the queue class only cope with a minimum number of chars? (Wouldn’t have thought so as there’s the “empty” method).
(This isn’t a major issue, as I never talk in less than eight bytes; I just want to know in case it comes up and attacks me later on in life.)

I did a bit of digging around online and through the documentation and found the odd referece to a buffer, but that seems to be more to do with using the queue as a buffer, rather than it having one…

Other information;

OS: RedHat

IDE: Eclipse


Code: Queue

//Thread-safe call to save the message to the queue
void MessageQueue::SaveToQueue(char* Data)
{
// Lock the mutex to prevent any other threads accessing this member (released when method exits).
boost::mutex::scoped_lock l(m_Msg);

//int i = 0;
//while (i < sizeof(Data))//iLength)
//{
//  unsigned int ByteVaule = Data[i];//pBuffer[i];//ByteValue = int(pBuffer[i]);//unsigned int(pBuffer[i]);
//  cout << "Buffer in Queue" << i << ": " << ByteVaule << endl;
//  i++;
//}

MsgQ.push(Data);
}

//Thread-safe call to get the message from the queue
char* MessageQueue::GetFromQueue()
{
// Lock the mutex to prevent any other threads accessing this member (released when method exits).
boost::mutex::scoped_lock l(m_Msg);
char* message = MsgQ.front();
MsgQ.pop();
return message;
}

//Thread-safe call to check if the queue is empty
bool MessageQueue::IsEmpty()
{
// Lock the mutex to prevent any other threads accessing this member (released when method exits).
boost::mutex::scoped_lock l(m_Msg);
return MsgQ.empty();
}

Code: Manager
int iStatus = 0;

//Start class to store message queue
MessageQueue* pQueue = new MessageQueue();

// Current hard coded value for the write scoket location
// TODO: change this to reading from enviroment variable
string WritePath = "DefaultSocket";

ReadSocket* pRead = new ReadSocket();
WriteSocket* pWrite = new WriteSocket();
cout << "Creating read socket" << endl;
iStatus = pRead->CreateSocket(pQueue);
cout << "Creating write socket." << endl;
iStatus = pWrite->CreateSocket(WritePath);

//while is running, check the message container and process it as needed
while (pRead->IsRunning())
{
    while (!(pQueue->IsEmpty()))
    {
        char* Msg = pQueue->GetFromQueue();

        iStatus = pWrite->WriteToSocket(Msg);
        // TODO: catch on failure
    }
    //sleep(1);
}
//Destroy sockets as program is closing
pWrite->~WriteSocket();
pRead->~ReadSocket();

// TODO: add exception?
//Token return
return iStatus;

To save making this too long and complex the read and write sockets are the same as in

http://www.linuxhowtos.org/C_C++/socket.htm

The read char* is saved to the queue using the

SaveToQueue()

method and taken from the queue using the

GetFromQueue

method.

  • 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-23T22:58:17+00:00Added an answer on May 23, 2026 at 10:58 pm

    std::queue has no such limitation. What you are seeing must be the result of problems in your code. (You say yourself, that you never take in less than 8 bytes?!)

    Edit: What seems odd in the code samples is memory management (explicit calls to destructors, instead of delete; the memory of the char* -s doesn’t seem to be released – how is it allocated?).

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

Sidebar

Related Questions

I have a program which reads data from 2 text files and then save
I've written a program that uses fsockopen() and fgets() to read a website's content
I've written a C program to extract files from a tar archive using libarchive.
I've got a program that in a nutshell reads values from a SQL database
I have written a program that reads in a File object (really an XML
I've written a C# program using the Google Docs .NET API to read a
I have a console program written in C# that I am using to send
I have a data acquisition program written in C++ (Visual Studio 6.0). Some clients
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have a fairly large program written in C. It spans several files, and

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.