Yes, there are some differences between windows and linux with CR LF, it's "normal".
One approach that works nice is to make use of buffer and then wait for your data to be ready or timeout. For exmaple your seperator token can be “\r” and if you get an “\n” after just drop it.
Here is an example expecting a token from a custom protocol:
int Connection::readDataIntoBuffer(int maxSize)
{
if (maxSize > MaxBufferSize)
return 0;
int numBytesBeforeRead = buffer.size();
if (numBytesBeforeRead == MaxBufferSize) {
abort();
return 0;
}
while (bytesAvailable() > 0 && buffer.size() < maxSize) {
buffer.append(read(1));
if (buffer.endsWith(SeparatorToken))
break;
}
return buffer.size() - numBytesBeforeRead;
}
See http://doc.qt.nokia.com/stable/network-network-chat-connection-cpp.html
Depending on what you need another suggestion is to try an stick to some standard protocol. Like this you can test with different type of clients.
If you want to stick to your custom protocol I suggest you write your own client and write proper test cases to collaborate with your server. Qt makes it easy and fast ;) Take a look at the network examples.
Edit:
You might consider readline()
instead of read()
on your QTcpSocket
which is an QIODevice
. It waits for a newline instead of read() (see the doc excerpt below). However this gives less control over when to end your line:
qint64 QIODevice::readLine ( char * data, qint64 maxSize )
From the doc:
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.
The secret ingredient in Qt is asynchronous signal driven-design. See the section Networking / State Machines section in the article Threads, Events and QObjects for some ideas.