Socket blocking and non-blocking send/recv__ networks

Source: Internet
Author: User
Tags socket blocking

Let's take a look at the concept of blocking and non-blocking:

Blocking is the thread function that keeps the current calling thread in the middle of a stop waiting, pending state.

Non-blocking is no matter how the results of the operation will continue to execute (often have to deal with many return results), the thread function is generally a loop, non-stop polling.


Just a second. Send receive function:

send/sendto function, just copy the application layer of data to the kernel send buffer, does not guarantee that the data will be sent to the right side, the actual delivery and when the delivery is determined by the System (protocol stack), so the Send/sendto function return success, can only explain the success of the copy, If the network is disconnected before it is sent, the send fails.

The Recv/recvform function, which copies the data from the kernel to the buffer in the application layer, is determined by the system layer for the true execution of the receiving data.


The socket is blocked by default, so the send and receive are blocked, so the call does not return immediately, but enters the sleep waiting operation to complete.

First, send/sendto operation

1. the send operation in blocking mode will wait until all data is copied to the send buffer before returning

If the send buffer can be 0 or smaller than the length of the data to be sent, it will block until the data in the send buffer is sent by the system and the available buffer size is larger than the length of the data being sent, the send return succeeds or the wait is blocked. As a result, send returns the size of the delivery, must be the size of the send in your parameters.

2. sendto operation is not blocked in blocking mode

UDP does not have a true sense of the sending buffer, it is only to the application layer of the buffer data copy to the underlying protocol stack, in this process with UDP headers, IP headers, so there is no blocking

3. send operation returns immediately in non-blocking mode

If the send buffer has a usable size of 0, the Ewouldblock error is returned immediately, indicating that no data can be copied to the send buffer, and if the send buffer is not 0 available, but less than the length of the sent data, copy the available size data to the buffer; Non-blocking send always does its best to send buffer copies as much data as possible, so the presence of non-blocking send returns is smaller than the length of the data being sent.

4. SendTo operation is not blocked in non-blocking mode

Roughly the same as in blocking mode, and will not be blocked


Second, recv/recvfrom operation

1. in blocking mode, Recv/recvfrom blocks into the receiving buffer with a byte or a full UDP datagram, and then returns

RECV prototype: int recv (SOCKET sd, char *buffer, int len, int flag), noting that the system does not wait for the buffer to be filled up and return, but once the data has been received, it returns immediately, So do not expect the actual length of the data received to be equal to Len.

2. in non-blocking mode, Recv/recvfrom will return immediately

If the receive buffer has at least one byte or UDP datagram, the received data size is returned, and if not, an error is returned Ewouldblock

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.