- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的客户端设置如下:客户端首先发送一个数据包,其中包含要从套接字读取的实际数据的长度。然后客户端自己发送实际数据。 (我在实际发送数据之前预先设置了数据包长度)。
我想知道如何在 Qt Creator 中读取下一个数据包。我收到一条错误消息,提示未找到此类插槽:MyThread::readPacket (int)。我做错了什么?
(如果我读取的字节数错误,请忽略该部分)
我的服务器端代码如下:
#include "thread.h"
#include <QDebug>
#include "DataSetProtos.pb.h"
MyThread::MyThread(qintptr ID, QObject *parent) :
QThread(parent)
{
this->socketDescriptor = ID;
}
void MyThread::run()
{
// thread starts here
qDebug() << " Thread started";
socket = new QTcpSocket();
// set the ID
if(!socket->setSocketDescriptor(this->socketDescriptor))
{
// something's wrong, we just emit a signal
emit error(socket->error());
return;
}
// connect socket and signal
// note - Qt::DirectConnection is used because it's multithreaded
// This makes the slot to be invoked immediately, when the signal is emitted.
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
// We'll have multiple clients, we want to know which is which
qDebug() << socketDescriptor << " Client connected";
// make this thread a loop,
// thread will stay alive so that signal/slot to function properly
// not dropped out in the middle when thread dies
exec();
}
void MyThread::readyRead()
{
// get the information
char* buffer = new char[SIZE];
int iResult= socket->read(buffer, SIZE);
connect(socket,SIGNAL(readyRead()), this, SLOT(readPacket(iResult)), Qt::DirectConnection);
}
void MyThread::readPacket(int bufferLength)
{
char *buffer = new char[bufferLength];
int iResult = socket->read(buffer, bufferLength);
printf("\n Read 2 : %d", iResult);
}
void MyThread::Print(Visualization::PacketData packet)
{
printf("\n\nPacket Name : %s", packet.name());
printf("\n Packet length : %d ", packet.length());
}
void MyThread::disconnected()
{
qDebug() << socketDescriptor << " Disconnected";
socket->deleteLater();
exit(0);
}
我的thread.h文件如下:
#ifndef THREAD_H
#define THREAD_H
#include <QThread>
#include <QTcpSocket>
#include <QDebug>
#include "DataSetProtos.pb.h"
#define SIZE 500
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(qintptr ID, QObject *parent = 0);
void run();
signals:
void error(QTcpSocket::SocketError socketerror);
public slots:
void readyRead();
void readPacket( int bufferLength);
void disconnected();
void Print( Visualization::PacketData packet );
private:
QTcpSocket *socket;
qintptr socketDescriptor;
};
#endif // THREAD_H
最佳答案
正如 MrPickles 指出的那样,您让自己很难过。但是,您不需要一直更改数据格式。您只需要在不需要时停止尝试使用线程。 Qt 中几乎没有任何东西需要使用线程,除了繁重的数据处理或类似的事情。绝对不是基本的网络 I/O。
我有一个应用程序正在与消息长度前缀的 protobufs 通信。这是我的 onReadyRead 实现的要点,更改了一些细节并删除了不必要的内容:
void ProtobufMessageReceiver::onReadyRead()
{
//Some maximum size of a message to protect from malformed
//packets or bugs. This value should be above the size of the largest
//protobuf message you ever expect to receive.
static const uint32_t MAX_ALLOWED_SIZE = 1024 * 1024; //1MB
uint32_t msg_size;
while ( m_socket->bytesAvailable() >= sizeof(msg_size) )
{
m_socket->peek(reinterpret_cast<char*>(&msg_size), sizeof(msg_size));
//OK, we have read in a 32-bit message size, now verify that
//it is valid before we try to read that many bytes on the socket
if ( msg_size > MAX_ALLOWED_SIZE )
{
//You might throw an exception here or do some other error
//handling. Like most Qt code, though, this app doesn't
//use exceptions, so we handle a malformed packet by just
//closing the socket. In a real app you'd probably at least
//want to do some logging here, as this should be a very
//abnormal case and signifies a bug on the sending end.
m_socket->close();
break;
}
//Now check to see if we have that many bytes available to read in.
//If we do, we have at least one full message waiting to process, if not,
//we'll process the message on the next call to onReadyRead.
if ( m_socket->bytesAvailable() >= sizeof(msgSize) + msgSize )
{
m_socket->read(reinterpret_cast<char*>(&msg_size), sizeof(msg_size));
QScopedArrayPointer<char> buffer(new char[msgSize]);
m_socket->read(&buffer[0], msgSize);
processMessage(buffer, msgSize);
}
else
{
break;
}
}
}
processMessage 函数现在将始终处理一个单一的、完全有效的 protobuf 流,可以使用 parseFromArray 对其进行解码。 (在这个例子中,我们可能会使用一个 protobuf Union Type 在一个流中交错几种不同的类型)。请注意,因为不能保证 onReadyRead 会为每个 protobuf 消息调用一次,所以在其中设置 while 循环非常重要。无论您使用的是 Qt 还是使用套接字的其他方式,情况都是如此,因为 TCP 只是一个字节流,并不划定消息边界。毕竟,这就是我们甚至必须首先处理长度前缀的全部原因。
关于c++ - 如何在Qt中读取下一个数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31079605/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!