Search code examples
windowsqt4telnetqtnetworkqtcpserver

QT QTcpServer telnet taking characters \r\n


I'm writing a QTcpServer. I used telnet.exe as a client for testing. Upon a new client connection my server sends a Hi! message to the client which is displayed - all is well and fine so far.

But when I type something in the telnet.exe window, a readyRead() is emitted for each character. I only want it to be sent after \r\n! What's the problem? Is it the nature of telnet.exe in Windows? Cause I've used telnet on my linux box and it only sends the string after \r\n, as expected.


Solution

  • Unfortunately, that's how the Windows telnet.exe client works and there's no way to change that.

    You must not rely on client-specific behavior like this when handling TCP streams. TCP does not guarantee message boundaries, but it does guarantee that, from your point of view, the data is delivered int he same order it was written by the client. You must take this into account when designing your protocol.

    You'll need to buffer incoming data and handle this at the application protocol level. Common solutions include defining a message terminator sequence (and a mechanism for escaping that sequence if it can appear inside the normal messages) - for example, \r\n could be the terminator sequence in this scenario -, or you can packetize sent data prefixing it with the follow-up message length, or you can use dedicated messaging libraries (such as ZeroMQ or ActiveMQ - but then you can't use Qt's networking, unfortunately), etc.