使用QTcpSocket时出错

使用QTcpSocket时出错

本文介绍了使用QTcpSocket时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个(非常基本的) MJPG 服务器在浏览器上显示网络摄像头数据。

I am creating a (very basic) MJPG server to show webcam data on a browser. I have partly managed to do it so far.

这是我的代码:

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 OK\r\n" \
    "Cache-Control: no-cache\r\n" \
    "Cache-Control: private\r\n" \
    "Content-Type: multipart/x-mixed-replace;boundary=--boundary\r\n");
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 = ("--boundary\r\n" \
        "Content-Type: image/jpeg\r\n" \
        "Content-Length: ");
    BoundaryString.append(QString::number(CurrentImg.length()));
    BoundaryString.append("\r\n\r\n");

    socket->write(BoundaryString);
    socket->write(CurrentImg); // Write The Encoded Image
    socket->flush();
}

}

问题 -

当我运行此程序时,将显示第一个图像。之后,应用程序会连续打印以下错误 -

When I run this program, the first image is shown. After that, the following error is continuously printed on the app -

QIODevice::write (QTcpSocket): device not open

它看起来像套接字已关闭,所以我使用重新初始化套接字,像这样 - socket = server-> nextPendingConnection(); ,尽管应用程序使用此代码引发了错误。有关如何解决此问题的任何帮助?

It looked like the socket got closed, so I used re-initialized the socket, like this - socket = server->nextPendingConnection();, although the app threw an error with this code. Any help on how to fix this?

编辑 -

lambda方法和它工作正常。但我仍然面临2个问题 -

I tried the lambda method and it worked fine. However, I still face 2 problems -


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

  1. The image size has to be excessively low (around 270x480 with lowest JPG quality)

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

(MORE IMPORTANT) I have to manually press the reload button on browser to reload the image, it doesn't automatically change from one image to the other.


推荐答案

这里是代码我用来打开MJPEG流服务器在。也许它可以帮助你找出你的问题。

Here Is the Code I used to open the MJPEG Streaming Server in my original Project. Perhaps It can help you finding out your Problem.

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 OK\r\n" \
                              "Server: en.code-bude.net example server\r\n" \
                              "Cache-Control: no-cache\r\n" \
                              "Cache-Control: private\r\n" \
                              "Content-Type: multipart/x-mixed-replace;boundary=--boundary\r\n\r\n");

    m_TcpHttpClient->write(ContentType);


    while(1){

        if (m_TcpHttpClient->isOpen())
        {

            // Send Image

            QThread::msleep(10);

        }else{
            return;
        }

    }

    m_TcpHttpClient->disconnect(); //Should never be Reached
}

这篇关于使用QTcpSocket时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 12:20