Results 1 to 4 of 4

Thread: GET Network program

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default GET Network program

    I'm curious, why doesn't this program work:

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



    MAINWINDOW.H
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. /**
    5. @author eleanor <eleanor@localhost>
    6. */
    7.  
    8. #include <QWidget>
    9. #include <QString>
    10. #include <QTextStream>
    11. #include <QTcpSocket>
    12. #include <QAbstractSocket>
    13. #include <QLabel>
    14.  
    15. class CMainWindow : public QWidget {
    16. public:
    17. CMainWindow(QWidget *parent=0);
    18.  
    19. ~CMainWindow();
    20.  
    21. private:
    22. QTcpSocket *m_pSocket;
    23. };
    24.  
    25. #endif
    To copy to clipboard, switch view to plain text mode 



    MAINWINDOW.CPP
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. CMainWindow::CMainWindow(QWidget *parent) : QWidget(parent) {
    4. //creating a socket
    5. m_pSocket = new QTcpSocket(this);
    6.  
    7.  
    8. QString myHost("www.google.com");
    9. int myPort = 80;
    10.  
    11. //this function establishes connection and returns immediately
    12. m_pSocket->connectToHost(myHost, myPort);
    13.  
    14. QString input = "GET /index.php HTTP/1.1\n\n";
    15. if(m_pSocket->state() == QAbstractSocket::ConnectedState) {
    16. QTextStream stream(m_pSocket);
    17. stream << input;
    18. }
    19.  
    20. QString text;
    21. while(m_pSocket->canReadLine()) {
    22. text += m_pSocket->readLine();
    23. }
    24.  
    25. QTextStream output(stdout);
    26. QString enteredCommand = text;
    27. output >> text;
    28. }
    29.  
    30.  
    31. CMainWindow::~CMainWindow() {
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    I would appreciate any tips. Thanks

  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: GET Network program

    Because the socket works asynchronously to the application - it doesn't block when you write or read from it. You need to react on signals or use QHttp instead.

  3. #3
    Join Date
    Nov 2006
    Posts
    96

    Default Re: GET Network program

    Ok, I'm trying this way:

    Qt Code:
    1. QString myHost("www.google.com");
    2. int myPort = 80;
    3.  
    4. //this function establishes connection and returns immediately
    5. m_pSocket->connectToHost(myHost, myPort);
    6. connect(m_pSocket, SIGNAL(connected()), this, SLOT(connectedSlot()));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void CMainWindow::connectedSlot() {
    2. QString input = "GET /index.php HTTP/1.1\n\n";
    3. if(m_pSocket->state() == QAbstractSocket::ConnectedState) {
    4. QTextStream stream(m_pSocket);
    5. stream << input;
    6. }
    7.  
    8. QString text;
    9. while(m_pSocket->canReadLine()) {
    10. text += m_pSocket->readLine();
    11. }
    12.  
    13. QTextStream output(stdout);
    14. QString enteredCommand = text;
    15. output >> text;
    16. }
    To copy to clipboard, switch view to plain text mode 

    I'm calling this slot when the connecter() signal occurs, still does not work!

  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: GET Network program

    Because m_pSocket->canReadLine() will return false and break out of the loop immediately - there is no data in the socket yet. You need to make your flow event driven - for example connect to the socket's readyRead() signal - it is emited when some data becomes available in the socket.

Similar Threads

  1. Replies: 5
    Last Post: 29th October 2007, 22:49
  2. Version setting in QT Program
    By sabeesh in forum Qt Programming
    Replies: 4
    Last Post: 24th October 2007, 12:07
  3. In what order do you create program pieces?
    By Backslash in forum Newbie
    Replies: 2
    Last Post: 15th July 2007, 18:18
  4. QT Program debug,GDB on Linux
    By darpan in forum General Programming
    Replies: 1
    Last Post: 26th January 2007, 22:02
  5. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

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.