When we just get started with C + + network programming, we will learn some basic API or the implementation of reflexive server, chat room and simple HTTP server in the book.
After learning the above-mentioned foundation, most of the children want to do a decent project, but it seems that they don’t know how to start, or what the framework of a decent project is.
I suggest the beginningDon’t drill into the network framework, alsoNot suitable for starting mature and complex projectsIt’s better to find some basic open source projects and add bricks and tiles or rewrite them according to your own ideas. After you have these foundations, you can talk about refinement.
In fact, for beginners,The most difficultIs to find oneEasy to start, basic background development project。
At this time, many people will recommend and recommend Chen Shuo’s book and Muduo, and I also recommend it.
However, I suggest that before reading Mr. Chen’s book, you can first read the dual Linux high-performance server programming, run the code and routines in the book again, and then read Muduo, which will surely be more fruitful.
C + + background development, generally refers to the server-side development, involving C / S model and B / S model. Among them, the C / S model pays more attention to the TCP protocol, both the server and the client need to complete by themselves, and the data package also needs to be designed by itself; the B / S model pays more attention to the HTTP protocol, because the browser acts as the client and there are ready-made HTTP protocols, so it focuses on the server side.
We look at mostThe contents in the book are all about the C / S model first, and then the B / S model。 Compared with the entry-level, the reflexive server and chat room program in the book are easier to get started and understand, but it’s not easy to make a C / S model project that can be taken easily.
From the perspective of interview project, I prefer to start with a simple B / S model, and then transition to a simple C / S model。 In this way, we can skip the design of client and data package, focus more on the server side, and complete a small demo as soon as possible to enhance the confidence of beginners.
Here, I would like to share with you the project I prepared for the autumn recruitment interview last year. I hope it will be helpful to the newcomers. This project is still being updated and maintained.
Project link: https://github.com/qinguoyi/T…
Last autumn, after reading Youshuang’s high-performance Linux server programming, I saved one according to the code in the bookC + + lightweight web server under LinuxOn the basis of the original project, it has realized simple front-end interaction and background access functions such as simple registration and login, and can realize tens of thousands of concurrent connections after stress test. (the test machine is Intel i7 7700, 16g memory).
Although C + + lightweight web server under Linux is a toy level project, butAs long as you follow the steps in the readme, after multiple students test, you can run throughThis will greatly enhance the confidence of beginners to do the project.
- Registration demo
- Login demo
After feedback from the partners, once the server file is requested to be larger, there will be a problem, either the file is not fully displayed, or it cannot be displayed. After analyzing and locating the problem, I made a preliminary correction and realized it
Demo。 When the code test is complete, I’ll synchronize it to
At present, after the bug has been initially revised, I have conducted a large file test in Chrome browser under Ubuntu, and requested the large image (6m) and video (493k) on the server. The effect is as follows.
- Video testing
- Big picture test
Webbench can realize tens of thousands of concurrent connections by stress testing the server
- Total concurrent connections: 10500
- Server access time: 5S
- Response requests per second: 552852 pages / min
- Data transmission per second: 1031990 bytes / sec
- All visits were successful
The overall framework is like this below. The module connection is relatively simple, and the coupling is not high. It is very simple and crude. It only needs to be able to understand.
The modules and knowledge points involved in the project are sorted out and listed below. Each module has corresponding knowledge understanding and application.
- Involving thread, lock mechanism。 Using a work queue completely decouples the main thread and the worker thread: the main thread inserts the task into the work queue, and the worker thread obtains the task through competition and executes it.
HTTP request and response
- It involves Linux system programming, network programming, TCP and HTTP protocol。 According to the state transition, the HTTP connection class is encapsulated by the master-slave state machine. Among them, the master state machine calls the slave state machine internally, and the slave state machine transmits the processing state and data to the main state machine.
- It involves a little knowledge of HTML。 The user name and password verification is realized, and the jump is realized by setting action in HTML file.
- It involves the use of signal and data structure in Linux system。 Because the inactive connection takes up the connection resources, which seriously affects the performance of the server, a server timer is implemented to process the inactive connection and release the connection resources.
Database connection pool
- Mysql database involved。 Establishing database connection pool can solve the problem of frequent connection establishment by reusing these established database connections, so as to improve the system performance.
Synchronous / asynchronous log system
- Involving design pattern, custom blocking queue。 The synchronous / asynchronous log system mainly involves two modules, one is the log module and the other is the blocking queue module. The blocking queue module is mainly used to prepare for asynchronous writing logs.
- Read the webbench source code to deepen the understanding of the process。 Create multiple processes through webbench, and each process accesses the server through HTTP connection to complete the stress test.
AgainMost of the code is from the book. I just organized it into a relatively complete project, added some functions, and made some comments and explanations on all of them.
If you have any questions about the above project module principle, everyoneLook up Linux high performance server programming of Youshuang firstIf you have some vague words, please come to my company to check the series of explanations. If you really don’t know, please harass me directly.
If you’re interested in the project, or have questions about learning about it,Check my profile and you can contact me directly。
Thank you for seeing this.