PDA

View Full Version : Signal isn't emitted from socket



8Observer8
14th August 2013, 11:50
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


#-------------------------------------------------
#
# Project created by QtCreator 2013-08-14T13:28:42
#
#-------------------------------------------------

QT += core
QT += network
QT -= gui

TARGET = SignalSocket
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
sockettest.cpp

HEADERS += \
sockettest.h


main.cpp


#include <QCoreApplication>
#include "sockettest.h"

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

SocketTest mTest;
mTest.Test();

return a.exec();
}


sockettest.h


#ifndef SOCKETTEST_H
#define SOCKETTEST_H

#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QAbstractSocket>

class SocketTest : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = 0);
void Test();

signals:

public slots:
void connected();
void disconnected();
void bytesWritten(qint64 bytes);
void readyRead();

private:
QTcpSocket *socket;
};

#endif // SOCKETTEST_H


sockettest.cpp


#include "sockettest.h"

SocketTest::SocketTest(QObject *parent) :
QObject(parent)
{
}

void SocketTest::Test() {
socket = new QTcpSocket(this);

connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));

qDebug() << "Connecting...";

socket->connectToHost("google.com", 443);

if (!socket->waitForConnected(1000)) {
qDebug() << "Error: " << socket->errorString();
}
}

void SocketTest::connected() {
qDebug() << "Connected!";

socket->write("HEAD / HTTP/1.0\r\n\r\n\r\n");
}

void SocketTest::disconnected() {
qDebug() << "Disconnected!";
}

void SocketTest::bytesWritten(qint64 bytes) {
qDebug() << "we wrote: " << bytes;
}

void SocketTest::readyRead() {
qDebug() << "Reading...";
qDebug() << socket->readAll();
}


Thank you!

wysota
14th August 2013, 12:15
Why would it emit readyRead()? HTTPS servers do not send any data upon connection. The SSL handshake is initiated by the client.

8Observer8
14th August 2013, 12:51
Thank you for reply!

But in video it works!

wysota
14th August 2013, 13:33
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?