14. Network programming
The seven-layer network structure: application layer, presentation layer, session layer, transport layer, network layer, data link layer, physical layer
Seven-layer correlation: the upper layer depends on the lower layer, and the upper layer uses the services provided by the lower layer.
Client server mode: the client actively requests, the server waits for the request, and the server starts first.
Server Socket establishment process: 1.Socket 2.bind 3.listen 4.accept 5.Send/Resv
The client establishes the Socket process: 1.Socket 2.Connect 3.Send/Resv
16. Thread synchronization and asynchronous socket programming
Thread synchronization: events, critical sections, mutexes
Events: kernel-mode thread locks divided into manual reset and automatic reset.
Critical section: thread lock in user mode.
Mutex: A thread lock in kernel mode.
Asynchronous socket principle: Based on the further encapsulation of Socket, the non-blocking asynchronous mode is used for communication.
MFC函数:WSAAsyncSelect(...);WSARecvFrom(...);WSASendTo(...);
17. Interprocess communication
Inter-process communication methods: shared memory, message queues, pipes, sockets
1. Shared memory (clipboard)
Principle: A block of memory is allocated by the kernel, which can be accessed by any process in the system.
Advantages: 1. High efficiency. 2. Can be accessed by multiple processes at the same time
Disadvantages: 1. It must be locked to prevent one process from reading while another process writes. 2. To apply conditional restrictions, the two processes must be under the same host.
MFC相关函数:OpenClipBoard(...);CloseClipBoard(...);EmptyClipBoard(...);SetClipBoard(...);GetClipBoard(...);IsClipBoardFormatAviable(...);GlobalLock(...)lGlobalUnlock(...);
2. Pipes
Pipeline principle: Byte stream communication, which connects the standard output of one process with the standard input of another process. It is equivalent to the process of indirectly operating the memory area maintained by the kernel by doing byte streams.
Features: 1. One to one. 2. One-way
Disadvantages: 1. May cause blocking. 2. Issues related to the opening and closing of pipes.
MFC相关函数:CreatePipe(...);CreateProcess(...);ReadFile(...);CreateNamePipe(...);ConnectNamePipe(...);CreatEvent(...);WaitNamePipe(...);
3. Message queue
Principle: Call a shared buffer independent of the communication process through the process interface to read and write data.
Advantages: 1. There is no limit to the number of processes relative to pipes. 2. Does not block. 3. Message priority can be set.
Disadvantages: 1. Relatively less efficient than memory sharing. 2. Need to lock to prevent read and write synchronization.
Function: generally implemented with third-party libraries, such as ActiveMq