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
#-------------------------------------------------
#
# 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
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QCoreApplication>
#include "sockettest.h"
int main(int argc, char *argv[])
{
SocketTest mTest;
mTest.Test();
return a.exec();
}
#include <QCoreApplication>
#include "sockettest.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SocketTest mTest;
mTest.Test();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
sockettest.h
#ifndef SOCKETTEST_H
#define SOCKETTEST_H
#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QAbstractSocket>
{
Q_OBJECT
public:
explicit SocketTest
(QObject *parent
= 0);
void Test();
signals:
public slots:
void connected();
void disconnected();
void bytesWritten(qint64 bytes);
void readyRead();
private:
};
#endif // 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
To copy to clipboard, switch view to plain text mode
sockettest.cpp
#include "sockettest.h"
SocketTest
::SocketTest(QObject *parent
) :{
}
void SocketTest::Test() {
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
Thank you!
Bookmarks