The non-blocking mode of a socket refers to the working mode in which functions called by the socket are immediately returned no matter whether the operation is completed or not. Non-blocking sockets have obvious advantages in processing multiple connections established at the same time, such as unevenly distributed data volume and time uncertainty. However, this socket is difficult to use.
1. Non-Blocking Mode
Set the socket to non-blocking mode, that is, to notify the kernel:When calling the Windows Sockets API, do not let the thread sleep, but let the function return immediately. The function returns an error.Code. Shows how a non-blocking mode socket calls the Recv () function multiple times. Kernel data is not ready when the Recv () function is called three times before. Therefore, this function immediately returns the wsaewouldblock error code. When the Recv () function is called 4th times, the data is ready and copied to the application.ProgramThe Recv () function returns a success indicator, and the application starts to process data.
2. Set the non-blocking mode of the socket
When you use the socket () function and wsasocket () function to create a socket, it is blocked by default. After creating a socket, call the ioctlsocket () function to set the socket to non-blocking mode. The first parameter of the function is socket, the second parameter is set to fionbio, and the second parameter is set to a non-zero value of the unsigned long type. The following code lists how to use the ioctlsocket () function to set the socket to non-blocking mode.
(Non-blocking model)
(This chapter contains a lot of content, which can be further discussed after being thoroughly mastered ......)