Error while using QTcpSocket

前端 未结 2 1295
暗喜
暗喜 2020-12-20 07:59

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:

TcpSe         


        
相关标签:
2条回答
  • 2020-12-20 08:45

    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
    }
    
    0 讨论(0)
  • 2020-12-20 08:48

    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
    
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题