Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c++ - Error while using QTcpSocket

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

Here is my code:

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

");

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

}

The Problem -

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

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?

EDIT -

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

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

  2. (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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looked like the socket got closed

Rather than guessing, connect to the error signals of TCPServer and TCPSocket to know when errors occur, or when a client disconnects.

The problem you have is the while(1) loop. Qt is an event-driven framework, so having code in an infinite loop on the main thread is going to prevent events being delivered.

Instead of the infinite loop, connect to QTcpSocket::readyRead signal and handle the data when the connected slot is called.

Qt demonstrates this with the Fortune Server and Fortune Client example code.

If you're using C++ 11, you can use a connection to a lambda function to handle the readyRead, like this

void TcpServer::newConnection()
{
    ...

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

        // handle received data here

    });

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...