Original address: http://blog.csdn.net/a4150902/article/details/7399578
One, int recv (SOCKET s, char far *buf, int len, int flags);
Both the client and server applications use the RECV function to receive data from the other end of the TCP connection.
The first parameter of the function specifies a receive-side socket descriptor;
The second parameter indicates a buffer, which is used to hold the data received by the RECV function;
The third parameter indicates the length of the BUF;
The fourth parameter is generally 0.
This describes only the execution flow of the recv function that synchronizes the socket. When the application calls the Recv function, recv waits for the data in the send buffer of s to be passed by the protocol, and if the protocol has a network error while transmitting the data in the send buffer of S, then the RECV function returns SOCKET_ERROR, If there is no data in the transmit buffer of s or after the data has been successfully sent by the protocol, RECV checks the socket s receive buffer, if there is no data in the s receive buffer or the protocol is receiving data, then recv waits until the protocol receives the data. When the protocol takes over the data, the RECV function will copy the data from the receive buffer of S to BUF ( Note that the data received by the Protocol may be greater than the length of the buf, so in this case a few recv functions are called to copy the data from the receive buffer of S. The recv function is just copy data, and the actual receive data is the protocol to complete ), the RECV function returns the number of bytes of its actual copy. If recv errors in copy, it returns SOCKET_ERROR, and if the recv function waits for the protocol to receive data, the network is interrupted, it returns 0.
Note: Under UNIX systems, if the RECV function waits for the protocol to receive data when the network is disconnected, the process that calls Recv receives a sigpipe signal, and the process terminates the default processing for that signal.
Two, once my Rcv function is this. Call MSG_RCV (socket) in the main function; And my MSG_RCV (Socket,char buf[]), so that when I call recv (socket,buf,0), a return value of-1 is present. Because my buf does not exist at all, the buf of the formal parameter does not accept the correct address from the main function.