TinyWebServer
C++ lightweight web server under Linux helps beginners quickly practice network programming and build their own server.
- Concurrency model using thread pool + non-blocking socket + epoll (implemented by both ET and LT) + event processing (implemented by both Reactor and simulated Proactor)
- Use state machine to parse HTTP request messages, support parsing GET and POST requests
- Access the server database to implement web user registration and login functions, and can request server pictures and video files
- Implement synchronous/asynchronous log system to record server running status
- After Webbench stress test , tens of thousands of concurrent connection data exchanges can be achieved
write in front
- During the development and maintenance of this project, many children's shoes were offered red envelopes to support me, but I declined them all. I will not, nor will I in the future, package this project into any course for sale, nor will I open any support channels.
- At present, there are people on the Internet who package this project or the project of Youshuangdao into courses for sale. Please keep your eyes peeled and identify the C++ server projects of major learning/job hunting websites, and don’t pay blindly.
- A senior interviewer found me in the company through project information and found that many children's resumes used this project. However, during the interview process, I found that
很多童鞋通过本项目入门了,但是对于一些东西还是属于知其然不知其所以然的状态,需要加强下基础知识的学习
. It is recommended to read it carefully.- "Advanced Programming in Unix Environment"
- "unix network programming"
- Thank you to all the bosses, friends, and children’s shoes for your recognition and support. It would be a great honor for me if this project could get you started.
Table of contents
Overview | frame | Demo | stress test | Change log | Source code download | Run quickly | Personalized operation | Cook Ding Jie Niu | CPP11 implementation | Acknowledgments |
---|
Overview
- C/C++
- B/S model
- Thread synchronization mechanism wrapper class
- http connection request processing class
- Semi-synchronous/semi-reactor thread pool
- Timer to handle inactive connections
- Synchronous/asynchronous logging system
- Database connection pool
- Synchronous thread registration and login verification
- Simple Server Stress Test
frame
Demo
- Request image file demonstration (6M)
- Request video file demonstration (39M)
stress test
After closing the log, use Webbench to perform a stress test on the server. Use ET and LT modes for listenfd and connfd respectively. Both can achieve tens of thousands of concurrent connections. Listed below are the test results after the combination of the two.
- Proactor, LT + LT, 93251 QPS
- Proactor, LT + ET, 97459 QPS
- Proactor, ET + LT, 80498 QPS
- Proactor, ET + ET, 92167 QPS
- Reactor, LT + ET, 69175 QPS
- Total number of concurrent connections: 10500
- Access server time: 5s
- All accesses were successful
Note: When using the webbench of this project for stress testing, if an error message shows that the webbench command cannot be found, delete the executable file webbench and recompile.
Change log
Source code download
There are currently two versions. There are major changes in the code structure between versions, and the documentation and code running methods are also inconsistent. The refactored version is more concise, and the original version (raw_version) is larger, retaining the original flavor of Youshuang code, and it is easier to start with the original version.
If you encounter a failure to download the github code, or the access is too slow, you can download it from the following link and synchronize it with the latest Github submission.
- Refactored version download address: BaiduYun
- Original version (raw_version) download address: BaiduYun
- Extraction code: 9wye
- To run the original version, please refer to the original documentation.
Run quickly
Server test environment
- Ubuntu version 16.04
- MySQL version 5.7.29
Browser testing environment
- Both Windows and Linux are available
- Chrome
- FireFox
- No testing on other browsers yet
Make sure the MySQL database is installed before testing
// 建立yourdb库
create database yourdb;
// 创建user表
USE yourdb;
CREATE TABLE user (
username char ( 50 ) NULL,
passwd char( 50 ) NULL
)ENGINE=InnoDB;
// 添加数据
INSERT INTO user (username, passwd) VALUES( ' name ' , ' passwd ' );
Modify the database initialization information in main.cpp
//数据库登录名,密码,库名
string user = " root " ;
string passwd = " root " ;
string databasename = " yourdb " ;
build
Start server
Browser side
Personalized operation
./server [-p port] [-l LOGWrite] [-m TRIGMode] [-o OPT_LINGER] [-s sql_num] [-t thread_num] [-c close_log] [-a actor_model]
Warm reminder: The above parameters are not necessary, you do not need to use them all, you can choose them according to your personal situation.
- -p, custom port number
- -l, select the log writing method, the default is synchronous writing
- 0, synchronous writing
- 1. Asynchronous writing
- -m, mode combination of listenfd and connfd, defaults to LT + LT
- 0, means use LT + LT
- 1, means using LT + ET
- 2, means using ET + LT
- 3, means using ET + ET
- -o, close the connection gracefully, not used by default
- -s, number of database connections
- -t, number of threads
- -c, close the log, open by default
- 0, open the log
- 1. Close the log
- -a, select the reactor model, the default is Proactor
- 0, Proactor model
- 1. Reactor model
Test example commands and meanings
./server -p 9007 -l 1 -m 0 -o 1 -s 10 -t 10 -c 1 -a 1
Cook Ding Jie Niu
The recent version iteration is faster, and the following content is mostly based on the old version (raw_version) code for detailed explanation.
- A novice’s perspective: Understand the president’s TinyWebServer in one article
- Detailed explanation of the latest version of the Web server project - 01 Thread synchronization mechanism encapsulation class
- Detailed explanation of the latest version of the Web server project - 02 Semi-synchronous semi-reactor thread pool (Part 1)
- Detailed explanation of the latest version of the Web server project - 03 Semi-synchronous semi-reactor thread pool (Part 2)
- Detailed explanation of the latest version of Web server project - 04 http connection processing (Part 1)
- Detailed explanation of the latest version of Web server project - 05 http connection processing (Part 2)
- Detailed explanation of the latest version of Web server project - 06 http connection processing (Part 2)
- Detailed explanation of the latest version of the Web server project - 07 Timer processing of inactive connections (Part 1)
- Detailed explanation of the latest version of the Web server project - 08 Timer processing of inactive connections (Part 2)
- Detailed explanation of the latest version of the Web server project - 09 Logging system (Part 1)
- Detailed explanation of the latest version of the Web server project - 10 Logging system (Part 2)
- Detailed explanation of the latest version of Web server project - 11 Database connection pool
- Detailed explanation of the latest version of Web server project - 12 Register and log in
- Detailed explanation of the latest version of Web server project - 13 pitfalls and interview questions
- Updated
Star History
CPP11 implementation
A simpler and more elegant CPP11 implementation: Webserver
Acknowledgments
Linux high-performance server programming, written by You Shuang.
Thanks to the following friends for PR and help: @RownH, @mapleFU, @ZWiley, @zjuHong, @mamil, @byfate, @MaJun827, @BBLiu-coder, @smoky96, @yfBong, @liuwuyao, @Huixxi, @markparticle, @ blogg9ggg.