Reproduced: Network programming socket readable and writable condition determination

Transfer: http://blog.csdn.net/majianfei1023/article/details/45788591

 

To understand the conditions socket readable and writable, we first few concepts:
1. The buffer receives the low water mark (for reading) and transmits the low-water mark buffer (for writing):

Each socket has a receiver and a low water level low transmission. They use the select function.

Receiving the low-water mark is to select the amount of data required to return the socket receive buffer when "read." For TCP, the default value is 1.

Send low water mark is to select return the socket send buffer free space is required to "write." For TCP, the default value is usually 2048. (also interview one point)

 

Popular explain that buffer us as a space of size n bytes, then:

Area of the buffer role is received, the received data in the cache area opposite, supply the read program. Of course, only when the data buffer readable (receiving low-water mark) reaches a certain level (eg: 1) time, we can read the data, or data does not yet enrolled.
Cache role is to send area, the application sends data to the buffer, then sent with the opposite. Of course, only if certain remaining buffer space (Send low water mark) (eg: 2048), you can write data into it, or may lead to lack of space.

2.FIN: (end flag, Finish) used to end a call back but the corresponding TCP port is still open, ready to receive subsequent data.

 

socket readable by:

First, if the following four conditions is satisfied by any one, socket ready to read: 
data byte in the receive buffer 1. socket receive buffer is greater than or equal to the low water mark socket of the current size. Such a read operation will not clog the socket and returns a value greater than zero (i.e., return read data ready). We can use SO_RCVLOWATsocket option to set the low-water mark of the socket. For TCP and UDP .socket, the default value is 1.
2. This half of the read-off connection (i.e. TCP connection receives FIN). Such read operation of the socket will not clog and return 0
3.socket is a socket for listening, and the number of connections has been completed in a non-readable 0. Such soocket state, because the other received the connect socket request, performing the first step of the three-way handshake: SYN requests sent over each other, so that the listening socket is in the readable state; normally, this operation on the Accept does not block the socket;
4. there is a socket to be abnormal error condition processing. for such a socket read operation will not block, and returns an error (-1), errno is set to clear the error condition. these errors can also be processed to obtain and clear by specifying the socket option SO_ERROR call getsockopt ;

 

socket writable condition:

Second, if the following three conditions is satisfied by any one, socket is ready to write: 

1. socket data byte transmit buffer in the transmit buffer is greater than equal to the current size of the socket of the low water mark. Write such a socket will not block and returns a value greater than 0 (that is, ready to return data write). We can use SO_SNDLOWAT socket option to set the low-water mark of the socket. For TCP and UDPsocket, it defaults to 2048

2. Write the close connection of this half. Write such a socket will generate SIGPIPE signal, the default behavior of this signal is to terminate the process.

3. The socket has a pending exception error conditions. For such a socket will not block the write operation and return an error (-1), errno is set to a specific error conditions. These errors may be treated by the specified socket options SO_ERROR call getsockopt function to get and clear;

 

Explain connected read / write this half off:

Figure:

 

 

A need to terminate the connection section 4, the active closed end (A) transmits call FIN close to the other end (B), after receiving the FIN B, A has the initiative known closed, i.e., the data A does not occur then this will inevitably end calls read readable, and returns 0 (read returns 0).

original link: https: //blog.csdn.net/majianfei1023/article/details/45788591

Guess you like

Origin www.cnblogs.com/wztshine/p/12088541.html