In socket programming, the RECV function returns a value of-1.

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.