When it comes to socket programming, most people raise their hands c,c++. Unfortunately, there is no chance to follow and reach the level of the write server, so you will still consider the C # version of the socket server.
After a query, trial. Some of the data and facts surfaced, as well as the C # version of the server has a lot of confidence, the following slowly listed.
Basic knowledge:
1, first socket by platform to divide there are two kinds of is originated from UNIX and from the Microsoft platform Winsock, sentence data show, Winsock reference UNIX socket.
2, there are two types of programming patterns, synchronous and asynchronous
3, according to the TCP protocol, there are short links, long links.
In Unix, the socket is also referred to as a network file descriptor, which can be treated as a special I/O handle under Window, which are system resources. Each request from the network requires the socket resource to be processed. Increasing the utilization of these resources achieves the goal of improving server performance. The guiding ideology is: async, reuse, multi-threading, thread pooling. And the fusion of these ideas is the IOCP model.
Borrow this buddy's research to introduce the next IOCP
1) Advantages of using IOCP model programming
① helps maintain a pool of reused memory. (related to overlapped I/O technology)
② Remove thread creation/termination burden.
③ facilitates management, allocates threads, controls concurrency, and minimizes thread context switching.
④ optimizes thread scheduling to increase the hit rate of CPU and memory buffers.
2) The knowledge points to be programmed using the IOCP model (no sequencing)
① Synchronous vs. asynchronous
② blocking and non-blocking
③ overlapped I/O technology
④ Multithreading
⑤ Stack, queue these two basic data structures
. NET Socket Programming:
Back to. Net,win32 There is a technique called Overlapped I/O, which is interpreted as: Overlapped I/O is a technology of WIN32, you can ask the operating system to send data to you and notify you when the transmission is complete. This technology allows your program to continue to process transactions while I/O is in progress. In fact, it is the thread I/O that completes the overlapped I/O within the operating system. You can gain all the benefits of a thread without paying any painful costs. In other words, overlapped is primarily about setting up asynchronous I/O operations, where an application can read or write data in the background and do other things in the foreground. OVERLAPPED is a struct under winddows, which is different in c,c++ and. Net. The Overlapped object in the CLR can effectively encapsulate a native Windows Overlapped structure for managing asynchronous I/O operations. Each in-progress Socket asynchronous I/O operation has a Overlapped object instance. In an MSDN Magazine, Xuancheng starts with 3.5, you can have 60,000 or more connection sockets, and at the same time maintain a pending asynchronous receive I/O operation on each socket.
After the basics have been given, let's take a look at the official instructions:
The 2.0 version of the Socket class uses the Windows I/O completion port to complete asynchronous I/O operations. This allows applications to easily scale to a large number of open sockets. The. NET Framework implements the System.Threading.ThreadPool class, which provides a completion thread that reads the completion port and completes the asynchronous I/O operation. In developing the upcoming release of the 3.5. NET Framework, we put a lot of effort into eliminating the overhead in the code path, including reading the completion port and invoking the application's completion agent or signaling the I/O completion event object in the IAsyncResult object. The APM in the NET Framework is also known as Begin/end mode. This is because the Begin method is called to start an asynchronous operation and then return a IAsyncResult object. You can choose to provide a proxy as a parameter to the Begin method, which is called when the asynchronous operation completes. Alternatively, a thread can wait for iasyncresult.asyncwaithandle. When a callback is called or a wait signal is signaled, the end method is called to get the result of the asynchronous operation. This model is flexible and relatively simple to use, and is very common in the. NET Framework. However, you must be aware that there is a price to pay for a large number of asynchronous socket operations. For each operation, a IAsyncResult object must be created, and the object cannot be reused. This can affect performance because of the large use of object allocation and garbage collection. To solve this problem, the new version provides another way to use the socket to perform asynchronous I/O mode. This new pattern does not require that an action context object be assigned to each socket operation. Instead of creating a completely new pattern, we just took the existing pattern and made a basic change. Now, there are methods in the Socket class that use variations of the event-based completion model. In version 2.0, you can use the following code to initiate an asynchronous send operation on a socket:
void Onsendcompletion (IAsyncResult ar) {}iasyncresult AR = socket. BeginSend (buffer, 0, buffer.) Length, Socketflags.none, onsendcompletion, state);
In the new version, you can also implement:
void Onsendcompletion (Object src, SocketAsyncEventArgs SAE) {}socketasynceventargs SAE = new SocketAsyncEventArgs (); SAE . Completed + = Onsendcompletion;sae. SetBuffer (buffer, 0, buffer.) Length); socket. SendAsync (SAE);
There are some obvious differences here. Encapsulates an SocketAsyncEventArgs object, not a IAsyncResult object, in the context of an operation. The application creates and manages (and can even reuse) SocketAsyncEventArgs objects. All parameters of a socket operation are specified by the properties and methods of the SocketAsyncEventArgs object. The completion state is also provided by the properties of the SocketAsyncEventArgs object. Finally, you need to use the event handler callback completion method. Based on the above basics, feel confident to use the C # version of the socket server. At present, SuperSocket has been selected, the use of simple, the official list of more famous users. Blog Park seems to have someone recommend their company's products. Looks like it's not good. Off-topic, will write c,c++ socket server really cool Ah, have headhunting out of the position of more than 40w, unfortunately not competent.
why. Net socket server programming is also efficient