Linux recv, send blocking, non-blocking differences and usage __linux

Source: Internet
Author: User

Non-blocking IO and blocking IO:

In network programming, the concept of blocking IO and non-blocking IO is encountered for a network handle, and here is a description of the two sockets first:
Basic concepts:

Blocking IO::

The blocking mode of the socket means that IO operations (including errors) must be done before

Return.

Non-blocking IO::

Non-blocking mode returns immediately, regardless of whether the operation is completed, and needs to be passed by other parties

To determine whether a specific operation is successful or not. (For Connect,accpet operations, select to determine,

For Recv,recvfrom,send,sendto to be judged by the return value + error code


IO Mode settings:

SOCKET
How to handle a socket that is blocking or non-blocking mode::

Method::

Use the FCNTL setting; F_GETFL to get flags and set flags| O_nonblock;

At the same time, recv,send is used to read and send messages in a non-blocking manner, that is, flags are set to Msg_dontwait

Realize

The FCNTL function can set a socket handle to a non-blocking mode:
Flags = FCNTL (SOCKFD, F_GETFL, 0); Gets the flags value of the file.

Fcntl (SOCKFD, F_SETFL, Flags |   O_nonblock); Set to non-blocking mode;

Flags = FCNTL (sockfd,f_getfl,0);

Fcntl (Sockfd,f_setfl,flags&~o_nonblock); Set into blocking mode;

And when data is received and sent:

Recv, the last of the Send function has a flag parameter set to Msg_dontwait

recv (SOCKFD, Buff, buff_size,msg_dontwait); Message sending in non-blocking mode

Send (SCOKFD, buff, buff_size, msg_dontwait); Message acceptance in non-blocking mode


Normal file

For file blocking mode or non-blocking mode::

Method 1, open, use O_nonblock;

Method 2, Fcntl settings, using f_setfl,flags| O_nonblock;

Message Queuing

For Message Queuing messages to be sent and accepted::

Non-blocking msgsnd (sockfd,msgbuf,msgsize (does not contain type size), ipc_nowait)

Blocking MSGRCV (scokfd,msgbuf,msgsize), msgtype,ipc_nowait);

Read

The difference between blocking and non-blocking reads://The difference between blocking and non-blocking is whether or not the data is returned immediately when it arrives.

Read (READ/RECV/MSGRCV):

The nature of reading can not actually be read, in practice, the specific received data is not carried out by these calls, is due to the bottom of the system automatically completed. Read, recv is only responsible for the data from the bottom buffer copy to our designated location.

For reading (read, or recv)::

Blocking case::

Under blocking conditions, the behavior of READ/RECV/MSGRCV:

1, if you do not find the data in the network buffer will always wait,

2, when the discovery of data will be read to the user specified buffer, but if this time to read a small amount of data, than the specified length of the parameter is smaller, read will not always wait, but immediately return.

Read's principle:: The data is not more than the specified length of how much to read, no data will be waiting.

So under normal circumstances:: We read the data need to read the data in a circular way, because once read finished can not guarantee that we need to read the length of data,

After reading, you need to judge the length of the data you read before deciding whether to read again.

Non-blocking case::

In the case of non-blocking, Read's behavior::

1, if you find that there is no data directly returned,

2, if found that the data is also used to read how much of the processing.

So:: Read the end of the time to determine the length of the data to decide whether it needs to read again.

For reading: The difference between blocking and non-blocking is whether the data is returned immediately when it arrives.
There is a msg_waitall parameter in the recv::

recv (SOCKFD, Buff, buff_size, Msg_waitall),
Under normal circumstances, recv will wait until the length of the buff_size is read, but the waitall here is just as full as possible, and recv may be interrupted in the event of an interruption, causing the length of the specified buff_size to not be read.

Therefore, even if the use of recv + WaitAll parameters or to consider whether the need to cycle read the problem, in the experiment for most cases recv (using the Msg_waitall) can still read the buff_size,

So the corresponding performance will be better than the direct read for loop reading.

Note:://When using Msg_waitall, SOCKFD must be in blocking mode, otherwise it will not work.

So Msg_waitall can't be used with Msg_nonblock at the same time.

Note that when using Msg_waitall, SOCKFD must be in blocking mode, otherwise WaitAll will not work.

Write

The difference between blocking and non-blocking writing://
Write (SEND/WRITE/MSGSND)::

Writing is not the nature of the send operation, but the user state of the data copy to the bottom of the system, and then by the system to send operations, Send,write return success, only to indicate that the data has been copy to the underlying buffer, and does not mean that the data has been issued, but also can not indicate that the
For write (or send),

Blocking case:://Blocking case, write will send the data out. (but may be interrupted)

In the case of blocking, it will wait until the write is finished, and all the data is returned. This behavior differs from the read operation.

&n

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.