#include <QtCore>
#include <QtNetwork>
#include <QDebug>
Q_OBJECT
public:
connect(&sock, SIGNAL(connected()), this, SLOT(connected()));
connect(&sock, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(&sock, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void run() {
sock.connectToHost("www.google.com", 80);
}
public slots:
void connected() {
sock.write(ba);
qDebug() << "Wrote:" << ba;
}
void disconnected() {
qDebug() << "Disconnected";
qApp->exit();
}
void readyRead() {
qDebug() << "Read:" << ba;
}
private:
};
int main(int argc, char *argv[])
{
Example e;
e.run();
return app.exec();
}
#include "main.moc"
#include <QtCore>
#include <QtNetwork>
#include <QDebug>
class Example: public QObject {
Q_OBJECT
public:
Example(QObject *p = 0): QObject(p) {
connect(&sock, SIGNAL(connected()), this, SLOT(connected()));
connect(&sock, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(&sock, SIGNAL(readyRead()), this, SLOT(readyRead()));
}
void run() {
sock.connectToHost("www.google.com", 80);
}
public slots:
void connected() {
QByteArray ba("GET / HTTP/1.0\r\n\r\n");
sock.write(ba);
qDebug() << "Wrote:" << ba;
}
void disconnected() {
qDebug() << "Disconnected";
qApp->exit();
}
void readyRead() {
QByteArray ba = sock.readAll();
qDebug() << "Read:" << ba;
}
private:
QTcpSocket sock;
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Example e;
e.run();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks