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. [SOLVED] Writing more than 4096 bytes on a socket in once
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Writing more than 4096 bytes on a socket in once

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    nando76
    wrote on last edited by
    #1

    Hi,
    i have the following problem.

    I have written a server process where a client process (also written by me) connects to.

    The server sends data to the client via QSslSocket which works fine as long as the data the sever sends does not exceeds 4096 bytes.

    The client @bytesAvailable@ slot is called and i always see that there are 4096 bytes available.
    But the server has sent much more.

    I understand that TCP can fragment data which is okay.

    My client side code looks like this:

    @
    void Sync::readyRead()
    {
    qDebug() << "Sync::readyRead: bytes available=" << m_tcpSocket->bytesAvailable();

    const   QByteArray data&(m_socket->readAll());
    

    }
    @

    readyRead() gets called more times (which is okay because of tcp fragmentation).

    My question is there a way (without implementing a protocol which holds the size information in header for example) to know on the client side that there is still data available?
    I can not predict how ofter readyRead get called.

    Hmm. i think i have to implement a protocol somehow....

    Or any other ideas?

    Greetings

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You can check with a while loop if there is more data to read in your readyRead function. To my understanding the problem is that the new readyRead signal is not issued when you are in the assigned slot routine.
      So any signal issued while reading will be ignored. With the while loop you will continue to read until there is no further data. When more data comes after you have exited the routine you should have another signal coming in anyway.

      Alternatively, you can call the readyRead slot externally with a delay or use a single shot timer.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nando76
        wrote on last edited by
        #3

        Hi,

        i tried it with a while loop like:

        @while(socket->bytesAvailable())
        {
        ... raed data....
        }@

        But it is not working.

        bytesAvailable gives back 0 and short time after byteAvailable is called again.

        I solved it now by adding a small protocol.

        Just the size in top of the message.

        I wrote to methods:

        @toNet()@
        which adds the <size> in front of the stream

        and

        @fromNet()@
        which removes the <size> from the beginning of the stream

        Then i have a small logic which waits until <size> bytes were read and then starts processing the message instead of direct start processing after first @socket->readAll()@ call in @bytesAvailable()@ slot

        This seems to work fine.

        Greetings

        1 Reply Last reply
        0

        • Login

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