WinSock programming model

Source: Internet
Author: User
Tags microsoft mail

First, you must understand the concepts of synchronization, Asynchronization, blocking, and non-blocking.
Synchronous and asynchronous are the communication modes. Blocking and non-blocking refer to socket I/O operations.
In fact, for socket, There is only blocking and non-blocking, and synchronization and Asynchronization are different in program implementation.
The Recv function is executed in a blocking way. This function does not return data before it receives data. Therefore, it is easy for the thread that executes the function to be in the status of waiting for data on I/O, then it is suspended. Non-blocking is not the same. When executing Recv, no data is returned immediately. When there is data, data is returned. If there is no data, an error is returned. Non-blocking can bring about program efficiency and precautions in writing programs. In non-blocking situations, when sending and receiving data, you must manage your own buffer, in addition, you need to record the sending and receiving locations. Because it is very likely that the task of sending and receiving data cannot be completed at one time, you need to call send and Recv multiple times to complete the task.
Synchronous Asynchronization is used to represent the communication mode. the synchronization of communication mainly refers to the next request sent by the client only after the server responds to the request. Therefore, all requests at this time will be synchronized on the server. Asynchronous communication means that after the client sends a request, it can send the next request without waiting for the response from the server. In this way, all request actions will be asynchronous on the server, the link of this request is like a request queue, and all the actions will not be synchronized here. However, I personally feel that when talking about synchronous and asynchronous socket, the synchronization concept is similar to blocking. The result is returned only when there is a result. The asynchronous mode tells the system that I want to Recv data and then return immediately, after the data arrives, the system tells the program that the data has arrived, and then the program Recv the data. Reference the good description on the Internet: "blocking block means that you call someone's phone number, but this person is not there, so you can wait for him to return, and no longer use the phone number. Synchronization is about the same as blocking. Non-blocking nonblock means that you call someone, but this person is not there, so you can stop the call and call again later. As for the time when he came back, he only had to make a call. This is called "Round Robin/poll ". Asynchronous means that you call someone's phone number, but this person is not there, so you ask the person answering the phone to tell the person (Leave a message) and then call you (call back )."

Obviously, Asynchronization is more efficient. There are many asynchronous methods in Winsock. The following six Winsock working models are available:
I. Select model
Ii. wsaasyncselect Model
Iii. wsaeventselect Model
4. overlapped I/O Event Notification Model
V. overlapped I/O completion routine model
Vi. iocp Model
From the first to the sixth, more advanced, more efficient, and more complex implementation. I have seen some metaphors on the Internet to describe these models very well. I will reference them here.

Old Chen has a daughter who works in a field and cannot come back frequently. She and she contact him by letter. Their mail will be delivered to their mailbox by the postman.
I. Select model

Chen wanted to hear from his daughter. So that he goes downstairs every 10 minutes to check whether he has a daughter's email ~~~~~
In this case, "go downstairs to check the mailbox" and return to the upstairs, which delayed Lao Chen too much time, so that he could not do other work.

Ii. wsaasyncselect Model

Later, Chen used a new Microsoft mailbox. This kind of mailbox is very advanced. Once there are new letters in the mailbox, Gates will call Chen: Hello, Lord, you have new letters! From then on, Old Chen no longer has to go up and down frequently to check the mailbox, and his teeth do not hurt. You can look at it, blue sky ...... No, Microsoft ~~~~~~~~

Iii. wsaeventselect Model

Later, Microsoft's mailbox became very popular. The number of people who bought Microsoft Mail was counted as one million ...... as a result, Gates calls the customer 24 hours a day, causing a sore waist and backache ~~~~~~
Microsoft improved their mailbox: by adding an additional device to the customer's home, the device monitors the customer's mailbox, whenever new mail comes, this device will send a "new letter arrives" to remind old Chen to receive the mail. Gates can finally go to bed.

4. overlapped I/O Event Notification Model

Later, Microsoft found through a survey that Chen did not like sending and receiving letters from the upper and lower floors, because the upper and lower floors were a waste of time. So Microsoft improved their mailbox again. The new mailbox uses more advanced technologies. As long as the user tells Microsoft the number of its home on the floor, the new mailbox will send the mail directly to the user's home and then tell the user, your letter has been placed in your home! Old Chen is very happy because he no longer needs to send and receive emails in person!

V. overlapped I/O completion routine model

After Chen receives a new letter, the general procedure is: open the envelope, pull out the letter paper, read the letter, and reply to the letter ...... to further reduce the burden on users, Microsoft has developed a new technology: as long as you tell Microsoft the operation steps on the mail, Microsoft Mail will follow these steps to process the mail, you no longer need to personally split/read/reply! Chen has finally lived a petty asset!

Vi. iocp Model

Microsoft mailbox seems perfect, and Chen is also very satisfied. But in some big companies, the situation is completely different! These large companies have tens of thousands of mailboxes, and hundreds of mails need to be processed every second, so that Microsoft mailboxes often crash due to overload! Need to restart! Microsoft had to launch a killer ......
Microsoft assigned a super robot named "completion port" to every big company to process those letters!

In fact, each of the above models has advantages. to select an appropriate model based on program requirements, the efficiency of the first three models is already relatively high. Isn't it very easy to implement, many general network programs use the first three models. Only servers with high network requirements will consider the models below. The casyncsocket class in MFC uses the wsaasyncselect model, which is also used in the plug-in. However, it is optimized when looking for the corresponding socket to make the search faster. In gridcast, The wsaeventselect model is used, wait.

BTW: As mentioned above, only Winsock has so many models. In Linux, it seems that there is only the first select mode. I don't know much about socket in Linux, there should also be a lot of improvements to efficiency.
 

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.