Handle non-blocking socketEAGAIN errors in linux

Source: Internet
Author: User
When a customer uses the send function provided by Socket to send large data packets, an EAGAIN error may be returned. This error is caused by sending

Use a non-blocking socket in Linux.
(1) sending

When the customer sends large data packets through the send function provided by Socket, an EAGAIN error may be returned. This error occurs because the size variable in the send function exceeds the value of tcp_sendspace. Tcp_sendspace defines the amount of data that the application can cache in the kernel before calling send. If the O_NDELAY or O_NONBLOCK attribute is set in the socket, the EAGAIN error is returned if the sending cache is full.


To eliminate this error, you can choose from the following three methods:
1. increase tcp_sendspace so that it is greater than the size parameter in send
--- No-p-o tcp_sendspace = 65536

2. before sending, set a greater value for SNDBUF in the setsockopt function.

3. replace send with write because write does not set O_NDELAY or O_NONBLOCK.

(2) receiving

When receiving data, the error message "Resource temporarily unavailable" is often reported. The errno code is 11 (EAGAIN ). This indicates that you have called the blocking operation in non-blocking mode. if the operation is not completed, this error is returned. this error will not damage the synchronization of the socket, the next cycle is followed by recv. For a non-blocking socket, EAGAIN is not an error. On VxWorks and Windows, EAGAIN is named EWOULDBLOCK. In fact, this is not an error, but an exception.

In addition, if EINTR is returned, that is, errno is 4, and the error description is Interrupted system call, the operation should continue.

Finally, if the return value of recv is 0, it indicates that the other party has disconnected, and our receiving operation should also end.

(3) The following is another explanation.

If the sending end traffic is greater than the receiving end traffic (that is, epoll's program reads faster than the forwarded socket), because it is a non-blocking socket, the send () function returns, however, the data in the actual buffer zone is not actually sent to the receiving end. in this way, the EAGAIN error is generated when the buffer zone is full (refer to man send), and the data sent by this request is ignored. so,

The socket_send () function needs to be encapsulated to handle this situation. This function will try to write the data and then return it. The return value-1 indicates an error. Within socket_send (), when the write buffer is full (send () returns-1 and errno is EAGAIN), the system will wait and try again. this method is not perfect. Theoretically, it may be blocked within socket_send () for a long time, but there is no better way.
This method is similar to readn and writen encapsulation (I have written it myself and I have also introduced it in advanced programming for UNIX environments)


Size_t socket_send (int sockfd, const char * buffer, size_t buflen)
{
Size_t tmp;
Size_t total = buflen;
Const char * p = buffer;

While (1)
{
Tmp = send (sockfd, p, total, 0 );

If (tmp <0)
{
// When sending receives the signal, you can continue writing, but here-1 is returned.
If (errno = EINTR)
{
Return-1;
}

// If this error is returned when the socket is not blocked, it indicates that the write buffer queue is full,
// Retry after delay.
If (errno = EAGAIN)
{
Usleep (1000 );
Continue;
}

Return-1;
}

If (size_t) tmp = total)
{
Return buflen;
}

Total-= tmp;
P + = tmp;
}

Return tmp;
}

Related Article

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.