0

I’m trying to compare arrived data in TCP-Client with a QString. But I think, it’s a wring format for comparison. My TCP client is written in Qt and TCP server in C#. Here is example code of Qt my read-function:

void MyClient::slotReadyRead()
{
    QString okStr = "OK!";
    ui->textEdit->append(okStr);
    ui->textEdit->append("Reading...");
    QString str = socket->readAll();
    ui->textEdit->append(str);
    if(str == okStr)
        ui->textEdit->append("OK! is true");
    else
        ui->textEdit->append("OK! is false");
}

And here is C#-server code for writing commands “OK!”:

private void WriteOutput(Commands cmd)
{
    if (NS == null)
        return;

    string str = Enum.GetName(typeof(Commands), cmd);
    StreamWriter writer = new StreamWriter(NS);
    writer.WriteLine(str + "!");
    writer.Flush();
}

In this case cmd would be „OK“.In Qt-TCP Client I get following messages: OK! Reading... OK!

OK! is false But I would like to have a OK! is true :). Which data-format should I choose?

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Viktor Tarasov
  • 87
  • 1
  • 2
  • 10
  • If you are using a `!` to mark the end of an application-level message, then your read function has to keep reading until it reads a `!`. TCP won't do the application-level message boundaries for you -- you have to do it. (Or are you using lines to delimit application-level messages? We can't tell from your code.) Please read the last two paragraphs of [my answer](http://stackoverflow.com/a/11873691/721269) to [this](http://stackoverflow.com/questions/11873366/android-tcp-app-hanging-on-instream-readline) TCP question. – David Schwartz Nov 06 '12 at 18:17
  • I use '\n' to delimit application-level messages. Here is description of writeline-function: http://msdn.microsoft.com/en-au/library/system.io.streamwriter.writeline – Viktor Tarasov Nov 06 '12 at 18:38
  • Then why aren't you reading from the socket until you get a newline? – David Schwartz Nov 06 '12 at 18:44
  • But if I try to compare: ´QString okStr = "OK!\n"; ui->textEdit->append(okStr); ui->textEdit->append("Reading..."); QString str = socket->readAll();´ – Viktor Tarasov Nov 06 '12 at 18:45
  • 1
    If that works, it's only by luck. You have to read from the socket in a way that matches the way you wrote to it. If you write delimited by a newline, then you have to read until you get a newline. – David Schwartz Nov 06 '12 at 18:45
  • Also, avoid converting implicitly from bytearray (read/readAll() return QByteArrays) to unicode QStrings. Convert to QString as late as possible and use fromUtf8/fromAscii/fromLatin1 etc. explicitly, depending on the encoding used. – Frank Osterfeld Nov 06 '12 at 18:55
  • **@Frank Osterfeld** Yeah, I think, that is the problem. But in which format should I compare?? The arrived data are in QByteArray... – Viktor Tarasov Nov 06 '12 at 19:08
  • I've just read that readLine() and readAll () also return QString: QString QTextStream::readAll () QString QTextStream::readLine ( qint64 maxlen = 0 ) – Viktor Tarasov Nov 06 '12 at 19:16
  • @David Schwartz, thank you very much. I tried to read only a part of QString with command: **str.left(5);** and it works!! Here is my code: QString okStr = "OK!\n"; QString str = socket->readLine(); QString kurzstr = str.left(5); ui->textEdit->append(str); if(str == kurzstr) ui->textEdit->append("OK! is true"); else ui->textEdit->append("OK! is false"); – Viktor Tarasov Nov 06 '12 at 20:01
  • In my code was an error, here is my revision: if(**okStr** == kurzstr) – Viktor Tarasov Nov 06 '12 at 22:44

1 Answers1

0

I solved it. The problem was that in windows a new line is not '\n' but '\r\n'. Therefore if I compare my QString "OK!\n" on windows with arrived TCP-data "OK!\r\n" then I always get "false". One solution would be to compare my QString "OK!\r\n" with arrived TCP-data "OK!\r\n" or second solution would be to split only the first "OK!" part of the data with the Qt command QString kurzstr = str.section("\r\n", 0, 0); Here is the code:

QString okStr = "OK!";
QString str = socket->readLine();
QString kurzstr = str.section("\r\n", 0, 0);
    if(okStr == kurzstr)
        ui->textEdit->append("OK! is true");
    else
        ui->textEdit->append("OK! is false");
Viktor Tarasov
  • 87
  • 1
  • 2
  • 10