This paper is suitable for reading for junior High school readers
Sometimes, spend a few hours to read, debug, track the excellent source code program, can quickly grasp some key points and the essence of technology. Of course, the premise is that there is a general understanding of these technologies.
I through several use CSocket class to write and based on client/server (client/server) network chat and Transfer files program (see: Source code Reference), in the process of debugging these programs, tracking depth to the CSocket core source Sockcore.cpp , the operation mechanism of the CSocket class is glance, and it is a little experience to write the socket program under blocking and non-blocking mode.
To read this article, please note first:
The blocking and non-blocking concepts here apply only to the Server-side socket program. Socket meaning socket, which is different from the socket, please note the case of the first letter.
Client and server communication is simple: the server socket is responsible for listening, answering, receiving and sending messages, while the client socket is only connected, answering, receiving, sending messages. In addition, if you are not familiar with the principle of writing Client/server Web programs using the CSocket class, please check it first (see: Reference books and online Help).
Before this, it is necessary to tell first: network transport service providers, Ws2_32.dll, socket events and Socket window.
1, network transport service provider (Network transport service process), socket event, Socket Window
The Network transport service provider (transport service provider) exists as a DLL and is loaded by the service process Svchost.exe when the Windows operating system starts. When the socket is created, the API function socket is invoked (in Ws2_32.dll), and the socket function passes three arguments: The address family, the socket type (note 2), and the Protocol, which determines which type of network transport service provider to start the network Transport service functionality. All network traffic is done by the network transport service provider, where the network transport service provider is referred to as a network transport service process that is more understandable, as it is mentioned earlier that the network transport service provider is loaded by the Svchost.exe service process.
The following figure describes the interface hierarchy between network applications, CSocket (WSock32.dll), the Socket API (Ws2_32.dll), and network transport service processes:
When Client-side sockets communicate with the Server-side socket, the socket event is triggered at both ends. Here is only a brief description of the two socket events:
Fd_connect: Connection event, usually triggered when client socket calls socket API function Connect, this event occurs on the client side.
Fd_accept: The connection event being introduced, typically the server socket is being triggered when it is receiving a connection from a client-side socket, and this event occurs on the server side.
The network transport service process saves the socket event to the event queue in the socket. In addition, the network transport service process sends a message wm_socket_notify to the socket window, notifying you of the occurrence of the socket event, as detailed below for the socket window.
After calling the Csocket::create function, the socket is created. Casyncsocket::attachhandle (socket hsocket, casyncsocket* psocket, BOOL Bdead) is invoked during the socket creation process. The function is:
Add the socket instance handle and the socket pointer to a mapping table variable m_pmapsockethandle of the current module state (note 1).
In the Attachhandle process, a Csocketwnd instance (based on CWnd) is introduced, which is referred to as the socket window and further understood as a message pool (window message) that holds all sockets. , please look carefully and add an S to the socket, which means that multiple sockets created will share a message pool.
When the client-side socket communicates with the server end, the network transport service process sends a message wm_socket_notify to the socket window, which needs to be explained by the Csocketwnd window handle saved in the current module state. In the M_hsocketwindow variable.