Results 1 to 5 of 5

Thread: QTcpSocket and setReadBufferSize

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket and setReadBufferSize

    I've been working with QTCPSocket and it only ever receives data up to 1024 bytes, but the data coming to me can be up to 1536 bytes.

    I've tried to increase the receive socket buffer using socket->setReadBufferSize(1536) but it still only receives 1024 at most:
    Bytes to read: 1024
    Bytes to read: 21

    When I set setReadBufferSize to 512 I do get
    Bytes to read: 512
    Bytes to read: 512
    Bytes to read: 21

    But I cannot set it higher than 1024. I've even tried to make use of setsockopt
    Qt Code:
    1. int iSize = 2048;
    2. fd = socket->socketDescriptor();
    3. setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &iSize, sizeof(iSize));
    To copy to clipboard, switch view to plain text mode 

    And nothing changes.

    The data I get does vary (a few bytes to what seems to be never more than 2048 bytes) and is essentially telnet data (MUD text game). Though, after 1024 the data is broken up a bit and my output to the screen is a bit broken up.

    My readRead() looks like
    Qt Code:
    1. ::readyRead() {
    2. while(socket->bytesAvailable())
    3. {
    4. QByteArray data = socket->readAll();
    5. emit processData(data);
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions I could look at?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket and setReadBufferSize

    I am not sure what the problem is, your output suggest that you get more than 1024 bytes, just not in one emit.

    Since a TCP connection is a stream of bytes it hardly matters in which chunk size the stream data is delivered.

    If you need to wait for a certain amount of data before you can act on it, write it to a buffer or put the QByteArray into a list until the amount of received data has reached your protocol's threshold.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket and setReadBufferSize

    This is a bit of the exchange of data via qDebug()

    Bytes to read: 54
    2 bytes written to socket
    Bytes to read: 91
    10 bytes written to socket
    Bytes to read: 954
    9 bytes written to socket
    Bytes to read: 56
    10 bytes written to socket
    Bytes to read: 932
    10 bytes written to socket
    Bytes to read: 1024
    Bytes to read: 26

    To me it looks like the receive buffer is 1024 bytes maximum, but since the data inbound is not of a particular size, adding it to an array/buffer and then waiting for a specific size to be filled doesn't work with this particular applciation.

    When data comes in from the socket, it's read directly to a QPlainTextEdit widget (via append) with the cursor position being updated. Though when the data comes on the socket and it's greater than 1024 (as the last two lines show) then the data is sort of split up funny. Perhaps it's an issue more with my cursor placing than with the packets that are streaming in?

    Below is the code that happens after I ui->txtOutput.appendPlainText(data);
    Qt Code:
    1. QTextCursor c = ui->txtOutput->textCursor();
    2. c.movePosition(QTextCursor::End);
    3. ui->txtOutput->setTextCursor(c);
    To copy to clipboard, switch view to plain text mode 

    Example of where the text get's broken up:

    246: Fir <999> May 18: Ranger meat tweak
    247: Angrybeard <37> May 26: Metlof/wolf resets
    248: Emeritus <26> May 26: last
    249->Emeritus <26> May 26: Xenophobe
    250: Sic <25> May 27: the
    mysterious shop

    The "mysterious shop" should be up at the same line as "May 27: the "

    Perhaps something else is happening that makes the text break funny?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket and setReadBufferSize

    Having worked on a MUD client myself years ago, I'd recommend thinking about a parser:-)

    MUDs tend to send not just data but escape sequences, etc and you will need to keep track of state.

    Cheers,
    _

  5. #5
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket and setReadBufferSize

    I currerently parse ASCII command codes and ANSI colours (ie supports ANSI colours)

    Though I'm thinking the issue might be QPlainTextEdit, as I've read that appending a QString to it, will also append the \n character which is possibly causing me grief.




    Added after 39 minutes:


    Figured it out.. it was the QPlainTextEdit and append/appendHtml

    I had to change them to insertPlainText/insertHtml and manipulate the cursor. The previous functions append a newline which was the cause of my grief.
    Last edited by OnyxDragun; 27th May 2014 at 18:48.

Similar Threads

  1. QTcpSocket Help
    By minotaurds in forum Qt Programming
    Replies: 0
    Last Post: 20th October 2011, 11:18
  2. Replies: 1
    Last Post: 8th March 2011, 06:27
  3. QTcpSocket
    By Fallen_ in forum Qt Programming
    Replies: 5
    Last Post: 26th August 2010, 05:32
  4. Need Help with QTcpSocket
    By jknotzke in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 13:55
  5. QTcpSocket help
    By tsd-charlie in forum Newbie
    Replies: 1
    Last Post: 6th August 2009, 19:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.