How to implement http Service with Qt
This article will explain in detail how Qt implements http services. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
First, let's look at the implementation result:
Qt HttpServer
On the left is the open Qt Http service, monitoring the service port, and receiving client requests; on the right is the browser access service.
Here is the specific code:
HttpDemo.pro
QT + = core
QT + = network
QT-= gui
CONFIG + = 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: UnixPlus TARGET 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 () waitForReadyRead (100)); / / waiting to read data from the browser
Char webBrowerRXData [1000]
Int sv = socket- > read (webBrowerRXData, 1000)
QDebug () 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 (""
"
"
"
"Test Qt HttpServer"
"
"seconds connected:")
QByteArray str
Static qint16 count; / / Statistics for display on browsers
Str.setNum (count++)
Socket- > write (str)
Socket- > write (""
")
Socket- > flush ()
Connect (socket, & QTcpSocket::disconnected, socket, & QTcpSocket::deleteLater)
Socket- > disconnectFromHost ()
}
MyHttpServer::~MyHttpServer ()
{
Socket- > close ()
} this is the end of the article on "how Qt implements http Services". I hope the above content can be of some help to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.