Talking about network programming (based on C + +) __linux

Source: Internet
Author: User

This article tells a little bit about some new ways of network programming. Learning is the process of constantly summarizing and improving.

Design to processes, threads, thread pools, Reactior and proactor concurrent programming patterns, iocp,linux under epoll ...




I am engaged in enterprise-level software development, looking at today's enterprise-class software, stand-alone model has become less and more of the C/S model, the current client and server communication between the socket technology to achieve.

The above mentioned socket technology, naturally to learn TCP/IP protocol, for the theory of TCP/IP, Learning Richard's "TCP/IP Detailed Volume 1: agreement" I think is the choice, this book covers a lot of, if for only the realization of C/s communication network library, Just understand the chapters that introduce UDP and TCP, and a solid theoretical foundation will give you a good explanation of what's going on in the network, and it's easy for you to solve these problems. Theoretical link implementation, or Richard's "UNIX Network Programming Volume 1:" Set interface API (commonly known as UNP1), this book I have been watching, but not finished, I think this is the Web programming the Bible, you mastered the book, the basic also mastered the network programming, even the details can be covered.

The following only covers the TCP protocol

Then learn to practice, think about the scene of network communication: one server should correspond to hundreds of thousands. Customers, so you have to take into account the server's processing power.

The simplest model is that you use a process to handle all the client connections, my God. You think, in the process if there are hundreds of connections at the same time request services, we use this pattern, first the next connection to wait for the last connection to finish (sync), the processing of the connection is likely to block in the data operation (I/O), In this way the efficiency of the processing of the connection and the slow response of the client I think almost no one can bear it.

Well, in order to improve efficiency, let's improve, for each client connection to produce a thread (Windows) or process (Linux) to handle, throw the thread or process of context switching loss, and not talk about SMP, just see the hundreds of threads and the feasibility of the process, I'm sorry, My operating system is a thread or process resource limit.

In order to solve the problem of resource loss and resource limit caused by frequent handoff, we can improve it, adopt a thread pool to handle some connections, wait for other connections, after all, I have not much CPU, but also able to deal with so several connections, the response efficiency and processing efficiency are still not mentioned.

Think of a problem, in fact, our network time consuming is generally in the data operation (I/O), in order to increase the response of the client, we can divide the network into the processing of connected threads and logical processing of the thread, so that can greatly improve the client's response, Remember, however, that you must maintain this connected session in a logical processing thread. This seems to be good, no,no, in fact, is not good, you do not know when there is data to be processed, you have to poll to determine whether the data operation ..., efficiency or bad AH.

Well, we don't have the original technology, choose the classic reactor and Proactor concurrent programming mode, they are all based on event-driven, we are the network needs to deal with the events to register to the event manager (such as network behavior events, IO operation events ...), and then the event status is ready, He used a callback to notify us to deal with, how, so at least the CPU will not idle, with only one thread can handle almost all the events. But reactor and Proactor are still very different, reactor for I/O This step is to be handled by themselves, but proactor for I/O This step is done by the operating system, and then the completion of the event to inform you, Then you can do the next step (read the data from the buffer buf, for example), which is much quicker than the way I am operating I/O. At present, I write the network library in Windows is the Proactor mode: With Windows provided by the completion of the Port model (IOCP), in Linux, because Linux does not have a good asynchronous I/O mechanism, had to use the reactor way: using Li The peculiar epoll of Nux.

Talk about my own opinion: from my understanding, for most of the network library, many are I/o-intensive, so as if the Proactor mode has more advantages, but Linux does not and Windows under the IOCP similar mechanism, but can be used Epoll Add the task queue way to achieve a set, but as if it is very complicated, I want to realize on their own to forget it. Fortunately, "Mountain poor water, Vista", boost ASIO has been for us to seal the Windows and Linux under the Proactor implementation, Windows is the completion of the port, Linux is the Epoll plus the task queue to achieve the way. Next, I'm going to change the reactor network library implemented by the Epoll way under Linux to boost ASIO's implementation.

Today, for the network programming overall and general introduction under it, in fact, there are many problems did not involve, I am very interested in network programming, and now engaged in this aspect of the work, so I have the opportunity to share some more detailed and comprehensive knowledge, in view of my limited level, I hope you can criticize the mistakes in the article, we progress together ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.