Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qtcpsocket read large data from server

Qtcpsocket read large data from server

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PouryaTorabi
    wrote on last edited by PouryaTorabi
    #1

    Hi everyone

    I have a qtcpsocket client that I want it to receive a black and white png from a server. The server is written in another language and have checked it with another client and it works fine but the client written in qt, doesn't receive the current data size. I send around 53258 bytes but the client, each time receives different amount, from 4000 to 16000. Here is my qt code:

    socket = new QTcpSocket();
    socket->connectToHost(Address, Port.toUShort());
    if(socket->waitForConnected(3000))
    {
         qDebug()<< "connected";
         emit connected();
    }
    
    socket->write("Sendmethedata\n");
    socket->waitForBytesWritten(1000);
    socket->waitForReadyRead(3000);
    QByteArray document2 = socket->readAll();
    qDebug()<<document2.length();
    

    Any suggestions?

    JonBJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You should wait and collect the data in a loop (or better - use the async API). Qt sockets will report data ready for reading as soon as it gets a proper chunk of it - which is not necessarily the whole file that the server is sending. Then it will emit readyRead() again when next part of data is received etc. So you need to keep reading from the socket until the whole file is ready.

      BTW. If your server is a HTTP/ HTTPS server, you can also use QNetworkAccessManager to get the file more easily.

      (Z(:^

      P 1 Reply Last reply
      5
      • sierdzioS sierdzio

        You should wait and collect the data in a loop (or better - use the async API). Qt sockets will report data ready for reading as soon as it gets a proper chunk of it - which is not necessarily the whole file that the server is sending. Then it will emit readyRead() again when next part of data is received etc. So you need to keep reading from the socket until the whole file is ready.

        BTW. If your server is a HTTP/ HTTPS server, you can also use QNetworkAccessManager to get the file more easily.

        P Offline
        P Offline
        PouryaTorabi
        wrote on last edited by
        #3

        @sierdzio Can you give me an example please.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PouryaTorabi
          wrote on last edited by
          #4

          I tried to make 5 second sleep and it read the data correctly, but is there better way that writing a delay in the thread?

          P 1 Reply Last reply
          0
          • P PouryaTorabi

            I tried to make 5 second sleep and it read the data correctly, but is there better way that writing a delay in the thread?

            P Offline
            P Offline
            PouryaTorabi
            wrote on last edited by
            #5

            @PouryaTorabi I Change the delay to 1 MilliSecond and it still works (-:

            aha_1980A 1 Reply Last reply
            0
            • P PouryaTorabi

              @PouryaTorabi I Change the delay to 1 MilliSecond and it still works (-:

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @PouryaTorabi You should not use sleep or waitFor in the main thread. Use proper Signals&Slots, see for example here: https://doc.qt.io/Qt-5/qtnetwork-fortuneclient-example.html

              Regards

              Qt has to stay free or it will die.

              P 1 Reply Last reply
              4
              • aha_1980A aha_1980

                @PouryaTorabi You should not use sleep or waitFor in the main thread. Use proper Signals&Slots, see for example here: https://doc.qt.io/Qt-5/qtnetwork-fortuneclient-example.html

                Regards

                P Offline
                P Offline
                PouryaTorabi
                wrote on last edited by
                #7

                @aha_1980 As you know waitForReadyRead is a signal so I am using signal slot, but the problem is the waitForReadyRead as @sierdzio mentioned, will be emited when the first byte is available, so I need to give some time to my client to receive all data, besides my client is in another thread which is different from Main thread, so would there be any problem?

                aha_1980A 1 Reply Last reply
                0
                • P PouryaTorabi

                  @aha_1980 As you know waitForReadyRead is a signal so I am using signal slot, but the problem is the waitForReadyRead as @sierdzio mentioned, will be emited when the first byte is available, so I need to give some time to my client to receive all data, besides my client is in another thread which is different from Main thread, so would there be any problem?

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by aha_1980
                  #8

                  @PouryaTorabi said in Qtcpsocket read large data from server:

                  As you know waitForReadyRead is a signal so I am using signal slot

                  No, that is wrong. readyRead() is a signal. waitForReadyRead() is a blocking function and that causes your problems.

                  You should really take a look at the example I linked above.

                  Regards

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  4
                  • P PouryaTorabi

                    Hi everyone

                    I have a qtcpsocket client that I want it to receive a black and white png from a server. The server is written in another language and have checked it with another client and it works fine but the client written in qt, doesn't receive the current data size. I send around 53258 bytes but the client, each time receives different amount, from 4000 to 16000. Here is my qt code:

                    socket = new QTcpSocket();
                    socket->connectToHost(Address, Port.toUShort());
                    if(socket->waitForConnected(3000))
                    {
                         qDebug()<< "connected";
                         emit connected();
                    }
                    
                    socket->write("Sendmethedata\n");
                    socket->waitForBytesWritten(1000);
                    socket->waitForReadyRead(3000);
                    QByteArray document2 = socket->readAll();
                    qDebug()<<document2.length();
                    

                    Any suggestions?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @PouryaTorabi
                    Do what @sierdzio & @aha_1980 are telling you:

                    1. Get rid of any call to waitForReadyRead() (and also to waitForBytesWritten(), you don't need it). Do not put any sleep() in.

                    2. Hook to the readyRead() signal and read the bytes from the socket there, e.g. via https://doc.qt.io/qt-5/qiodevice.html#readAll. Each time that is called there will be some new bytes arrived, but do not assume it will be all the bytes you are waiting for. You have to keep reading & accumulating when bytes arrive till the desired number have arrived. You may find that https://doc.qt.io/qt-5/qdatastream.html#startTransaction etc. helps you with this.

                    3. When all the necessary data has been received, emit a signal or whatever to start acting on the data.

                    Since you will now by using asynchronous socket calls instead of synchronous ones, you do not need to have a separate thread to handle the socket I/O --- your Qt GUI will not be blocked anyway. I do not know whether you have good reason to be using a separate thread in any case, if you do you will probably want to send a signal from that thread to your main thread when all the data has arrived so that the main thread can then handle the data.

                    1 Reply Last reply
                    4

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved