Socket is the basis of communication and the basic interface supporting network protocol data communication. Winsocket Provides some interesting I/O Model to facilitate applicationProgramOne "Asynchronous" method is used to manage the communication on one or more sockets at a time. These models include Select (Select ), Wsaasynselect (Asynchronous selection ), Wsaeventselect (Event Selection ), Overlapped I/O (Overlapping I/O ) And Completion port (Complete port ).
① Select model:
The select model is one of the most widely used models in Winsock. The core is the select function, which can be used to determine whether data exists on the socket or whether data can be written to a socket. This function can effectively prevent the application from being blocked when the socket is in blocking mode; at the same time, it can also prevent the generation of a large number of wsaewouldblock error select. The advantage is that it can perform multiple connections and I/O from multiple sockets of a single thread. This avoids the increasing number of threads that are congested with sockets and multiple connections.
② Wsaasyncselect model:
Because it is based on messages, the key is the wsaasyncselect function, which sends socket messages to the hwnd window, and then processes the corresponding fd_read, fd_write and other messages. Advantage: The wsaasyncselect and wsaeventselect models provide asynchronous notifications for reading and writing data, but they do not provide asynchronous data transmission, while overlapping and complete Ports provide asynchronous data transmission. In addition, it can process many connections at the same time with low system overhead, while the select model also needs to establish the fd_set structure. Disadvantage: you must use a window to receive messages. If you process thousands of sockets, you cannot handle them.
③ Wsaeventselect model:
This is also a network event notification based on time, but unlike wsaasyncselect, it is mainly completed by the event object handle, rather than through the window. Advantage: No window is required. Disadvantage: You can only wait for 64 events at a time, so it is necessary to organize a thread pool when processing multiple sockets. Therefore, the scalability is inferior to the subsequent completion port.
④ Overlapping model:
This model enables the program to achieve better system performance. The basic design principle is to allow applications to use overlapping data structures to deliver one or more I/O requests at a time. The application can provide services to these submitted requests after they are completed. It is divided into two implementation methods: Use in events, and complete routines.
⑤ Complete Port:
Port completion provides the best scalability, which can often make the system achieve the best performance. It is the first choice for processing thousands of sockets. Essentially, to complete the port model, you need to create a Windows Port object that manages overlapping I/O requests through a specified number of threads, to provide services for completed overlapping I/O requests.