6 Types of IO models under Windows

Source: Internet
Author: User

Blocking Model
int recv (
SOCKET S,
char* buf,
int Len,
int flags
);
int Send (
SOCKET S,
Const char* BUF,
int Len,
int flags
);

This way is most familiar, the socket default is blocking mode.

In recv, the socket blocks there until the data is readable on the connection, and the Recv function returns after reading the data into buffer, otherwise it will remain blocked there.

If the main thread is blocked, and the data is delayed, then the program will be locked. Such problems can be solved with multithreading, but in the case of multiple socket connections, this is not a good choice, extensibility is poor, but also easy to have a lock problem. Too many threads also cause the context switch to become too frequent, causing the system to slow down, and most of the threads are inactive, which wastes the system's resources.

set to non-blocking:
int ioctlsocket (
In SOCKET S,
In Long cmd,
In Out u_long FAR * ARGP
);
#define FIONBIO/* set/clear non-blocking I/o */

Call the Ioctlsocket function to set Fionbio to 1 to non-blocking mode.

When the recv and send functions are not ready for data, the function does not block, immediately returns an error value, and the error code returned with GetLastError is Wsaewouldblock, which is interpreted as "an operation that cannot be completed immediately with a non-blocking socket."

Of course, you can use non-blocking analog blocking mode, which is to use the while loop to keep calling recv until Recv returns successfully. This is not efficient, but the advantage is that you can do other things when you don't receive the data, or sleep directly.

Set the socket to non-blocking mode, which tells the system: when calling the Windows Socket API, do not let the keynote thread sleep, but let the function return immediately. For example, when calling the Recv function, even if the accept buffer does not have data at this time, it will not cause the thread to wait at recv, and the RECV function will return immediately. If the successful function is not called, the Wsaerouldblock error code is returned. In order to receive data, the recv must be looped round, which is also the main difference between nonblocking and blocking patterns.

unsigned long ul=1;    int ret;    S=socket (af_inet,sock_stream,0);    Ret=ioctlsocket (S,fionbio, (unsigned long) &ul);//set to non-blocking mode.    if (ret==socket_error)//Setup failed.    {    }  

  

6 Types of IO models under Windows

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.