Socket buffer and blocking mode __socket (sockets)

Source: Internet
Author: User
In the "Receive and send socket data" section, you can use the Write ()/send () function to send data using the Read ()/recv () function to receive data, and this section will look at how the data is delivered. Socket BufferAfter each socket is created, two buffers are allocated, the input buffer and the output buffer.

Write ()/send () does not immediately transfer data to the network, but instead writes the data to the buffer and sends the data from the buffer to the target machine by the TCP protocol. Once the data is written to the buffer, the function returns successfully, regardless of whether they reach the target machine or when they are sent to the network, which is the responsibility of the TCP protocol.

The TCP protocol is independent of the Write ()/send () function, data may have just been written to the buffer to send to the network, but also may be in the buffer backlog, write data is sent to the network once, depending on the network situation at that time, the current thread is idle and many other factors, not controlled by the programmer.

The Read ()/recv () function also reads data from the input buffer rather than directly from the network.
Diagram: Schematic of I/O buffers for TCP sockets
These I/O buffer attributes can be sorted as follows: The I/O buffer exists separately in each TCP socket, the I/O buffer is generated automatically when the socket is created, and even the closed socket continues to transmit data left in the output buffer; Closing the socket will lose the data in the input buffer.
The default size of the input and output buffers is typically 8K, which can be obtained by using the getsockopt () function:
unsigned optval; int optlen = sizeof (int); GetSockOpt (Servsock, Sol_socket, So_sndbuf, (char*) &optval, &optlen); printf ("Buffer Length:%d\n", optval); Run Result:
Buffer length:8192 here only to show examples, will be explained in detail later. blocking ModeFor a TCP socket (by default) when sending data using write ()/send ():
1 will first check the buffer, if the free space of the buffer is less than the data to be sent, write ()/send () is blocked (paused) until the data in the buffer is sent to the target machine, freeing enough space to wake the Write ()/send () function to continue writing the data.

2 If the TCP protocol is sending data to the network, then the output buffer will be locked and write ()/send () will be blocked until the data is sent over to unlock the buffer, write ()/send () will be awakened.

3 If the data to be written is greater than the maximum length of the buffer, it will be written in batches.

4 until all data is written to the buffer write ()/send () to return.

When reading data using the Read ()/recv ():
1 will first check the buffer, if there is data in the buffer, then read, otherwise the function will be blocked until there is data on the network arrival.

2 If the length of the data to be read is less than the length of the data in the buffer, you cannot read all the data in the buffer at one time, and the remaining data will accumulate until the read ()/recv () function is read again.

3 the Read ()/recv () function is not returned until the data is read, otherwise it is blocked.

This is the blocking mode of the TCP socket. The so-called blocking, that is, the last step is not completed, the next action will be paused until the last step is completed before continuing to maintain synchronization. TCP sockets are blocking mode by default and are most commonly used. Of course you can also change to non-blocking mode, which we will explain later.

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.