Qt實(shí)現(xiàn)http服務(wù)的示例代碼
先看執(zhí)行結(jié)果:

Qt HttpServer
左邊是開啟的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.hmyhttpserver.h
#ifndef MYHTTPSERVER_H
#define MYHTTPSERVER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
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_Hmain.cpp
#include <QCoreApplication>
#include "myhttpserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyHttpServer server;
return a.exec();
}
myhttpserver.cpp
#include "myhttpserver.h"
#include <QDebug>
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 \n
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("<!DOCTYPE>"
"<html>"
"<header>"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
"<title>測試Qt HttpServer</title>"
"</header>"
"<body>已連接秒數(shù):");
QByteArray str;
static qint16 count; //用于在瀏覽器上顯示的統(tǒng)計(jì)數(shù)字
str.setNum(count++);
socket->write(str);
socket->write("</body>"
"</html>");
socket->flush();
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
socket->disconnectFromHost();
}
MyHttpServer::~MyHttpServer()
{
socket->close();
}到此這篇關(guān)于Qt實(shí)現(xiàn)http服務(wù)的示例代碼的文章就介紹到這了,更多相關(guān)Qt http服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++的QT項(xiàng)目打包成獨(dú)立可執(zhí)行和發(fā)布的exe文件(項(xiàng)目構(gòu)建過程)
這篇文章主要介紹了C++的QT項(xiàng)目打包成獨(dú)立可執(zhí)行和發(fā)布的exe文件(項(xiàng)目構(gòu)建過程),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
C語言實(shí)現(xiàn)手寫Map(全功能)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C語言實(shí)現(xiàn)手寫Map(全功能),文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下2022-08-08
如何基于 Blueprint 在游戲中創(chuàng)建實(shí)時(shí)音視頻功能
我們在本文先來講講如何在 Unreal 中用 Blueprint 快速實(shí)現(xiàn)。稍后會分享基于 C++的實(shí)現(xiàn)步驟。感興趣的朋友跟隨小編一起看看吧2020-05-05
C++基礎(chǔ)學(xué)習(xí)之利用兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列
這篇文章主要給大家介紹了關(guān)于C++基礎(chǔ)學(xué)習(xí)之利用兩個(gè)棧實(shí)現(xiàn)一個(gè)隊(duì)列的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

