使用QTcpSocket时出错
Error while using QTcpSocket
我正在创建一个(非常基本的)MJPG
服务器,以便在浏览器上显示网络摄像头数据。到目前为止,我已经部分做到了。
这是我的代码:
TcpServer::TcpServer(QObject *parent) :
QObject(parent)
{
server = new QTcpServer(this);
// whenever a user connects, it will emit signal
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
if (!server->listen(QHostAddress::Any, 9999))
qDebug() << "Server could not start";
else
qDebug() << "Server started!";
}
...
void TcpServer::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
QByteArray ContentType = ("HTTP/1.0 200 OKrn"
"Cache-Control: no-cachern"
"Cache-Control: privatern"
"Content-Type: multipart/x-mixed-replace;boundary=--boundaryrn");
socket->write(ContentType);
std::vector<uchar> buff;
Mat img; //OpenCV Material
while (1) {
// Image to Byte Array via OPENCV Method
buff.clear();buff.empty();
vCapture->read(img); //Read from webcam
imencode(".jpg", img, buff, compression_params);
std::string content(buff.begin(), buff.end());
QByteArray CurrentImg(QByteArray::fromStdString(content));
QByteArray BoundaryString = ("--boundaryrn"
"Content-Type: image/jpegrn"
"Content-Length: ");
BoundaryString.append(QString::number(CurrentImg.length()));
BoundaryString.append("rnrn");
socket->write(BoundaryString);
socket->write(CurrentImg); // Write The Encoded Image
socket->flush();
}
}
问题-
当我运行这个程序时,会显示第一个图像。之后,以下错误会持续打印在应用程序上-
QIODevice::write (QTcpSocket): device not open
看起来套接字关闭了,所以我使用了重新初始化套接字,就像这样-socket = server->nextPendingConnection();
,尽管应用程序在这段代码中抛出了一个错误。关于如何解决这个问题,有什么帮助吗?
编辑-
我尝试了lambda方法,效果很好。然而,我仍然面临两个问题-
图像大小必须过低(约270x480,JPG质量最低)
(更重要)我必须手动按下浏览器上的重新加载按钮才能重新加载图像,它不会自动从一个图像更改为另一个图像。
看起来插座好像关上了
与其猜测,不如连接到TCPServer
和TCPSocket
的错误信号,以了解何时发生错误,或者客户端何时断开连接。
您遇到的问题是while(1)循环。Qt是一个事件驱动的框架,因此让代码在主线程上的无限循环中会阻止事件的传递。
不使用无限循环,而是连接到QTcpSocket::readyRead信号,并在调用连接的插槽时处理数据。
Qt用Fortune Server和Fortune Client示例代码演示了这一点。
如果您使用的是C++11,您可以使用到lambda函数的连接来处理readyRead
,比如这个
void TcpServer::newConnection()
{
...
QTcpSocket *m_TcpHttpClient = server->nextPendingConnection();
connect(m_TcpHttpClient, &QTcpSocket::readyRead, [=](){
// handle received data here
});
}
这是我在最初的项目中用来打开MJPEG流媒体服务器的代码。也许它可以帮助你找出你的问题。
void MjpegStreamingEngine::StartServer(){
m_TcpHttpServer = new QTcpServer();
m_TcpHttpServer->connect(m_TcpHttpServer,
SIGNAL(newConnection()),
this,
SLOT(TcpHttpconnected()));
m_TcpHttpServer->listen(QHostAddress::Any,
8889);
}
void MjpegStreamingEngine::TcpHttpconnected(){
m_TcpHttpClient = m_TcpHttpServer->nextPendingConnection();
m_TcpHttpClient->connect(m_TcpHttpClient,
SIGNAL(readyRead()),
this,
SLOT(TcpHttpreadyRead()));
}
void MjpegStreamingEngine::TcpHttpreadyRead(){
m_TcpHttpClient->readAll(); // Discard "Get Request String"
QByteArray ContentType = ("HTTP/1.0 200 OKrn"
"Server: en.code-bude.net example serverrn"
"Cache-Control: no-cachern"
"Cache-Control: privatern"
"Content-Type: multipart/x-mixed-replace;boundary=--boundaryrnrn");
m_TcpHttpClient->write(ContentType);
while(1){
if (m_TcpHttpClient->isOpen())
{
// Send Image
QThread::msleep(10);
}else{
return;
}
}
m_TcpHttpClient->disconnect(); //Should never be Reached
}
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 为重写std::exception的库生成swig接口时出错
- 使用QTcpSocket时出错