Important: Please read the Qt Code of Conduct -

Server side does not emit ReadyRead signal

  • @kshegunov I have fixed the issue with the thread, but still the signal is not being emitted on the server side, I just don't understand what is wrong. For getting the video from the webcam I use OpenCV.

  • Lifetime Qt Champion

    @mandruk1331 Are there any other warnings when running the client or server?
    Also: before writing to socket you should either call or connect slot to signal and write in this slot to make sure you're actually connected at the time you're trying to write. And you should always connect slots to error signals.

  • Moderators

    Start by making sure you're sending data. Test your client by sending a few bytes to a server application that's known to work, e.g. the fortune server Qt example. Then test your server, for example open netcat and send a few bytes manually to your server, if you receive them, then the problem is most certainly with your client. Also make sure you're not using threading on the server, as QTcpServer::nextPendingConnection is not the correct way to do it.

  • if(connectedToHostC == false){
        connectedToHostC = true;

    Can you verify that the socket actually connects?

    qDebug() << socket->state();

  • @jsulm , @kshegunov @BjornW At the moment the photo is being transmitted to the server part. And when the frame is showed on the server part the lower part of the image is grey don't really know why and now when I want to scale it so it would fit the label size it gives these errors in the console:

    QImage::scaled: Image is a null image
    Img rece
    Corrupt JPEG data: premature end of data segment

  • Lifetime Qt Champion

    @mandruk1331 As the error message says your image is corrupted (most probably incomplete). You should check your networking code.
    How do you read the data on the server? Keep in mind that most probably the data does not arrive at once but in several chunks.

  • @jsulm On the server side I use the readAll function like this:
    QString str(socket->readAll());
    And how I can make the client side to wait for a msg from the server side. The question how I can make TCP/IP synchronous?
    If you want I can give a link to GitHub account.

  • Lifetime Qt Champion


    1. Why do you convert the data to QString?! You expect to get an JPEG image, right?
    2. Do you call readAll() only once? As I said the data arrives in several chunks and you need to read all of them. You connect readyRead signal to a slot and in this slot you call readAll() and store the data in a buffer until you have all the data.

    "And how I can make the client side to wait for a msg from the server side" - what do you mean? you send a picture from client to server, right?
    "The question how I can make TCP/IP synchronous?" - you don't. Why would you?

  • @jsulm From the server side I expect a QString "1" img passed the validation "0" !passed. I call it only once. How I can determine that the buffer has all the data that can be used?
    Client->JPEG->Server (checks if img is ok)->send msg that img is ok->client rcv that img is ok.

  • Lifetime Qt Champion

    @mandruk1331 "From the server side I expect a QString "1" img passed the validation "0" !passed" - actually I wanted to know how you read on server.
    You need to define a protocol. For example the client could send first an int containing the number of bytes it is going to send next. Then the server knows when all data was received.

  • @jsulm At the moment I can send a videostream over the socket to the server side using TCP/IP protocol and I was a little bit surprised because it works really really good, the frame rate on the server side is the same as on the client side. Now I will try to send the validation string from the server side to the client. A big thank you for everybody who gave me the advice on how to fix my problem and a special thank you to JSULM.

  • Lifetime Qt Champion

    @mandruk1331 Glad I could help :-)

  • @jsulm One more question,. At the moment the frame are being transmitted between client and server, but on the server side I get a corrupted image (lower part of it is grey), but if the image size is less then 9k bytes than everything is ok, how I can make the image to be transmitted fully?
    Thank you.

  • Lifetime Qt Champion

    @mandruk1331 I explained it above: you have to make sure you read the whole picture on the server side. Data is transmitted in packages over the network - you don't know how many packages you will get, so you need to read until you got everything.
    One question: how big are the broken image on the server side? Do they have exact same size as on client side?

  • @jsulm So I can write to the socket the size of the image therefore the server will know the size of the picture, and after that send the image?

  • Lifetime Qt Champion

  • @jsulm Ok. Got it.

  • @jsulm so this if what I got so far
    The client side send the size but the server does not response that he received the data, the string is "" but it has to be "2".
    Could you tell me what am I doing wrong?
    Server side:

    void TCPSocket::newImageReceived()
        qDebug()<<"Img rece";
        QByteArray ba;
        if(socket->bytesAvailable() <=0){
            qDebug()<<"Cannot read from socket"<<socket->errorString();
              QString size(socket->readAll());
              size_data = size.toInt();
        while(size_data >0){
            ba += socket->readAll();
        QImage img = QImage::fromData(ba);
        qDebug()<<"Frame ok";
            qDebug()<<"Frame not ok";
        emit sendImage(img); // to the GUI side not over the network

    Client side:

    void _Camera::SendData(QImage &frame){
        QByteArray ba;
        QBuffer buffer(&ba);;, "JPEG");
        //ba.append("We did it");
        if(connectedToHostC == false){
        connectedToHostC = true;
            qDebug()<<"Socket is valid";
        qDebug()<<"Data written"<<socket.write(QString::number(buffer.size()).toStdString().c_str());
        qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000);
        while(socket.bytesAvailable() <0){
        qDebug()<<"no data to be read";
        QString response (socket.readAll());
        qDebug()<<"response is"<<response;
        if(response == "2"){
            qDebug()<<"Data written"<<socket.write(;
            qDebug()<<"Bytes written"<<socket.waitForBytesWritten(1000);
            qDebug()<<"Bytes available"<<socket.bytesAvailable();
            qDebug()<<"smth is wrong";

  • Lifetime Qt Champion

    @mandruk1331 No need to send anything to the client after reading size. Also, you're reading size wrongly: size should have a fixed size (4 bytes should be enough), so you need to read exactly 4 bytes not a string which can have arbitrary size.
    Then, remove this while loop where you read the picture data - this should be done in the usual way using readyRead signal. You really should take a look at the networking Qt examples.

  • @jsulm Ok, I will take a look at the examples and the docs.

Log in to reply