socket buffer and blocking mode

socket buffer

After each socket is created, two buffers are assigned, the input buffer and output buffer.

write () / send () in the network does not transmit data immediately, but writes data to the buffer, and then transmits the data from the buffer by the TCP protocol to the target machine. Once the data is written to the buffer, the function can return success, regardless of whether they have not reached the target machine, no matter when they are sent to the network, these are the TCP protocol is responsible for what.

TCP protocol independent write () / send () function, data may be sent to the buffer just written to the network, it may continue to backlog in the buffer, data is written many times sent to the network only once, which depending on prevailing network conditions, the current thread is idle and many other factors, not by the programmer to control.

read () / recv () function is true, read the data from the input buffer, instead of reading directly from the network.


FIG: TCP socket I / O buffers schematic


These I / O buffers characteristics may be summarized as follows:

  • I / O buffer present in each individual TCP socket;
  • I / O buffer is automatically generated when creating the socket;
  • Close the socket will continue to transmit even when the remaining buffer data output;
  • Closing the socket will lose the input data buffer.


The default size of the input and output buffers are generally 8K, the function can be obtained by getsockopt ():

  1. unsigned optVal;
  2. int optLen sizeof(int);
  3. getsockopt(servSock, SOL_SOCKET, SO_SNDBUF(char*)&optVal&optLen);
  4. printf("Buffer length: %d\n", optVal);

Operating results:
Buffer length: 8192

Here are examples only, will be explained in detail later.

Blocking mode

For a TCP socket (by default), when a write () / send () to send data:
1) first checks the buffer, if the data is less than the length of buffer space available to be transmitted, then the write () / send () will be blocked (suspended) until the data buffer is sent to the target machine, free up enough space, only to wake write () / send () function continues to write data.

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

3) If the data to be written is greater than the maximum length of the buffer, then the batch will be written.

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

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

2) If the data length is less than the length of the data to be read in the buffer, then it can not at one time all data in the buffer is read out, the remaining data will continue to accumulate until there is read () / recv () function is called again take.

3) until the read data read () / recv () function will return, otherwise it has been blocked.

This is the TCP socket blocking mode. The so-called blocking, that is the last step action is not completed, the next move will be suspended until after the step moves to continue to maintain synchronization.

Under TCP socket is in blocking mode by default, it is the most commonly used. Of course, you can also change the non-blocking mode
 

socket buffer

After each socket is created, two buffers are assigned, the input buffer and output buffer.

write () / send () in the network does not transmit data immediately, but writes data to the buffer, and then transmits the data from the buffer by the TCP protocol to the target machine. Once the data is written to the buffer, the function can return success, regardless of whether they have not reached the target machine, no matter when they are sent to the network, these are the TCP protocol is responsible for what.

TCP protocol independent write () / send () function, data may be sent to the buffer just written to the network, it may continue to backlog in the buffer, data is written many times sent to the network only once, which depending on prevailing network conditions, the current thread is idle and many other factors, not by the programmer to control.

read () / recv () function is true, read the data from the input buffer, instead of reading directly from the network.


FIG: TCP socket I / O buffers schematic


These I / O buffers characteristics may be summarized as follows:

  • I / O buffer present in each individual TCP socket;
  • I / O buffer is automatically generated when creating the socket;
  • Close the socket will continue to transmit even when the remaining buffer data output;
  • Closing the socket will lose the input data buffer.


The default size of the input and output buffers are generally 8K, the function can be obtained by getsockopt ():

  1. unsigned optVal;
  2. int optLen sizeof(int);
  3. getsockopt(servSock, SOL_SOCKET, SO_SNDBUF(char*)&optVal&optLen);
  4. printf("Buffer length: %d\n", optVal);

Operating results:
Buffer length: 8192

Here are examples only, will be explained in detail later.

Blocking mode

For a TCP socket (by default), when a write () / send () to send data:
1) first checks the buffer, if the data is less than the length of buffer space available to be transmitted, then the write () / send () will be blocked (suspended) until the data buffer is sent to the target machine, free up enough space, only to wake write () / send () function continues to write data.

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

3) If the data to be written is greater than the maximum length of the buffer, then the batch will be written.

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

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

2) If the data length is less than the length of the data to be read in the buffer, then it can not at one time all data in the buffer is read out, the remaining data will continue to accumulate until there is read () / recv () function is called again take.

3) until the read data read () / recv () function will return, otherwise it has been blocked.

This is the TCP socket blocking mode. The so-called blocking, that is the last step action is not completed, the next move will be suspended until after the step moves to continue to maintain synchronization.

Under TCP socket is in blocking mode by default, it is the most commonly used. Of course, you can also change the non-blocking mode

Guess you like

Origin www.cnblogs.com/gao88/p/12114551.html