The difference between blocking and non-blocking sockets on send and receive from the buffer

Source: Internet
Author: User

Recently on the network to see some posts and replies, while searching for some network on the blocking non-blocking differences in the description, found that many people in describing the sending and receiving operation return and the difference between buffer processing has different degrees of misunderstanding. So I would like to write an article to correct the error, and as a record for easy access, if reproduced, the author (jwybobo2007) and the source can be.

First the socket is blocked by default (non-asynchronous operation and some other special purpose, the direct default is nonblocking), which makes the send and receive operations in a blocked state, that is, the call does not return immediately, but into the sleep wait operation completed. The following points divide the discussion into sending and receiving.

one. Send select Send (here specifically TCP) and sendto (here specifically UDP) to describe

The first thing to note is that, regardless of blocking or non-blocking, data is copied from the application buffer to the kernel buffer (so_rcvbuf option declaration, unless the buffer size is 0) when it is sent. I saw some people on the web saying that blocking is the actual sending of data to each other, and that blocking occurs when you need to send all the data in front of it, and then send it again, instead of blocking the copy to the send buffer. I have to say that the above statement is wrong.

in blocking mode, the send operation waits for all data to be copied to the send buffer before it is returned.

If the current send buffer total size is 8192, has been copied to the buffered data is 8000, the remaining size is 192, now need to send 2000 bytes of data, the blocking send will wait for the buffer enough to copy all 2000 bytes of data, such as the first copy into 192 bytes, When the buffer has successfully sent 1808 bytes, then the remaining 1808 bytes of the application buffer are copied to the kernel buffer, and the send operation returns the number of bytes successfully sent.

From the above process it is not difficult to see that the blocked send operation returns the size of the send, which is necessarily the size of the send length in your parameters.

The sendto operation in blocking mode is not blocked.

The reason for this is that UDP does not have a real send buffer, it does just copy the application buffer to the lower stack, in the process of adding the UDP header, IP header, so there is no actual blocking.

in non-blocking mode, the send operation call returns immediately.

There is no objection to the immediate return of the people. Or take the example of blocking send, when the buffer is only 192 bytes, but it needs to send 2000 bytes, the call returns immediately and the return value is 192. As you can see, non-blocking send is just doing its best to copy as much data as possible to the buffer, so it is possible for a non-blocking send to return a value that is smaller than the sending length in your parameters.

What if the buffer does not have any space? It must also return immediately, but you will get wsaewouldblock/e wouldblock error, which means you can't copy any data to the buffer, you'd better take a break and try to send it again.

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

two. Receive selected recv (specifically TCP) and recvfrom (here specifically UDP) to describe

recv,recvfrom operation in blocking mode will block there is at least one byte (TCP) or a full UDP datagram in the buffer to return.

When no data arrives, calls to them will be in sleep and will not be returned.

in non-blocking mode, the Recv,recvfrom operation will return immediately.

If the buffer has any one byte data (TCP) or a full UDP datagram, they will return the received data size. The error wsaewouldblock/e Wouldblock is returned if there is no data .

The above is about the difference between blocking non-blocking send receive and the difference in buffer processing, and I hope to give some help to the people who see this article. At the same time also want to correct some of the wrong views on the network, the article expressed in error, I hope you correct, thank you.

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

The difference between blocking and non-blocking sockets on send and receive from the buffer

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.