main.cpp
#include < QCoreApplication>#include " sockettest.h"int main(int argc, char *argv[]){QCoreApplication a(argc, argv);SocketTest cTest;cTest.Connect();
return a.exec();}
}
----------------------------------------------
socketTest.h
#ifndef SOCKETTEST_H#define SOCKETTEST_H#include < QObject>#include< QTcpSocket>#include< QDebug>class SocketTest : public QObject{Q_OBJECTpublic:
explicit SocketTest(QObject *parent = 0);void Connect();signals:
public slots:private:
QTcpSocket *socket;};#endif // SOCKETTEST_H
----------------------------------------------------
socketTest.cpp
#include " sockettest.h"SocketTest::SocketTest(QObject *parent) : QObject(parent){}void SocketTest::Connect(){socket = new QTcpSocket(this);socket->connectToHost("google.com",80);if(socket->waitForConnected(3000)){
qDebug() < <"Connected";socket->write("Hello World \r\n\r\n");socket->waitForBytesWritten(1000);socket->waitForReadyRead(3000);qDebug() < < "Reading : " << socket->bytesAvailable();qDebug() < < socket->readAll();socket->close();}
else{
qDebug() < <"Not Connected";}
}