#include "httpserver.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonParseError>
#include <QNetworkReply>
#include <QFile>
#include "../net.h"
HttpServer &HttpServer::instance()
{
static HttpServer obj;
return obj;
}
void HttpServer::run(const QHostAddress &address, const quint16 &port)
{
m_httpServer->listen(address, port);
qDebug() << "111111111"<<address << port;
}
void HttpServer::newConnection()
{
qDebug() << "newConnection";
QTcpSocket *m_socket = m_httpServer->nextPendingConnection();
QObject::connect(m_socket, &QTcpSocket::readyRead, this, &HttpServer::readyRead);
}
void HttpServer::readyRead()
{
/*QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
if (socket){
QFile file(":/pc_probject/Resources/light_motor.bin");
if (!file.open(QIODevice::ReadOnly))
{
qDebug("open failed!");
return;
}
QByteArray request = socket->readAll();
qDebug() << "Request Data http:" << request;
static int count = 0;
count++;
QByteArray response = QString("<h1><center>Hello World %1</center></h1>").arg(count).toUtf8();
QString http = "HTTP/1.1 200 OK";
http += "Server: nginx";
http += "Content-Type: text/html;charset=utf-8";//Content-Type: application/octet-stream;charset=utf-8\r\n //Content-Type: text/html;charset=utf-8
http += "Connection: keep-alive";
http += QString("Content-Length: %1").arg(QString::number(file.size()));
socket->write(http.toUtf8());
socket->write(response);
socket->flush();
socket->waitForBytesWritten(http.size() + response.size());
socket->close();
}*/
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
QString curPath0;
switch (app.http_hig)
{
case ON0:curPath0 = "/PC/5W_light_motor.bin"; break;
case ON1:curPath0 = "/PC/7W_light_motor.bin"; break;
case ON2:curPath0 = "/PC/9W_light_motor.bin"; break;
case ON3:curPath0 = "/PC/12W_light_motor.bin"; break;
case ON4:curPath0 = "/PC/ceiling_light240.bin"; break;
case ON5:curPath0 = "/PC/ceiling_light180.bin"; break;
case ON6:curPath0 = "/PC/dl_switch1.bin"; break;
case ON7:curPath0 = "/PC/dl_switch2.bin"; break;
case ON8:curPath0 = "/PC/dl_switch3.bin"; break;
case ON9:curPath0 = "/PC/dl_switch4.bin"; break;
case ON10:break;
case ON11:break;
case ON12:break;
case ON13:break;
default:break;
}
QString result = curPath + curPath0;
QFile file(result);
if (!file.open(QIODevice::ReadOnly))
{
qDebug("open failed!");
return;
}
QByteArray request = socket->readAll();
qDebug() << "Request Data http:" << request;
QString http = "HTTP/1.1 200 OK\r\n";
http += "Server: nginx\r\n";
http += "Content-Type: application/octet-stream;charset=utf-8\r\n";
http += "Connection: keep-alive\r\n";
http += QString("Content-Length: %1\r\n\r\n").arg(QString::number(file.size()));
if (socket) {
QByteArray headData, data;
headData.append(http);
socket->write(headData);
while (!file.atEnd()) {
data = file.read(file.size()); //每次读取10k的数据,并发送
socket->write(data);
}
qDebug("success");
}
curPath0.clear();
result.clear();
}
HttpServer::HttpServer(QObject *parent) : QObject(parent)
{
m_httpServer = new QTcpServer(this);
m_httpServer->setMaxPendingConnections(1024);//设置最大允许连接数
QObject::connect(m_httpServer, &QTcpServer::newConnection, this, &HttpServer::newConnection);
curPath = QCoreApplication::applicationDirPath();
app.http_hig = ON0;
/*
QString curPath0 = "/PC/12W_light_motor.bin";
QString result = curPath + curPath0;
QString curPath = QCoreApplication::applicationDirPath();
QString result = curPath + curPath0;
qDebug() << "aaaaaaaaa" << result;
*/
}
HttpServer::~HttpServer()
{
}
QT+VS2013 C++代码 http服务器创建,实现esp32 http OTA升级
需积分: 5 21 浏览量
2022-06-16
12:30:13
上传
评论 2
收藏 2KB RAR 举报
凌风_lwp
- 粉丝: 96
- 资源: 16
最新资源
- projectData
- SQL Server 性能监视器,它旨在提供开箱即用的全面监控,并作为您自己的项目或应用程序的监控框架 它在本地数据库中收集性能
- Python 程序语言设计模式思路-并发模式:线程池模式:管理线程池,优化线程创建和销毁
- 股事汇投资工具-实时新闻、财经日历、市场快讯、持仓查询、外汇兑换、换算工具、大盘云图、江恩工具、指标检测等
- webrtc-streamer
- html+css+'青春献礼二十大 红色旅游助乡村'为主题的网页设计 2022年参与学校网页设计比赛时完成的
- Unity 模型布尔运算插件
- eCANtools工具,CAN资料包
- 129335283047061xiazaigongjuxiang(去重软件).apk
- Android环境检测工具,检测ksu,lsp,magisk等
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0