Number of concurrent sockets in various models

Source: Internet
Author: User
Tags net thread

Reprinted from: http://fpcfjf.blog.163.com/blog/static/5546979320108230035670/

Today, I took a serious look at socket programming at the company, and the number of concurrency for some of the programming models was never clear, just taking the opportunity of the current project to clear it up, currently, the socket programming model mainly includes the following types (mainly win, Linux and UNIX are rarely used ):
1. Common blocking and non-blocking programming.
Using thread pool technology and memory pool and socket pool technology, we can basically process about one thousand five hundred socket connections, but we generally use about two MB of memory on machines, without changing the size of the thread stack, we can create up to one thousand and seven eight hundred threads, But it basically cannot be changed. We tested about one thousand threads, and the machine was very slow.
Also in the WIN-XP-SP2, the single process of thread concurrency is processed, the default is 10, the method of modification online is more.
2. Select model (asynchronous and synchronous)
This model is a maximum of 64 socket connections by default in the case of a single thread. You can modify the fd_setsize in the winsock2.h file, but it cannot exceed the underlying Winsock limit (1024). However, if multiple threads are used, you can process more resources. The actual maximum number is, we recommend no more than 1000 threads in a single thread. As for multithreading, we should also control the thread switching efficiency and data processing time. There should be no problem with thousands.
3. wsaasyncselect Model
This model uses the message mechanism. It is recommended that no more than 1000 messages be created.
4. wsaeventselect Model
This model uses the event-driven approach. A single thread cannot exceed 64 (wsawaitformultipleevents can wait for a maximum of 64 events). If multiple sockets are concurrent, the thread pool technology should be used, there should be no problem with thousands.
5. Overlapping Io
There should be thousands. This is, after all, a technical basis for the following iocp.
6. iocp completes port + overlapping Io
This is a fuse weapon to solve socket communication. Unfortunately, we only use win and above 2000. Linux seems to have a similar epoll, and it seems to be better than this, but it has never been used. It should be easy to solve the concurrency of tens of thousands of sockets. Of course, you must keep up with the programming level and technology.
I can see from the Internet that if the server version of the operating system and machine used can reach a maximum of one hundred million, it would be terrible, then the guy himself reached a maximum of 50 thousand on XP, however, memory overflow occurs constantly. He has a maximum of 9 W.

By the way, the concurrent socket communication under net is extracted from others' blogs:
Brief performance of several socket models on the. NET platform
This content has also been discussed many times in cnblogs. I have read some documents over the past two days and read some simple performance indicators to discuss with you.
Socket + threads/threadpool
Approximate performance: less than 1500 connections
Implementation: An accept socket is handed over to a thread for management. It is relatively stupid, but it is also effective, because it is a synchronous method and easy to control. At the advanced level, the thread pool is managed by a thread pool. The thread pool is automatically managed by the system, saving the overhead thread time. Generally, it is sufficient for small projects and simple for development. Note that if several sockets occupy threads in the thread pool for a long time and there are many other connections, it is easy to prompt that you do not have enough threads for use. Haha, it is a good way to make the socket do less and take less time. In addition, if there are some good third-party thread pool components, you can also choose to use them, such as smartthreadpool.
Socket + select
Approximate performance: performance decreases after more than 1500 connections
Implementation: select is a common model. It is to poll one or more sockets in the blocking function, and put the socket to be processed into an ilist. When the select round robin ends, we can then process the socket in this ilist by ourselves. For specific usage, refer to msdn. The efficiency of the SELECT statement is not high, because when the number of sockets to be processed in the queue is large, it is not cost-effective to process the last few sockets.
Socket + asynchronous
Performance: about 7500 client connections
Implementation: beginxxxx, endxxxx. I'm not familiar with it anymore. In the final analysis, asynchronous socket uses the thread pool technology to process asynchronous Io. This raises another question ,. net thread pool is used in what implementation method, someone has said before ,. net thread pool is implemented using the completion port. I don't know if this statement is correct. I can't confirm it from the information I found (I hope some friends can tell me this ). Asynchronous socket Program The processing process is much more complicated than synchronization, and the control of asynchronous callback functions is not as intuitive as the synchronization method. However, I think it should be noted that the callback function should be lightweight and should not handle too many transactions. The processing of transmitted data should be handed over to other threads for processing.

Iocp (completion port)
Approximate performance: About 20000 ~ 50000 client connections
Implementation: There are some pseudo-iocp in. net. You can search for them. We have not seen any open socket examples implemented using these pseudo-iocp. I am talking about 20000 ~ 50000 client connections are developed in C ++. In this case, the basic technologies required include memory pools and queries.Algorithm.

The maximum number of connections that can be achieved by pseudo-iocp. If you have any information, you can discuss it. In addition, many of the data mentioned above are extracted from some materials. I have not tried it myself. I just want to discuss it with you. I think a high-performance server program may require more than just models, but also many details, such as memory processing and algorithms. Of course, this is only about the software cost, and the hardware must also be invested.

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.