Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTcpSocket Read Size - Is it possible to grab larger than 8192?



  • Hey All,

    Hope everyone is doing well! I am running into an issue that I can't get an answer to on Google. Hopefully, someone can help me and/or point me in the right direction......So, I have a TCP Client/Server deal going on. My server is sending data in larger amounts than the 8192 length that TCP read can handle.

    Code below is just something that I am using to grab the data from the server

    if(this->socket->waitForReadyRead(3000))
        {
            qint64 len = this->socket->bytesAvailable();
            char *data = new char[len];
            // len maxes out at 8192 here
            this->socket->read(data, len);
        }
    

    So, my question is, is there a way to increase the size of the socket read size (i.e double? triple) ? Because as of right now, I am sending in 11,416 but the socket read can only handle 8192.

    Or, is there a way to implement the read to be separated into multiple reads if the data exceeds 8192?
    So, something like this... I'm using pseudocode down here

    if(data->coming in is larger than 8192)
        then read the first 8192, pause, send remaining
    else
        send anything below 8192 regularly
    

    Thank you so much for taking the time to read this post!!!
    Derek


  • Lifetime Qt Champion

    Hi,

    You will likely be interested in QDataStream transactions to handle that kind of cases.



  • Hi @SGaist ,

    Thank you for your response! I apologize for mine being extremely late, I was out this previous weekend.

    So, I just finished reading what you proposed with the QDataStream and I am a little bit confused on how to implement this.

    qint64 len = this->socket->bytesAvailable();
    

    len, in this case, will always return 8192, regardless of the size (if it is larger than 8192). Would I need to use the peek function to see how much the server is sending and then implement the QDataStream?

    Also, how would I implement the read function with the QDataStream? Below is what I'm thinking of, and I'm 99% sure that it is not correct...

    if(this->socket->waitForReadyRead(1000)){
        qint64 len = this->socket->bytesAvailable();
        
        // I will try using peek here to get actual size instead of len
        if(len > 8192){
            QDataStream in(&file);
            
            // How do I implement the datastream here with socket read?        
        else{
            char *data = new char[len];
            this->socket->read(data, len);
        }
    

    Thanks so much!!
    Derek


  • Lifetime Qt Champion

    You can see an example of use in the Fortune Client example. The idea is that the read transaction does the handling of "missing data" for you.



  • Hi @SGaist ,

    Thanks!!! Alright, that was a big help!! I got it going!

    Thanks again,
    Derek


Log in to reply