Linux recv function return value analysis

Source: Internet
Author: User

Function Prototypes:

ssize_t recv (int sockfd, void *buf, size_t len, int flags);

The first parameter of the function formulates the receiving end 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 buffer buf;

The fourth parameter is generally 0; (Flag bit)

The RECV function returns the number of bytes of its actual copy. If recv error in copy, then it returns SOCKET_ERROR; If the recv function waits for the protocol to receive data when the network is interrupted, then it returns 0;

The recv function is just copy data, and the real receive data is the protocol to complete.

The default recv function socket is blocked

There is no distinction between blocking and non-blocking recv return values, which are
<0 Error
=0 Connection off
>0 Receive data size,

Special: When the return value is <0 and (errno = = Eintr | | errno = = Ewouldblock | | errno = eagain), the connection is considered normal and continues to receive.
Just blocking mode recv will block the reception of data, non-blocking mode if no data will be returned, will not block the read, so need to loop read).

Return Description:
When executed successfully, returns the number of bytes received. 0 is returned when the other end is closed. The failed return -1,errno is set to one of the following values
Eagain: The socket is marked as non-blocking, and the receive operation is blocked or the receive time-out
Ebadf:sock is not a valid descriptive word.
Econnrefuse: Remote host blocking network connection
Efault: Error in memory space access
EINTR: Operation interrupted by signal
EINVAL: Invalid Parameter
ENOMEM: Low Memory
Enotconn: Socket associated with connection-oriented is not yet connected
Enotsock:sock index is not a socket

When the return value is 0 o'clock, the connection is closed normally;

Linux recv function return value analysis

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.