I am creating a simple TCP server, and have a slot/function built which reads incoming text from a client (telnet connection) on a TCP socket. I've used the Fortune code examples to help me, but had to remove QDataStream since it didn't work as expected.
I want my readCommand function to collect incoming characters from a telnet client connection, and once it finds a newline or return to remove the typed command from the input buffer, remove /n and /r, add it to my string list (commandList), and then echo the commands (seperate function). Here's what I've got so far:
void MyServer::readCommand()
{
inBuffer += tcpSocketPtr->readAll();
// While newline is present, extract the command
int nextNewlinePos;
while ((nextNewlinePos = inBuffer.indexOf('\n')) > -1) {
commandList << inBuffer.left(nextNewlinePos);
inBuffer.remove(0,nextNewlinePos);
// Get rid of /n /r if present somehow
}
if (commandList.size() > 0)
{
echoCommand();
}
}
Before I start stripping /n and /r etc. manually, my gut is telling me there is a better way to do this. Is QTextStream the way to go? Can someone provide a simple(r) alternative to what I am trying to achieve?
You could simply use the readLine(...) variants:
qint64 QIODevice::readLine(char * data, qint64 maxSize)
QByteArray QIODevice::readLine(qint64 maxSize = 0)
Note, read line functionality would be invoked on your QTcpSocket instance, and not the QDataStream. See the documentation for the precise details here inline:
A terminating '\0' byte is always appended to data, so maxSize must be larger than 1. Data is read until either of the following conditions are met: The first '\n' character is read. maxSize - 1 bytes are read. The end of the device data is detected.
This way, you would need to work such an issue around, and the code could become a lot simpler.
One further note that may be helpful:
bool QIODevice::canReadLine() const [virtual]
Basically, this method would guarantee you do not try to run a line before that is available. This can come to handy in certain scenarios.