C # Notes for writing high-performance Tcp Socket applications

Source: Internet
Author: User

 

The following is a summary of the implementation of a high-performance Socket component. If you only need to handle thousands of concurrent applications, just pay attention to the code writing, however, we need to deal with tens of thousands or tens of thousands of concurrent applications. I believe this will be of great help to write the following summary.

SocketAsyncEventArgs

 

This object is. net 2.0 SP1. it is mainly used for high-performance Socket data sending and receiving (for more details, refer to MSDN ). this object provides three methods to set the buffer for sending and receiving, SetBuffer (Int32, Int32), SetBuffer (Byte (), Int32, Int32), BufferList, the first two cannot coexist with the latter (there are reasons on MSDN ). when you set the Buffer, whether it is SetBuffer (Byte (), Int32, Int32) and BufferList, try to set each SocketAsyncEventArgs instance only once throughout the life cycle of the program, this setting will consume a lot of resources. we recommend that you set the data buffer through SetBuffer (Byte (), Int32, Int32) during the construction of SocketAsyncEventArgs, and then use SetBuffer (Int32, Int32) for processing. when you want to set BufferList, it is best not to change the byte [] source referenced by the IList <ArraySegment <byte>. if this parameter is changed, SocketAsyncEventArgs re-binding the buffer will affect the efficiency.

SocketAsyncEventArgs pool

 

As mentioned above, do not change the buffer referenced by SocketAsyncEventArgs as much as possible to achieve this purpose. therefore, you need to create a SocketAsyncEventArgs application pool to initialize the SocketAsyncEventArgs object at the beginning of the program. in addition to reducing the creation of SocketAsyncEventArgs, the construction of the pool can also greatly save memory. The main reason is that you cannot know the size of each message. Of course, you can give the message the maximum limit before design, then set the buffer corresponding to SocketAsyncEventArgs. however, this is a waste of memory because not all messages have the maximum length. allocate a proper buffer size to SocketAsyncEventArgs, provide calls through the pool, flexibly write messages to one or more SocketAsyncEventArgs, or store multiple messages to one SocketAsyncEventArgs for processing.

Queue

 

We can see that many methods are to directly open a thread or throw it to the thread pool after receiving data. This method is very bad because it cannot better control the thread's work, including the thread's waiting. with the custom thread + queue, you can control the number of threads in charge of the work, and the work in the queue will only exist in the queue; there will not be a large number of threads or a large number of thread waits, which will lead to resource loss caused by the operating system's thread scheduling.

Delayed data merging

 

Delayed Merge data transmission is a method to solve too many network I/O operations. This method is not used in many scenarios, but is common in game servers. someone asked me a question. If there are 400 users in the scenario, every user's environment change will tell other users. if combined data is not used for sending, network I/O operations will be very scary, which is difficult for the system to carry. therefore, it is necessary to merge and send data for the current application within the appropriate latency range.

 

Author smark

Related Article

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.