這篇文章將為大家詳細(xì)講解有關(guān)Qt如何實(shí)現(xiàn)http服務(wù),小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
大化網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)于2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
先看執(zhí)行結(jié)果:
左邊是開啟的Qt Http服務(wù),監(jiān)控服務(wù)端口,及接收客戶端請求;右側(cè)是瀏覽器訪問服務(wù)。
下面是具體代碼:
HttpDemo.pro
QT += core
QT += network
QT -= gui
CONFIG += c++11 console
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
myhttpserver.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
myhttpserver.h
myhttpserver.h
#ifndef MYHTTPSERVER_H
#define MYHTTPSERVER_H
#include
#include
#include
class MyHttpServer : public QObject
{
Q_OBJECT
public:
explicit MyHttpServer(QObject *parent = nullptr);
~MyHttpServer();
QTcpSocket *socket;
public slots:
void connection();
private:
qint64 bytesAvailable() const;
QTcpServer *server;
signals:
public slots:
};
#endif // MYHTTPSERVER_H
main.cpp
#include
#include "myhttpserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyHttpServer server;
return a.exec();
}
myhttpserver.cpp
#include "myhttpserver.h"
#include
MyHttpServer::MyHttpServer(QObject *parent) : QObject(parent)
{
server = new QTcpServer(this);
connect(server, &QTcpServer::newConnection, this, &MyHttpServer::connection);
if(!server->listen(QHostAddress::Any, 8080))
{
qDebug() << "\nWeb服務(wù)未啟動";
}
else
{
qDebug() << "\nWeb服務(wù)在端口8080等待客戶端連接";
}
}
void MyHttpServer::connection()
{
socket = server->nextPendingConnection();
while (!(socket->waitForReadyRead(100))); //等待從瀏覽器讀取數(shù)據(jù)
char webBrowerRXData[1000];
int sv = socket->read(webBrowerRXData, 1000);
qDebug() << "正在從瀏覽器讀取數(shù)據(jù)=" << QString(webBrowerRXData);
socket->write("HTTP/1.1 200 OK\r\n"); // \r needs to be before
socket->write("Content-Type: text/html\r\n");
socket->write("Connection: close\r\n");
socket->write("Refresh: 1\r\n\r\n"); //refreshes web brower every second.Request
socket->write(""
""
""
""
"測試Qt HttpServer "
" "
"已連接秒數(shù):");
QByteArray str;
static qint16 count; //用于在瀏覽器上顯示的統(tǒng)計(jì)數(shù)字
str.setNum(count++);
socket->write(str);
socket->write(""
"");
socket->flush();
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
socket->disconnectFromHost();
}
MyHttpServer::~MyHttpServer()
{
socket->close();
}
關(guān)于“Qt如何實(shí)現(xiàn)http服務(wù)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。