Socket mode-development in non-blocking mode (1)

Source: Internet
Author: User

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 ......)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.