C++队列不喜欢少于 8 个字符*
用 C++ 编写的程序从网络套接字读取数据,然后写入本地套接字。它使用单独的线程进行“读取”。
当消息被读取时,它被放入 char * 队列中(使用 boost 库中的互斥体以使其线程安全)。
同时检查队列是否为空,如果不是,则从队列中弹出第一条消息(再次使用互斥体)并以 char * 形式写入本地套接字。
我的问题是:当将 4 字节的消息推送到队列时,队列会毫无问题地保存它,但是当再次将消息写回时,它已将消息增加到 8 字节! “新的”四个字节为零。
示例 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>
示例 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>
对于其原因有什么想法吗?队列类只能处理最小数量的字符吗? (不会这么想,因为有“空”方法)。 (这不是一个主要问题,因为我从来不会用少于八个字节的内容进行交谈;我只是想知道它是否会在以后的生活中出现并攻击我。)
我在网上和文档中进行了一些挖掘并发现了对缓冲区的奇怪引用,但这似乎更多地与使用队列作为缓冲区有关,而不是它有一个......
其他信息;
操作系统:RedHat
IDE:Eclipse
代码:队列
//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();
}
代码: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;
相同
http://www.linuxhowtos.org/C_C++/socket.htm
为了避免使其变得太长和复杂,读取和写入套接字与使用该方法将读取的 char* 保存到队列
SaveToQueue()
中并使用该方法从队列中取出
GetFromQueue
。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::queue
没有这样的限制。您所看到的一定是代码中存在问题的结果。 (你自己说,你从不接受少于 8 个字节?!)编辑:代码示例中看起来奇怪的是内存管理(显式调用析构函数,而不是 delete;
char*
-s 似乎没有被释放 - 它是如何分配的?)。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?).供参考;
char* 是指向 char 的指针,占用 8 个字节(取决于机器?),这意味着当我尝试输出指针的内容时,它会输出所有 8 个字节的大小;我想要的四个后面是四个空的。
要亲自查看这一点,您可以使用以下代码:
它为您提供了以下 O/P;
所以我可以使用 char,这样我就可以解决这个问题了!
For information;
A char* is a pointer to the char, and takes up 8 bytes (machine dependant?), meaning that when I try to output the pointer's contents, it outputs all of its size of 8 bytes; the four I want followed by four empty ones.
To see this for yourself, you can use the following code:
Which gives you the following O/P;
So I could have used char, and saved myself the question!