The difference between blocking and non-blocking socket in sending and receiving from buffering (reprinted)

Source: Internet
Author: User
From: http://blog.chinaunix.net/uid-24517549-id-4044877.html first socket is blocked by default, which makes the sending and receiving operations in the blocking state, that is, the call does not return immediately, but enters the sleep waiting for the Operation to complete.

1. Select send (TCP in this example) and sendto (UDP in this example) to describe sending.

The first thing to note is that no matter whether it is blocked or not blocked, data will be copied from the application buffer to the kernel buffer during sending (so_rcvbuf option declaration, unless the buffer size is 0 ).

Send in blocking modeThe operation will not be returned until all data is copied to the sending buffer.

If the total size of the current sending buffer is 8192 and the data copied to the buffer is 8000, the remaining size is 192. Now we need to send 2000 bytes of data, blocking transmission will wait for the buffer to copy all 2000 bytes of data. For example, if the buffer is copied to 192 bytes for the first time, after the buffer is successfully sent to 1808 bytes, copy the remaining 1808 bytes of the application buffer to the kernel buffer, and then the send operation returns the number of successfully sent bytes.

From the process above, it is not difficult to see that the size of the message returned by the blocked send operation must be the size of the sending length in your parameter.

Sendto in blocking modeThe operation will not be blocked.

The reason for this is that UDP does not have a real sending buffer. All it does is copy the application buffer to the lower-layer protocol stack. In this process, add the UDP header and IP header, so there is no actual blocking.

Send in non-blocking modeThe operation call will return immediately.

No objection is raised to the immediate return. Let's look at the example of blocking send. When the buffer zone only has 192 bytes but needs to send 2000 bytes, the call will return immediately and the returned value will be 192. As you can see, non-blocking send only copies as much data as possible to the buffer. Therefore, in non-blocking mode, sending may return a value smaller than the sending length in your parameter.

What if there is no space in the buffer zone? At this time, it must be returned immediately, but you will get the wsaewouldblock/e wouldblock error, which means you cannot copy any data to the buffer zone. You 'd better take a break and try again.

Sendto in non-blocking modeThe operation will not be blocked (the same as blocking, not described ).

2. Recv (TCP here) and recvfrom (UDP here) are used for receiving.

In blocking mode, Recv, recvfromThe Operation will block at least one byte (TCP) in the buffer or a complete UDP datagram to return.

When no data arrives, all calls to the data will be in sleep state and will not be returned.

In non-blocking mode, Recv, recvfromThe operation will be returned immediately.

If the buffer zone has any byte data (TCP) or a complete UDP datagram, they will return the size of the received data. If no data exists, the system returns the wsaewouldblock/e wouldblock error.

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.