使用QTcpSocket时出错

时间:2016-05-04 10:40:26

标签: c++ qt mjpeg qtcpsocket qtnetwork

我正在创建一个(非常基本的)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 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();
}

}

问题 -

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

QIODevice::write (QTcpSocket): device not open

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

编辑 -

我尝试了lambda方法,它工作正常。但是,我仍面临2个问题 -

  1. 图像尺寸必须过低(约为270x480,JPG质量最低)

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

    < / LI>

2 个答案:

答案 0 :(得分:2)

  

看起来插座已关闭

而不是猜测,连接到TCPServerTCPSocket的错误信号,以了解错误发生的时间,或客户端何时断开连接。

您遇到的问题是 while(1)循环。 Qt是一个event-driven框架,因此在主线程上的无限循环中使用代码将阻止事件的传递。

连接到QTcpSocket::readyRead信号而不是无限循环,并在调用连接的插槽时处理数据。

Qt使用Fortune ServerFortune Client示例代码演示了此内容。

如果您正在使用C++ 11,则可以使用与lambda函数的连接来处理readyRead,就像这样

void TcpServer::newConnection()
{
    ...

    QTcpSocket *m_TcpHttpClient = server->nextPendingConnection();
    connect(m_TcpHttpClient, &QTcpSocket::readyRead, [=](){

        // handle received data here

    });

}

答案 1 :(得分:1)

以下是我用于在my original Project中打开MJPEG Streaming Server的代码。也许它可以帮助你找到你的问题。

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
}