PDA

View Full Version : QTcpSocket and setReadBufferSize



OnyxDragun
27th May 2014, 01:56
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


int iSize = 2048;
fd = socket->socketDescriptor();
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &iSize, sizeof(iSize));

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


::readyRead() {
while(socket->bytesAvailable())
{
QByteArray data = socket->readAll();
emit processData(data);
}
}

Any suggestions I could look at?

anda_skoa
27th May 2014, 07:18
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,
_

OnyxDragun
27th May 2014, 17:36
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);


QTextCursor c = ui->txtOutput->textCursor();
c.movePosition(QTextCursor::End);
ui->txtOutput->setTextCursor(c);


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?

anda_skoa
27th May 2014, 17:54
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,
_

OnyxDragun
27th May 2014, 18:48
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. :)