Git Product home page Git Product logo

tinywebserver's Introduction

TinyWebServer

Linux下C++轻量级Web服务器,助力初学者快速实践网络编程,搭建属于自己的服务器.

  • 使用线程池 + epoll(ET和LT均实现) + 模拟Proactor模式并发模型
  • 使用状态机解析HTTP请求报文,支持解析GET和POST请求
  • 通过访问服务器数据库实现web端用户注册、登录功能,可以请求服务器图片和视频文件
  • 实现同步/异步日志系统,记录服务器运行状态
  • 经Webbench压力测试可以实现上万的并发连接数据交换

模块概述

Update

  • 解决请求服务器上大文件的Bug
  • 增加请求视频文件的页面
  • 解决数据库同步校验内存泄漏
  • 实现两种CGI数据库访问逻辑
  • 实现ET非阻塞和LT阻塞,并完成压力测试

Demo

  • 注册演示
  • 登录演示
  • 请求图片文件演示(6M)
  • 请求视频文件演示(39M)

压力测试

Webbench对服务器进行压力测试,在ET非阻塞和LT阻塞模式下均可实现上万的并发连接.

  • ET非阻塞
  • LT阻塞
  • 并发连接总数:10500
  • 访问服务器时间:5s
  • 所有访问均成功

注意: 使用本项目的webbench进行压测时,若报错显示webbench命令找不到,将可执行文件webbench删除后,重新编译即可。

框架

web端界面

  • 判断是否注册
  • 注册
  • 注册失败提示
  • 登录
  • 登录失败提示

基础测试

  • 服务器测试环境

    • Ubuntu版本16.04
    • MySQL版本5.7.29
  • 浏览器测试环境

    • Windows、Linux均可
    • Chrome
    • FireFox
    • 其他浏览器暂无测试
  • 测试前确认已安装MySQL数据库

    //建立yourdb库
    create database yourdb set utf8;
    
    //创建user表
    USE yourdb;
    CREATE TABLE user(
        username char(50) NULL,
        passwd char(50) NULL
    )ENGINE=InnoDB;
    
    //添加数据
    INSERT INTO user(username, passwd) VALUES('name', 'passwd');
  • 修改main.c中的数据库初始化信息

    //root root为服务器数据库的登录名和密码
    connection_pool *connPool=connection_pool::GetInstance("localhost","root","root","yourdb",3306,5);
  • 修改http_conn.cpp中的root路径

    const char* doc_root="/home/qgy/TinyWebServer/root";
  • 生成server

    make server
  • 启动server

    ./server port
  • 浏览器端

    ip:port

个性化测试

  • 选择任一校验方式,代码中使用同步校验,可以修改为CGI.
  • 同步线程数据库校验

    • 关闭main.c中CGISQLPOOL,打开SYNSQL

       23 #define SYNSQL    //同步数据库校验
       24 //#define CGISQLPOOL  //CGI数据库校验
    • 关闭http_conn.cpp中两种CGI,打开SYNSQL

      7 //同步校验
      8 #define SYNSQL
      
      10 //CGI多进程使用链接池
      11 //#define CGISQLPOOL
      
      13 //CGI多进程不用连接池
      14 //#define CGISQL
  • CGI多进程数据库校验,不使用连接池

    • 关闭main.c中SYNSQL和CGISQLPOOL

       23 //#define SYNSQL    //同步数据库校验
       24 //#define CGISQLPOOL  //CGI数据库校验
    • 关闭http_conn.cpp中SYNSQL和CGISQLPOOL,打开CGISQL

      7 //同步校验
      8 //#define SYNSQL
      
      10 //CGI多进程使用链接池
      11 //#define CGISQLPOOL
      
      13 //CGI多进程不用连接池
      14 #define CGISQL
    • 关闭sign.cpp中的CGISQLPOOL,打开CGISQL

      12 #define CGISQL    //不使用连接池
      13 //#define CGISQLPOOL  //使用连接池
    • 修改sign.cpp中的数据库初始化信息

      //root root为服务器数据库的登录名和密码
      connection_pool *connPool=connection_pool::GetInstance("localhost","root","root","yourdb",3306,5);
    • 生成CGISQL.cgi

      make CGISQL.cgi
  • CGI多进程数据库校验,使用连接池

    • 关闭main.c中SYNSQL,打开CGISQLPOOL

       23 //#define SYNSQL    //同步数据库校验
       24 #define CGISQLPOOL  //CGI数据库校验
    • 关闭http_conn.cpp中SYNSQL和CGISQL,打开CGISQLPOOL

      7 //同步校验
      8 //#define SYNSQL
      
      10 //CGI多进程使用链接池
      11 #define CGISQLPOOL
      
      13 //CGI多进程不用连接池
      14 //#define CGISQL
    • 关闭sign.cpp中的CGISQL,打开CGISQLPOOL

      12 //#define CGISQL    //不使用连接池
      13 #define CGISQLPOOL  //使用连接池
    • 生成CGISQL.cgi

      make CGISQL.cgi
  • 选择任一I/O复用方式,代码中使用LT阻塞,可以修改为ET非阻塞.
  • LT阻塞

    • 关闭main.c中ET,打开LT

      28 //#define ET       //边缘触发非阻塞
      29 #define LT         //水平触发阻塞
    • 关闭http_conn.cpp中ET,打开LT

      16 //#define ET       //边缘触发非阻塞
      17 #define LT         //水平触发阻塞
  • ET非阻塞

    • 关闭main.c中LT,打开ET

      28 #define ET         //边缘触发非阻塞
      29 //#define LT       //水平触发阻塞
    • 关闭http_conn.cpp中LT,打开ET

      16 #define ET       //边缘触发非阻塞
      17 //#define LT         //水平触发阻塞
  • 选择任一日志方式,代码中使用同步日志,可以修改为异步写入.
  • 同步写入日志

    • 关闭main.c中ASYNLOG,打开同步写入SYNLOG

      25 #define SYNLOG //同步写日志
      26 //#define ASYNLOG   /异步写日志
  • 异步写入日志

    • 关闭main.c中SYNLOG,打开异步写入ASYNLOG

      25 //#define SYNLOG //同步写日志
      26 #define ASYNLOG   /异步写日志
  • 选择数据库访问、I/O复用方式或日志写入方式后,按照前述生成server,启动server,即可进行测试.

更多资料

请关注公众号 “两猿社”.

  • 带你丰富互联网相关项目经验,轻松应对校招!!!
  • 项目模块详细讲解,在公众号内持续更新!!!

致谢

Linux高性能服务器编程,游双著.

tinywebserver's People

Contributors

qinguoyi avatar zwiley avatar mamil avatar zjuhong avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.