Results 1 to 4 of 4

Thread: Signal isn't emitted from socket

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Signal isn't emitted from socket

    Hi,

    Signal readyRead() isn't emitted from socket. Why? In this video it's working well: http://www.youtube.com/watch?v=j9uAfTAZrdM

    Output:
    Connecting...
    Connected!
    we wrote: 21
    Disconnected!
    SignalSocket.pro
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2013-08-14T13:28:42
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core
    8. QT += network
    9. QT -= gui
    10.  
    11. TARGET = SignalSocket
    12. CONFIG += console
    13. CONFIG -= app_bundle
    14.  
    15. TEMPLATE = app
    16.  
    17.  
    18. SOURCES += main.cpp \
    19. sockettest.cpp
    20.  
    21. HEADERS += \
    22. sockettest.h
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QCoreApplication>
    2. #include "sockettest.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. SocketTest mTest;
    9. mTest.Test();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    sockettest.h
    Qt Code:
    1. #ifndef SOCKETTEST_H
    2. #define SOCKETTEST_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <QTcpSocket>
    7. #include <QAbstractSocket>
    8.  
    9. class SocketTest : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit SocketTest(QObject *parent = 0);
    14. void Test();
    15.  
    16. signals:
    17.  
    18. public slots:
    19. void connected();
    20. void disconnected();
    21. void bytesWritten(qint64 bytes);
    22. void readyRead();
    23.  
    24. private:
    25. QTcpSocket *socket;
    26. };
    27.  
    28. #endif // SOCKETTEST_H
    To copy to clipboard, switch view to plain text mode 

    sockettest.cpp
    Qt Code:
    1. #include "sockettest.h"
    2.  
    3. SocketTest::SocketTest(QObject *parent) :
    4. QObject(parent)
    5. {
    6. }
    7.  
    8. void SocketTest::Test() {
    9. socket = new QTcpSocket(this);
    10.  
    11. connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    12. connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    13. connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
    14. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    15.  
    16. qDebug() << "Connecting...";
    17.  
    18. socket->connectToHost("google.com", 443);
    19.  
    20. if (!socket->waitForConnected(1000)) {
    21. qDebug() << "Error: " << socket->errorString();
    22. }
    23. }
    24.  
    25. void SocketTest::connected() {
    26. qDebug() << "Connected!";
    27.  
    28. socket->write("HEAD / HTTP/1.0\r\n\r\n\r\n");
    29. }
    30.  
    31. void SocketTest::disconnected() {
    32. qDebug() << "Disconnected!";
    33. }
    34.  
    35. void SocketTest::bytesWritten(qint64 bytes) {
    36. qDebug() << "we wrote: " << bytes;
    37. }
    38.  
    39. void SocketTest::readyRead() {
    40. qDebug() << "Reading...";
    41. qDebug() << socket->readAll();
    42. }
    To copy to clipboard, switch view to plain text mode 

    Thank you!

  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: Signal isn't emitted from socket

    Why would it emit readyRead()? HTTPS servers do not send any data upon connection. The SSL handshake is initiated by the client.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Signal isn't emitted from socket

    Thank you for reply!

    But in video it works!

  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: Signal isn't emitted from socket

    Apparently the video does something else than your code. Does your connected() slot ever get called?

    By the way, are you aware you are connecting to a service that expects an SSL handshake?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. loadFinished() signal not emitted
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 0
    Last Post: 27th June 2013, 12:21
  2. QTcpSocket error signal emitted twice
    By CactusPie in forum Qt Programming
    Replies: 2
    Last Post: 20th February 2013, 15:54
  3. QProgressDialog cancel signal is emitted always
    By Momergil in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2013, 02:24
  4. signal emitted when I zoom
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 8th July 2009, 17:02
  5. Signal emitted more than once?
    By dbrmik in forum Qt Programming
    Replies: 3
    Last Post: 13th March 2009, 12:44

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.