The difference between blocking and non-blocking sockets in sending and receiving from the perspective of buffering

Source: Internet
Author: User

I recently saw some posts and replies on the Internet, and found some descriptions about the differences between non-blocking and blocking on the Internet, it is found that many people have different misunderstandings when describing the differences between the Operation Return and buffer processing when sending and receiving the two. Therefore, I want to write an article to correct the error and make it easy to read as a record. If any record is reposted, just indicate the author (jwybo2007) and the source.

First, the socket is blocked by default (asynchronous operations and other special uses are not blocked by default), which makes the sending and receiving operations blocked, that is, the call does not return immediately, but goes to sleep and waits for the Operation to complete. The following points are discussed: sending and receiving.

 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 ). Some people on the Internet said that blocking means to actually send data to the other party, and blocking occurs when all the preceding data needs to be sent out, and then this time, instead of blocking, It is copied to the sending buffer. I have to say that the above statement is wrong.

In blocking mode, the send operation will not return 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 successfully sends 1808 bytes of data, 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.

The sendto operation in blocking mode is not 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.

In non-blocking mode, the send 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 getWsaewouldblock/e wouldblockIt indicates that you cannot copy any data to the buffer zone. You 'd better take a break and try again.

In non-blocking mode, the sendto operation is not blocked (consistent with blocking, not described ).

 

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

In blocking mode, the recvfrom operation will be blocked.
At least one byte (TCP) or one complete UDP datagram in the buffer zone is returned.

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

In non-blocking mode, the recvfrom operation returns immediately.

If the Buffer Zone
Any byte data (TCP) or a complete UDP datagram will return the size of the received data. If no data exists, an error is returned.
Wsaewouldblock/e wouldblock.

 

The above is about the difference in blocking non-blocking sending and receiving as well as the difference in buffer processing. I hope to help anyone who can see this article. At the same time, I also want to correct some incorrect ideas on the Internet. If any mistakes are stated in this article, I hope you can correct them. Thank you.

 

 

Reprinted by: jwybobo2007

Address: http://blog.csdn.net/jwybobo2007/archive/2011/01/26/6164362.aspx


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.