Discussion on the winsocket Model

Source: Internet
Author: User

There are two socket modes: blocking and non-blocking.

If blocking is used, if a network problem occurs at a certain point in time, when you call Recv to receive data, it will causeProgramHanging there! There are two solutions to this problem: one is to ensure that the environment in use will never be like this, and the other is to use multithreading (even if the thread is blocked, ).

If a non-blocking socket is used, we need to continuously check whether the Recv/send function is successful. If the function fails, we adopt the wait or other policies, this round robin method is not good for any program (inefficient, unless you have ). Think about it. It should be windows to determine when something is retained and writeable !! Winsock is a network programming interface provided by Ms. When something needs to be received, the underlying driver of MS is the clearest (when it can be sent is also the clearest for MS ), so we have the socket model for windows! When we call select, Ms is actually used to help us choose when it should be able to send and receive, rather than blindly round-robin. Metaphor: it is like having a mailbox. It is not what you know most about credit, but what the postman knows most about. If you keep running to view the mailbox, your efficiency is definitely very low (You are like a CPU ). However, if you change to a postman, give you a minute, and then you get the mail again, the efficiency will be much higher !! You can do other things at other times. The postman is the select function provided by Ms !!

However, in our program, we need to call the select function cyclically, but this loop ensures efficiency, because we can only do this when there is an event, and we will not move it when there is no event. So we can use a thread for this loop, so we don't have to worry that this thread will occupy our CPU time.

In fact, the principle of several socket models is the same, that is, when an event is coming, and the work is started !! This event is notified to you by Ms !! Think about it. Isn't wsaasyncselect notifying you through the message mechanism in Windows when Ms detects something to do? Wsaeventselect notifies you by means of events, and overlapping IO does not provide events and completion routines (when something can be done, I will call your completion routine !) ? The completion port can be used to open a thread based on the number of CPUs (so it is scalable). It notifies you by calling getqueuedcompletionstatus! Isn't the same as select?

To achieve the purpose of notifying you, windows must provide you with some interfaces and comply with certain rules. As for whether it is thread-based polling or event-based, or something I can't understand.AlgorithmWait, we don't have to worry about it!

Let's just talk about the following:ArticleWill be further explored!

Discussion on the winsocket model -- select model

Winsocket model -- wsaasyncselect Model

Winsocket model -- wsaeventselect Model

Winsocket model-overlapping Io Model

Explore the winsocket model-complete the port model

 

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.