Socket buffer

1. TCP socket receive and transmit buffer:
Socket (PF_INET, SOCK_STREAM, 0);
each of the Socket has a TCP transmission buffer and a receive buffer in the kernel, TCP full duplex operating mode and a sliding of TCP window is dependent on two separate buffer and the buffer fill state.

When the application calls the write () or send (), only the data in the application buffer copy to the socket transmit buffer (write () or send () return, data is not sent to the peer must have a data transmission), at the appropriate time, the kernel will send socket buffer to the recipient socket receive buffer.

When the application calls the recipient read () or receve (), simply copy the data receiving buffer to the application buffer from the socket in the recipient. If the application has been no call to read () to read, then the data will always receive buffer cache in the corresponding socket.

If the sending end when particularly fast, the buffer is filled quickly (socket default is 1024 × 8 = 8192 bytes), this time we should set the buffer size according to the situation, it can be achieved by setsockopt function. Check the size of the buffer by getsockopt function.

getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcv_size, &optlen);
setsockopt(s,SOL_SOCKET,SO_RCVBUF, (char *)&rcv_size, optlen);

If the application process has not been read, after the buffer is full, the action happens is: to inform the peer TCP protocol window closes. This is the realization of a sliding window. That the TCP socket receive buffer does not overflow, thus ensuring a reliable TCP transport. Because the other data sent over the advertised window size does not allow. This is the TCP flow control, and if the other party ignores the window size and window size sent over the data, the receiving TCP discards it.

2. UDP's receive buffer:
each UDP Socket has a receive buffer, there is no transmit buffer. Send data directly, regardless of whether the other party can receive correct, regardless of whether the receiving end buffer is full.
UDP is no flow control; fast sender can easily drown slow receiver, the receiver discards lead UDP datagram.

Guess you like

Origin www.cnblogs.com/linguoguo/p/12324381.html