recv function
int recv (SOCKET s, char FAR *buf, int len, int flags);
Both the client and the server application receive data from the other end of the TCP connection using the Recv function. The first parameter of the function specifies the receive-end socket descriptor;
The second parameter indicates a buffer for storing the data received by the RECV function;
The third parameter indicates the length of the BUF;
The fourth parameter is generally set to 0.
This only describes the execution flow of the recv function of the synchronous socket. When the application calls the Recv function,
(1) Recv waits for the data in the send buffer of s to be transmitted by the Protocol, if the Protocol has a network error when transmitting the data in the sending buffer of S, then the RECV function returns SOCKET_ERROR,
(2) If there is no data in the send buffer of s or the data is successfully sent by the protocol, recv first checks the socket s receive buffer, if the s receive buffer has no data or the protocol is receiving the data, then recv waits until the agreement has received the data. When the protocol has received the data, the RECV function will copy the data from the receive buffer of S to the BUF (note that the protocol receives more data than the BUF length, so it is necessary to invoke several recv functions in this case to copy the data in the receive buffer of S. The recv function is just copy data, and the real receiving data is protocol to complete it.
The
recv function returns the number of bytes of its actual copy. If recv fails at copy, it returns SOCKET_ERROR, which returns 0 if the Recv function is interrupted while waiting for the protocol to receive data.
The default socket is blocked
unblock and non-blocking recv return value does not distinguish between <0 error = 0 Connection shutdown >0 received data size,
Special: Return When the value is <0 and (errno = = Eintr | | | errno = = Ewouldblock | | errno = = eagain) The connection is considered normal and continues to receive. In blocking mode, recv is blocked from receiving data, and if no data is returned in non-blocking mode, it will not block read and therefore need to be read in a circular way.
Return Description:
Returns the number of bytes received when it is successfully executed. The other end is closed and returns 0. Failure returns -1,errno is set to one of the following values
eagain : Socket marked as non-blocking, and receive operation blocked or receive timeout
Ebadf:sock is not a valid descriptive word
Econnrefuse: remote host blocking network connection
Efault: Memory space Access error
Eintr: Operation interrupted by signal
Einval: Invalid parameter
Enomem: Out of Memory
Enotconn: The socket associated with the connection is not already connected
Enotsock:sock index is not a socket
when the return value is 0 o'clock for a normal shutdown connection;
thinking: When the opposite side does not send, That is, this side of the socket s receive buffer no data, what the return value is (Eagain, due to timeout, to be measured)