Qt实现http服务的示例代码

 更新时间:2023年04月13日 09:25:51   作者:音视频开发老舅  
这篇文章将为大家详细讲解有关Qt如何实现http服务,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获

先看执行结果:

Qt HttpServer

左边是开启的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 <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_H

main.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服务未启动";
    }
    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("<!DOCTYPE>"
                  "<html>"
                      "<header>"
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"
                        "<title>测试Qt HttpServer</title>"
                      "</header>"
                      "<body>已连接秒数:");
    QByteArray str;
    static qint16 count;    //用于在浏览器上显示的统计数字
    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();
}

到此这篇关于Qt实现http服务的示例代码的文章就介绍到这了,更多相关Qt http服务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++ Coroutine简单学习教程

    C++ Coroutine简单学习教程

    这篇文章主要为大家详细介绍了C++ Coroutine的简单学习教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • C++多线程中互斥量的使用详解

    C++多线程中互斥量的使用详解

    这篇文章主要介绍了C++多线程中互斥量的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • C语言实现数学表达式运算

    C语言实现数学表达式运算

    这篇文章主要为大家详细介绍了c语言实现数学表达式运算,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • C++右值引用问题解决

    C++右值引用问题解决

    本文主要介绍了C++右值引用问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • C++中的friend函数详细解析

    C++中的friend函数详细解析

    本篇文章主要介绍了C++中的friend函数详细解析,对初学c++的人有一定的帮助,有需要的可以了解一下。
    2016-11-11
  • C指针原理教程之语法树及其实现

    C指针原理教程之语法树及其实现

    本文给大家分享的是如何使用C语言的指针原来来实现语法树,并给大家提供了详细的实例代码,希望大家能够喜欢
    2019-02-02
  • C++基于先序、中序遍历结果重建二叉树的方法

    C++基于先序、中序遍历结果重建二叉树的方法

    这篇文章主要介绍了C++基于先序、中序遍历结果重建二叉树的方法,结合实例形式分析了基于C++构建二叉树的相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • C语言中带返回值的宏定义方式

    C语言中带返回值的宏定义方式

    这篇文章主要介绍了C语言中带返回值的宏定义方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C语言实现万年历效果

    C语言实现万年历效果

    这篇文章主要为大家详细介绍了C语言实现万年历效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • c++ 前自增/后自增操作符效率分析

    c++ 前自增/后自增操作符效率分析

    这篇文章主要介绍了c++ 前自增/后自增操作符效率分析,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下
    2021-01-01

最新评论