Results 1 to 4 of 4

Thread: Problem in printing the data return /read from QTcpsocket

  1. #1
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem in printing the data return /read from QTcpsocket

    Hi ,
    I trying the copy the data read form QtcpSocket to a text edit. I was able to connect to the destination machine , but facing problem in reading. I have just started using QT so please forgive my silly mistakes .

    Here is my code

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include "telnetWindow.h"
    3.  
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. telnetWindow client;
    9. client.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 


    telnetWindow.h

    Qt Code:
    1. #ifndef TELNETWINDOW_H
    2. #define TELNETWINDOW_H
    3.  
    4. //#include <QProcess>
    5. //#include <QtGui>
    6. #include <QtNetwork/QTcpSocket>
    7. #include <QtNetwork/QAbstractSocket>
    8.  
    9. #include "ui_telnet.h"
    10.  
    11.  
    12. class telnetWindow : public QWidget,private Ui::telnetWindow
    13. {
    14. Q_OBJECT
    15. public :
    16. telnetWindow(QWidget *parent = 0);
    17. ~telnetWindow();
    18.  
    19. private slots:
    20. void read_data();
    21. void displayError(QAbstractSocket::SocketError socketError);
    22. void enableButton();
    23.  
    24. private:
    25.  
    26. QTcpSocket *tcpSocket;
    27. //QTcpSocket tcpSocket2;
    28. // QString currentFortune;
    29. // QString mystring;
    30. // quint16 blockSize;
    31.  
    32. //void startConsole();
    33. //void exitConsole();
    34.  
    35. };
    36. #endif
    To copy to clipboard, switch view to plain text mode 


    telnetWindow.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "telnetWindow.h"
    3. //#include<iostream.h>
    4. //use namespace std;
    5. telnetWindow::telnetWindow(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8. setupUi(this);
    9. tcpSocket = new QTcpSocket(this);
    10.  
    11. connect(lineEdit_ipAdd, SIGNAL(textChanged(const QString &)),
    12. this, SLOT(enableButton()));
    13. connect(lineEdit_portNo, SIGNAL(textChanged(const QString &)),
    14. this, SLOT(enableButton()));
    15. connect(pushButton_telnet, SIGNAL(clicked()),
    16. this, SLOT(read_data()));
    17. //connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    18. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(read_data()));
    19. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    20. this, SLOT(displayError(QAbstractSocket::SocketError)));
    21.  
    22. //textEdit->append("----------------Console Window-----------------------------");
    23.  
    24. lineEdit_portNo->setFocus();
    25. }
    26. telnetWindow::~telnetWindow()
    27. {};
    28.  
    29. void telnetWindow::read_data()
    30. {
    31. QByteArray consoleop;
    32. int ret_val;
    33. pushButton_telnet->setEnabled(true);
    34.  
    35. tcpSocket->abort();
    36. tcpSocket->connectToHost(lineEdit_ipAdd->text(),
    37. lineEdit_portNo->text().toInt(),QIODevice::ReadWrite);
    38.  
    39. //tcpSocket->write(data);
    40. if(tcpSocket->waitForConnected(-1))
    41. {
    42. //textEdit->append("Console conncted");
    43. //qdebug() << "Connected";
    44. ret_val = tcpSocket->state();
    45. // cout << ret_val;
    46. if(tcpSocket->waitForReadyRead())
    47. {
    48. tcpSocket->flush();
    49. //cout << tcpSocket->waitForReadyRead();
    50. //while(tcpSocket->canReadLine())
    51. // {
    52. //QString text;
    53. //mystring=tcpSocket->readLine();
    54. consoleop = tcpSocket->readAll();
    55. QString text = textEdit->toPlainText()+QString::fromLocal8Bit(consoleop);
    56. //QString text=tcpSocket->readAll();
    57. //textEdit->append(text);
    58. //cout<<text;
    59. textEdit->append(consoleop);
    60. label->setText(text);
    61. textEdit->insert(consoleop);
    62. label->setText(text);
    63. // qdebug() << text;
    64. // }
    65.  
    66. pushButton_telnet->setEnabled(false);
    67. }
    68. }
    69. }
    To copy to clipboard, switch view to plain text mode 


    I simply have a button , line edit and textedit.

    When I try to print the output of the

    textEdit->append(consoleop); -- Its printing some garbage value..

    I think I need to convert data to some type Or ?
    Plz help me solving this .....
    Last edited by wysota; 19th February 2009 at 21:45. Reason: missing [code] tags

  2. #2
    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: Problem in printing the data return /read from QTcpsocket

    It's hard to say without knowing how the data was sent through the socket but the code you pasted is incorrect. You are waiting for some data to be read from the socket and then you read it once and continue your work. By doing that you expect all data to be available in the socket whereas you might only be able to read one octet. You have to do the reading in a loop until you are sure all data was received. I'm also not sure why you flush the socket before reading.

  3. #3
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem in printing the data return /read from QTcpsocket

    Hi , i was able to resolve this issue ...I refered the Fortune server example . But now I am facing one more issue .

    Steps :-

    1.I am opening a connection using tcp socket .
    2. Wrinting one command on the console .
    3. reading the output when readReady is emited.

    The probelm I facing is that " What ever data I am reading its not in expected format",
    One line is getting divided and returned as 2 line.

    For example " My name is Sam " is divide in to line

    My
    name is Sam

    I need to know do I need to convert the string which i read form tcp socket .


    Modified code
    void telnetWindow::readConsole()
    {
    QDataStream in(tcpSocket);
    in.setVersion(QDataStream::Qt_4_0);
    QString test;
    if( tcpSocket->isSequential()){
    cout << "yes";
    }
    blockSize = tcpSocket->readBufferSize();
    QByteArray data = tcpSocket->readAll();
    textEdit->append(data);
    pushButton_telnet->setEnabled(true);
    }


  4. #4
    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: Problem in printing the data return /read from QTcpsocket

    You are aware of the fact that append() also adds a newline to the data it operates on, right? Also are you sure QDataStream does what you expect it to (you are not using it anywhere, by the way)? I ask again, what is the format of data sent by the remote side?

Similar Threads

  1. error with QList with a class
    By john_god in forum Newbie
    Replies: 7
    Last Post: 12th January 2009, 21:48
  2. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  3. 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.