Qt如何实现http服务
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这篇文章将为大家详细讲解有关Qt如何实现http服务,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。先看执行结果:Qt HttpServer左边是开启的Qt Http
千家信息网最后更新 2024年09月22日Qt如何实现http服务
这篇文章将为大家详细讲解有关Qt如何实现http服务,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
先看执行结果:
左边是开启的Qt Http服务,监控服务端口,及接收客户端请求;右侧是浏览器访问服务。
下面是具体代码:
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服务未启动";
}
else
{
qDebug() << "\nWeb服务在端口8080等待客户端连接";
}
}
void MyHttpServer::connection()
{
socket = server->nextPendingConnection();
while (!(socket->waitForReadyRead(100))); //等待从浏览器读取数据
char webBrowerRXData[1000];
int sv = socket->read(webBrowerRXData, 1000);
qDebug() << "正在从浏览器读取数据=" << 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(""
""
""
""
"测试Qt HttpServer "
" "
"已连接秒数:");
QByteArray str;
static qint16 count; //用于在浏览器上显示的统计数字
str.setNum(count++);
socket->write(str);
socket->write(""
"");
socket->flush();
connect(socket, &QTcpSocket::disconnected, socket, &QTcpSocket::deleteLater);
socket->disconnectFromHost();
}
MyHttpServer::~MyHttpServer()
{
socket->close();
}
关于"Qt如何实现http服务"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
服务
浏览器
浏览
篇文章
客户
客户端
数据
更多
端口
不错
实用
代码
内容
右侧
数字
文章
正在
知识
结果
统计数字
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
江苏3u服务器散热器生产厂家
腾讯云服务器设置管理员
广西东塔安全网络安全培训
阿里云服务器是正品吗
数据库中服务器IP
信息网络安全评估办法
数据库备份全量与增量的区别
杭州软件开发的公司有多少
来电了电脑连不上服务器
软件开发研发部规划书
服务器域名可以做网页
综合网络安全工程介绍
张家港农商行数据库面试题
光伏电站网络安全
计算机软件数据库连接错误
洪湖市手机软件开发
php数据库查询接口
研究生 计算机网络技术
命运服务器不可用
信旭明途网络技术有限公司
我的世界租服务器卡吗
11年腾讯数据库
网络安全绘画入门素描
教职工网络安全提示
迷你http服务器
服务器管理员密码登录不上去
服务器深度安全防护系统卸载
湛江专业软件开发供应商
今日网络安全关键词
服务器主板芯片数量