TuFao服务器的安装和搭建
来源:互联网 发布:mac ext4 读写 编辑:程序博客网 时间:2024/05/30 04:30
TuFao服务器
tufao是一个由QT编写的HTTP服务器。
tufao代替apache来实现http的通信。
安装tufao
- 获取原代码
- 编译和安装
sudo apt-get install cmake qt-sdk
在tufao目录下创建build目录
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr
make
sudo make install - 创建工程
创建空的工程
工程文件中增加CONFIG += TUFAO1 C++11
增加一个类MyServer,一定是QObject派生类
增加一个main.cpp实现main函数
在MyServer的构造函数,创建Tufao::HttpServer对象server
将server的信号requestReady和自己写的槽函数slotRequestReady连接
在slotRequestReady函数中,实现http协议的响应报文。
示例如下:
MyServerHandlePost.h
#ifndef MYSERVERHANDLEPOST_H#define MYSERVERHANDLEPOST_H#include <QObject>#include <Tufao/HttpServer>#include <Tufao/HttpServerRequest>#include <Tufao/HttpServerResponse>using namespace Tufao;class MyServerHandlePost : public QObject{ Q_OBJECTpublic: explicit MyServerHandlePost(QObject *parent = 0); HttpServer* server; void handlePostData(HttpServerRequest&, HttpServerResponse&);signals:public slots: void slotRequestReady(HttpServerRequest&, HttpServerResponse&);};#endif // MYSERVERHANDLEPOST_H
MyServerHandlePost.cpp
#include "MyServerHandlePost.h"#include <QFile>#include <QJsonObject>#include <QJsonValue>#include <QJsonDocument>#include <QJsonArray>MyServerHandlePost::MyServerHandlePost(QObject *parent) : QObject(parent){ server = new HttpServer; if(!server->listen(QHostAddress::Any, 8081)) { qDebug()<<"bind error"; } connect(server, &HttpServer::requestReady, this, &MyServerHandlePost::slotRequestReady);}void MyServerHandlePost::handlePostData(HttpServerRequest &request, HttpServerResponse &response){// QFile file("/home/xueguoliang/a.txt_upload");// file.open(QFile::WriteOnly); /* data is {username: "xxx", password: "yyy"} */ QByteArray data = request.readBody(); QJsonDocument doc = QJsonDocument::fromJson(data); QJsonObject root = doc.object(); QString username = root.value("username").toString(); QString password = root.value("password").toString(); if(username == "aaa" && password== "bbb") { // {result: "ok"} root.insert("result", QString("ok")); } else { // {result: "ok"} root.insert("result", QString("err")); root.insert("reason", QString("username or password error")); } { QJsonDocument doc(root); QByteArray buf = doc.toJson(); response.writeHead(HttpResponseStatus::OK); response.end(buf); }// file.write(data);// file.close();}void MyServerHandlePost::slotRequestReady(HttpServerRequest &request, HttpServerResponse & response){ if(request.method() == "POST") { // connect(&request, &HttpServerRequest::ready, ) // end signal: all data is ready // connect(&request, SIGNAL(end()), this, SLOT(slotDataEnd())); connect(&request, &HttpServerRequest::end, [&](){ handlePostData(request, response); }); } else { response.writeHead(HttpResponseStatus::OK); response.end("i need post method\n"); }}
0 0
- TuFao服务器的安装和搭建
- tufao安装和实例详解
- SVN 服务器和客户端的安装,搭建
- SVN安装、服务器的搭建和使用
- SVN服务器的搭建和myeclipse svn插件的安装!
- php服务器的搭建和thinksns的安装
- Ubuntu 10.04 的源服务器和PXE安装环境搭建。
- CentOS的安装和搭建文件共享服务器(Samba)
- ubuntu14.04 搭建FTP服务器 -- vsftpd的安装和配置
- centos服务器上安装neo4j和ftp服务器的搭建(附最新安装包)
- SVN客户端安装和服务器搭建
- tufao环境搭建-记常见错误一例
- Django的安装与服务器的搭建
- tufao安装出错,提示make: *** [cmake_check_build_system] 段错误(core dumped)
- 安装基于vsftpd搭建的ftp服务器
- SVN服务器搭建--TortoiseSVN的配置安装
- MYSQL学习(一)环境的搭建和服务器的管理工具的安装
- 构建在Qt之上的C++ Web开发框架:tufao
- iOS JSON返回的数据有NSNull导致程序崩溃及ios nil、NULL和NSNull的区别
- Maven搭建SpringMvc+Spring+Hibernate框架
- 根据font-size计算rem尺寸,动态缩放页面
- MongoDB 分片问题汇总
- 广义表
- TuFao服务器的安装和搭建
- ExtJS学习笔记(八) 数据绑定
- @RequestMapping 用法详解之地址映射(转)
- jQuery获取radio选中项值得的几种方法checked属性
- 【C#函数式编程】C#中的函数式编程(二) —— IEnumerable和IEnumerator使用详解
- C++开发者都应该使用的10个C++11特性
- 网上下载easyui demo,使用方法
- c++友元的几种情况
- spring+mongodb的整合