Results 1 to 4 of 4

Thread: wrong syntax or QTcpSocket problem?

  1. #1
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question wrong syntax or QTcpSocket problem?

    Hello, the first QT program i'm writing is a NNTP program to post binaries.
    Up to now I managed to connect with the server and to read its response to several commands I give (like 'POST' and 'HELP'). So far so good.
    Of course I first try to send a small message with only text in the body, and if this works I can focus on sending binaries.

    But I have a problem I don't understand ... when I try to send that message I need to do it as in this code:
    Qt Code:
    1. QString CRLF("\n");
    2. t.clear();
    3. t.append("From: " + userName + + CRLF);
    4. t.append("Newsgroups: " + (comboBox->currentText()) + CRLF);
    5. /// and so on with a few other commands more
    6.  
    7. tcpSocket->write(t.toAscii().data()); //this writes the data to the socket
    To copy to clipboard, switch view to plain text mode 

    With this code the server returns a '500', meaning it doesn't understand the commands.

    Now I found out that if I remove the 'CRLF' the newsserver doesn't complain but of course nothing is posted. And I know from an existing program the cr/lf must be there. Here's a snippet from that c-program I sometimes use, called 'newspost' (but this text based program is no longer maintained for Linux).

    Qt Code:
    1. buff = buff_add(buff, "From: %s\r\n", data->from->data);
    2. buff = buff_add(buff, "Newsgroups: %s\r\n", data->newsgroup->data);
    3. buff = buff_add(buff, "Subject: %s\r\n", subject);
    4. buff = buff_add(buff, "User-Agent: %s\r\n", USER_AGENT);
    5. // and so on ...
    To copy to clipboard, switch view to plain text mode 

    For the CRLF I tried "\r\n" too ... with the same result.
    Here's the routine to read the server, it works without problems.

    Qt Code:
    1. void myQtApp::readHost()
    2. {
    3. QMessageBox msgWarn;
    4. s.clear();
    5. t.clear();
    6. s=tcpSocket->readLine();
    7.  
    8. switch(s.left(3).toInt()){
    9. case 100:
    10. t.append("request for help granted");
    11. break;
    12. case 200:
    13. t.append("server ready to receive");
    14. break;
    15. case 211:
    16. t.append("newsgroup valid and selected");
    17. break;
    18. case 221:
    19. t.append("no data follows");
    20. break;
    21. case 340:
    22. postFiles();
    23. break;
    24. case 500:
    25. t.append("wrong command");
    26. break;
    27. case 501:
    28. t.append("wrong syntax after command");
    29. break;
    30. default:
    31. t.append(s.left(3));
    32. // temporarily messageBox for control
    33. msgWarn.warning(this, "server says", s.toAscii().data(), QMessageBox::Ok);
    34. break;
    35. }
    36.  
    37. label_status->setText(t);
    38. }
    To copy to clipboard, switch view to plain text mode 

    It puzzles me that the server doesn't accept a carriage return ... well, to be honest I do believe it's my code that is in error, not the server. Or is it something with the tcpSocket?
    I'm stuck

    All info welcome. Thanks in advance for your reply.
    Vito
    from Flanders/Belgium

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wrong syntax or QTcpSocket problem?

    Are you sure you're received bytes you need to readLine()?

    Where do you set the code you check?

    Qt Code:
    1. s.left(3).toInt()
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Re: wrong syntax or QTcpSocket problem?

    Quote Originally Posted by mcosta View Post
    Are you sure you're received bytes you need to readLine()?

    Where do you set the code you check?

    Qt Code:
    1. s.left(3).toInt()
    To copy to clipboard, switch view to plain text mode 
    Thanks for your reply.
    But I'm not sure I understand what you mean ...
    To read what the server says I use this signal/slot:
    Qt Code:
    1. connect(tcpSocket, SIGNAL(readyRead()),this,SLOT(readHost()));
    To copy to clipboard, switch view to plain text mode 

    Every time the server sends a message the readHost() function is called. Here's a piece of code:
    Qt Code:
    1. switch(s.left(3).toInt()){
    2. case 100:
    3. t.append("request for help granted");
    4. break;
    5. case 200:
    6. t.append("server ready to receive");
    7. break;
    To copy to clipboard, switch view to plain text mode 
    I only need the first 3 characters to determine the command it sends, so I strip everything from the message except the first 3 characters.

    When I send "From: Vito" nothing happens, that seems normal to me because the string is not terminated by a "\n", but when I add a cr/lf the server responds with a '500' meaning it doesn't understand the message sent.

    Maybe I don't use the right commands, that's possible, I got them from an older c-program and that worked fine a while ago on the same newsserver.
    Vito
    from Flanders/Belgium

  4. #4
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking Re: wrong syntax or QTcpSocket problem?

    Quote Originally Posted by mcosta View Post
    Are you sure you're received bytes you need to readLine()?

    Where do you set the code you check?

    Qt Code:
    1. s.left(3).toInt()
    To copy to clipboard, switch view to plain text mode 
    OK, there seems to be no problem with the socket but a misunderstanding of commands for the server. When I send "post\n" the newsserver doest complain, it sends a '340' followed by a suggested ID to use for my post. I now derived all commands from the server with 'help\n" command and they are ALL different from the ones I had. Strange but true.

    I have to dig into these new commands now
    Vito
    from Flanders/Belgium

Similar Threads

  1. Syntax highlight selection problem
    By s.toonen in forum Qt Programming
    Replies: 9
    Last Post: 4th June 2008, 12:15
  2. Syntax Highlighter sample is wrong
    By kib2 in forum Qt Programming
    Replies: 2
    Last Post: 8th November 2007, 21:24
  3. Problem with QTcpSocket in QThread
    By Raistlin in forum Qt Programming
    Replies: 8
    Last Post: 6th October 2007, 12:23
  4. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.