使用QTcpSocket时出错

Error while using QTcpSocket

本文关键字:出错 QTcpSocket 使用      更新时间:2023-10-16

我正在创建一个(非常基本的)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方法,效果很好。然而,我仍然面临两个问题-

  1. 图像大小必须过低(约270x480,JPG质量最低)

  2. 更重要)我必须手动按下浏览器上的重新加载按钮才能重新加载图像,它不会自动从一个图像更改为另一个图像。

看起来插座好像关上了

与其猜测,不如连接到TCPServerTCPSocket的错误信号,以了解何时发生错误,或者客户端何时断开连接。

您遇到的问题是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
}