Results 1 to 11 of 11

Thread: readLine problem

  1. #1
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Question readLine problem

    Hi,

    I have a problem with the readLine function. It doesn't work while the computer is connected to the server. I have checked that the computer is connected with the telnet command. I have modified the Fortune Client example, removed the readFortune-function and added a readLine() in the requestNewFortune function. The last function is renamed to read_data(). I have also added a waitforConnected if test and a canReadLine if test before the readLine() and after the connectToHost. The canReadLine fails for some reason. I have also tried to remove the canReadLine but the readLine doesn't read anything (the output of mystring is empty). The program consists of three files: client.cpp, client.h, main.cpp

    Any suggestions appreciated, thanks!

    Regards
    gQt


    client.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include "client.h"
    4. #include <iostream.h>
    5. #include <QtCore>
    6. #include <qdebug.h>
    7. Client::Client(QWidget *parent)
    8. : QDialog(parent)
    9. {
    10. hostLabel = new QLabel(tr("&Server name:"));
    11. portLabel = new QLabel(tr("S&erver port:"));
    12. hostLineEdit = new QLineEdit("10.249.0.48");
    13. portLineEdit = new QLineEdit("4007");
    14. portLineEdit->setValidator(new QIntValidator(1, 65535, this));
    15.  
    16. hostLabel->setBuddy(hostLineEdit);
    17. portLabel->setBuddy(portLineEdit);
    18. statusLabel = new QLabel(tr("This examples requires that you run the "
    19. "Fortune Server example as well."));
    20.  
    21. getFortuneButton = new QPushButton(tr("Get Fortune"));
    22. getFortuneButton->setDefault(true);
    23. getFortuneButton->setEnabled(false);
    24.  
    25. quitButton = new QPushButton(tr("Quit"));
    26.  
    27. tcpSocket = new QTcpSocket(this);
    28.  
    29. connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
    30. this, SLOT(enableGetFortuneButton()));
    31. connect(portLineEdit, SIGNAL(textChanged(const QString &)),
    32. this, SLOT(enableGetFortuneButton()));
    33. connect(getFortuneButton, SIGNAL(clicked()),
    34. this, SLOT(read_data()));
    35. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    36. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read_data()));
    37. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    38.  
    39. QHBoxLayout *buttonLayout = new QHBoxLayout;
    40. buttonLayout->addStretch(1);
    41. buttonLayout->addWidget(getFortuneButton);
    42. buttonLayout->addWidget(quitButton);
    43.  
    44. QGridLayout *mainLayout = new QGridLayout;
    45. mainLayout->addWidget(hostLabel, 0, 0);
    46. mainLayout->addWidget(hostLineEdit, 0, 1);
    47. mainLayout->addWidget(portLabel, 1, 0);
    48. mainLayout->addWidget(portLineEdit, 1, 1);
    49. mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
    50. mainLayout->addLayout(buttonLayout, 3, 0, 1, 2);
    51. setLayout(mainLayout);
    52.  
    53. setWindowTitle(tr("Fortune Client"));
    54. portLineEdit->setFocus();
    55. }
    56.  
    57. void Client::read_data()
    58. {
    59. getFortuneButton->setEnabled(true);
    60. tcpSocket->abort();
    61. tcpSocket->connectToHost(hostLineEdit->text(),
    62. portLineEdit->text().toInt());
    63.  
    64. cout << ("før if(canReadLine)\n");
    65.  
    66. if(tcpSocket->waitForConnected(-1))
    67. {
    68. cout <<"wait for connected loop";
    69. if(tcpSocket->canReadLine())
    70. {
    71. tcpSocket->readLine();
    72. qDebug() << "READ" << mystring;
    73. cout <<"canReadLine loop";
    74. }
    75. statusLabel->setText(mystring);
    76. getFortuneButton->setEnabled(false);
    77. }
    78.  
    79. }
    80. void Client::displayError(QAbstractSocket::SocketError socketError)
    81. {
    82. switch (socketError) {
    83. case QAbstractSocket::RemoteHostClosedError:
    84. break;
    85. case QAbstractSocket::HostNotFoundError:
    86. QMessageBox::information(this, tr("Fortune Client"),
    87. tr("The host was not found. Please check the "
    88. "host name and port settings."));
    89. break;
    90. case QAbstractSocket::ConnectionRefusedError:
    91. QMessageBox::information(this, tr("Fortune Client"),
    92. tr("The connection was refused by the peer. "
    93. "Make sure the fortune server is running, "
    94. "and check that the host name and port "
    95. "settings are correct."));
    96. break;
    97. default:
    98. QMessageBox::information(this, tr("Fortune Client"),
    99. tr("The following error occurred: %1.")
    100. .arg(tcpSocket->errorString()));
    101. }
    102.  
    103. getFortuneButton->setEnabled(true);
    104. }
    105.  
    106. void Client::enableGetFortuneButton()
    107. {
    108. getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
    109. && !portLineEdit->text().isEmpty());
    110. }
    To copy to clipboard, switch view to plain text mode 

    client.h
    Qt Code:
    1. #ifndef CLIENT_H
    2. #define CLIENT_H
    3. #include <QDialog>
    4. #include <QTcpSocket>
    5. #include <QAbstractSocket>
    6.  
    7. class QLabel;
    8. class QLineEdit;
    9. class QPushButton;
    10. class QTcpSocket;
    11. class Client : public QDialog
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. Client(QWidget *parent = 0);
    17.  
    18. private slots:
    19. void read_data();
    20. void displayError(QAbstractSocket::SocketError socketError);
    21. void enableGetFortuneButton();
    22.  
    23. private:
    24. QLabel *hostLabel;
    25. QLabel *portLabel;
    26. QLabel *cLabel;
    27. QLineEdit *hostLineEdit;
    28. QLineEdit *portLineEdit;
    29. QLabel *statusLabel;
    30. QPushButton *getFortuneButton;
    31. QPushButton *quitButton;
    32. QDialogButtonBox *buttonBox;
    33.  
    34. QTcpSocket *tcpSocket;
    35. QTcpSocket tcpSocket2;
    36. QString currentFortune;
    37. QString mystring;
    38. quint16 blockSize;
    39.  
    40. };
    41.  
    42. #endif
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "client.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. Client client;
    8. client.show();
    9. return client.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 26th February 2008 at 12:58. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Default Re: readLine problem

    I forgot to say that the data I 'm trying to read with readLine looks like this:

    $IIMWV,063,R,001.16,M,A*13
    $IIMWV,042,R,002.66,M,A*14
    $IIMWV,033,R,002.67,M,A*13
    $IIMWV,041,R,001.71,M,A*12

    with one new line available pr. second, so the problem can't be that readLine can't find a \n in the data. My problem is that the client connects, but readLine can't read anything.

    Any help is deeply apreciated, thanks in advance!

    Regards
    gQt

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: readLine problem

    The fact that there is a connection between two hosts doesn't yet mean there is anything to read and even if there is, it might not be terminated with a newline character(s).

  4. #4
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Default Re: readLine problem

    Thanks for your reply!

    I have tested that the data is available with the telnet command. The data comes with both LF and CR so canReadline /readLine should work. Output to terminal looks like this (changed all of the couts to english now)

    function read_data()
    after connectToHost
    wait for connected loop
    function read_data()
    after connectToHost
    wait for connected loop
    function read_data()
    after connectToHost
    wait for connected loop
    <Terminated with the Quit button>

    <client.cpp>

    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include "client.h"
    4. #include <iostream.h>
    5. #include <QtCore>
    6. #include <qdebug.h>
    7. Client::Client(QWidget *parent)
    8. : QDialog(parent)
    9. {
    10. hostLabel = new QLabel(tr("&Server name:"));
    11. portLabel = new QLabel(tr("S&erver port:"));
    12. hostLineEdit = new QLineEdit("10.249.0.48");
    13. portLineEdit = new QLineEdit("4002");
    14. portLineEdit->setValidator(new QIntValidator(1, 65535, this));
    15.  
    16. hostLabel->setBuddy(hostLineEdit);
    17. portLabel->setBuddy(portLineEdit);
    18. statusLabel = new QLabel(tr("This examples requires that you run the "
    19. "Fortune Server example as well."));
    20.  
    21. getFortuneButton = new QPushButton(tr("Get Fortune"));
    22. getFortuneButton->setDefault(true);
    23. getFortuneButton->setEnabled(false);
    24.  
    25. quitButton = new QPushButton(tr("Quit"));
    26.  
    27. tcpSocket = new QTcpSocket(this);
    28.  
    29. connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
    30. this, SLOT(enableGetFortuneButton()));
    31. connect(portLineEdit, SIGNAL(textChanged(const QString &)),
    32. this, SLOT(enableGetFortuneButton()));
    33. connect(getFortuneButton, SIGNAL(clicked()),
    34. this, SLOT(read_data()));
    35. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    36. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read_data()));
    37. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    38.  
    39. QHBoxLayout *buttonLayout = new QHBoxLayout;
    40. buttonLayout->addStretch(1);
    41. buttonLayout->addWidget(getFortuneButton);
    42. buttonLayout->addWidget(quitButton);
    43.  
    44. QGridLayout *mainLayout = new QGridLayout;
    45. mainLayout->addWidget(hostLabel, 0, 0);
    46. mainLayout->addWidget(hostLineEdit, 0, 1);
    47. mainLayout->addWidget(portLabel, 1, 0);
    48. mainLayout->addWidget(portLineEdit, 1, 1);
    49. mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
    50. mainLayout->addLayout(buttonLayout, 3, 0, 1, 2);
    51. setLayout(mainLayout);
    52.  
    53. setWindowTitle(tr("Fortune Client"));
    54. portLineEdit->setFocus();
    55. }
    56.  
    57. void Client::read_data()
    58. {
    59. getFortuneButton->setEnabled(true);
    60. cout << "function read_data()\n";
    61. tcpSocket->abort();
    62. tcpSocket->connectToHost(hostLineEdit->text(),
    63. portLineEdit->text().toInt());
    64.  
    65. cout << ("after connectToHost\n");
    66.  
    67. if(tcpSocket->waitForConnected(-1))
    68. {
    69. cout <<"wait for connected loop\n";
    70. if(tcpSocket->canReadLine())
    71. {
    72. mystring=tcpSocket->readLine();
    73. qDebug() << "READ" << mystring;
    74. cout <<"canReadLine loop after readLine\n";
    75. }
    76. statusLabel->setText(mystring);
    77. getFortuneButton->setEnabled(false);
    78. }
    79.  
    80. }
    81. void Client::displayError(QAbstractSocket::SocketError socketError)
    82. {
    83. switch (socketError) {
    84. case QAbstractSocket::RemoteHostClosedError:
    85. break;
    86. case QAbstractSocket::HostNotFoundError:
    87. QMessageBox::information(this, tr("Fortune Client"),
    88. tr("The host was not found. Please check the "
    89. "host name and port settings."));
    90. break;
    91. case QAbstractSocket::ConnectionRefusedError:
    92. QMessageBox::information(this, tr("Fortune Client"),
    93. tr("The connection was refused by the peer. "
    94. "Make sure the fortune server is running, "
    95. "and check that the host name and port "
    96. "settings are correct."));
    97. break;
    98. default:
    99. QMessageBox::information(this, tr("Fortune Client"),
    100. tr("The following error occurred: %1.")
    101. .arg(tcpSocket->errorString()));
    102. }
    103.  
    104. getFortuneButton->setEnabled(true);
    105. }
    106.  
    107. void Client::enableGetFortuneButton()
    108. {
    109. getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
    110. && !portLineEdit->text().isEmpty());
    111. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th February 2008 at 12:21. Reason: missing [code] tags

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: readLine problem

    Quote Originally Posted by gQt View Post
    Thanks for your reply!

    I have tested that the data is available with the telnet command. The data comes with both LF and CR so canReadline /readLine should work. Output to terminal looks like this (changed all of the couts to english now)
    Does the output come to the terminal within a 1ns after establishing the connection? I doubt that... The data arrives, but it arrives later - you can't connect and expect the data to be already there.

  6. #6
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Default Re: readLine problem

    I'm doing an if (waitForConnected(-1)) after the connectToHost if that wat you mean?

    Should I add anything else here?
    (I'm new to Qt and Socket-programming)

    From the read_data() function:

    Qt Code:
    1. tcpSocket->abort();
    2. tcpSocket->connectToHost(hostLineEdit->text(),
    3. portLineEdit->text().toInt());
    4.  
    5. cout << ("after connectToHost\n");
    6.  
    7. if(tcpSocket->waitForConnected(-1))
    8. {
    9. cout <<"wait for connected loop\n";
    10. if(tcpSocket->canReadLine())
    11. {
    12. mystring=tcpSocket->readLine();
    13. qDebug() << "READ" << mystring;
    14. cout <<"canReadLine loop after readLine\n";
    15. }
    16. statusLabel->setText(mystring);
    17. getFortuneButton->setEnabled(false);
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 27th February 2008 at 13:13. Reason: missing [code] tags

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: readLine problem

    Quote Originally Posted by gQt View Post
    I'm doing an if (waitForConnected(-1)) after the connectToHost if that wat you mean?
    No, this waits for the connection to be established, not for the data to be ready.

    Should I add anything else here?
    QAbstractSocket::waitForReadyRead
    (I'm new to Qt and Socket-programming)
    That should motivate you to do some more reading.

  8. #8
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Default Re: readLine problem

    Thanks a mill wysota !
    I hadn't thought about the waitForReadyRead- that I had to wait for the data to be ready to read also.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: readLine problem

    Be aware that the fact that waitForReadyRead returns means there is something to read, not that everything is ready to be read.

  10. The following user says thank you to wysota for this useful post:

    gQt (29th February 2008)

  11. #10
    Join Date
    Feb 2008
    Posts
    36
    Thanks
    4

    Default Re: readLine problem

    sorry for postin the start of the thread again,

    I have added a while waitForReadyRead loop inside the if waitForConnected and now it works.


    Qt Code:
    1. void Client::read_data()
    2. {
    3. getFortuneButton->setEnabled(true);
    4. //cout << "function read_data()\n";
    5. tcpSocket->abort();
    6. tcpSocket->connectToHost(hostLineEdit->text(),
    7. portLineEdit->text().toInt());
    8.  
    9. //cout << ("after connectToHost\n");
    10.  
    11. if(tcpSocket->waitForConnected(-1))
    12. {
    13. //cout <<"wait for connected loop\n";
    14.  
    15. while(tcpSocket->waitForReadyRead(-1))
    16. {
    17. while(tcpSocket->canReadLine())
    18. {
    19. mystring=tcpSocket->readLine();
    20. qDebug() << mystring;
    21. statusLabel->setText(mystring);
    22. }
    23. getFortuneButton->setEnabled(false);
    24. }
    25.  
    26.  
    27. }
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 2nd March 2008 at 09:17. Reason: missing [code] tags

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: readLine problem

    I still don't see you use waitForReadyRead...

    Qt Code:
    1. x->connectToHost(...);
    2. x->waitForConnected(...);
    3. while(x->waitForReadyRead(10000)){
    4. x->readAll(); // or canReadLine && readLine...
    5. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. problem with opengl, zooming, drawpixels, and origin
    By ntp in forum General Programming
    Replies: 0
    Last Post: 22nd February 2008, 21:48
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  3. QTextStream, input and readLine()
    By kramed in forum Newbie
    Replies: 6
    Last Post: 1st September 2007, 23:54
  4. problem with reading input data in qt
    By Ahmad in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2007, 10:58
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.