Linux non-blocking socket eagain error handling "Go"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/tianmohust/article/details/8691644

Copyright Notice: This article for Bo Master original article, without Bo Master permission not reproduced. In the case of using a non-blocking socket in Linux. (a) When sending a large packet via the Send function provided by the socket, the client may return a eagain error. 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 an app can cache in kernel before calling send.   When the application sets the O_ndelay or O_nonblock property in the socket, send returns a eagain error if the sending cache is full. To eliminate this error, there are three ways to choose:1. Adjust the tcp_sendspace to larger than the size parameter in send---no-p-o tcp_sendspace=65536     2before calling send, set a larger value for SNDBUF in the setsockopt function3. Use write instead of send, because write does not set O_ndelay or O_nonblock (ii) receive data at the time of receiving resource temporarily unavailable prompt, errno code is 11 ( Eagain). This indicates that you have a blocking operation in non-blocking mode, which returns the error if the operation is not completed, which does not break the synchronization of the socket, regardless of the next loop and then recv. For non-blocking sockets, Eagain is not an error. On VxWorks and Windows, Eagain is named Ewouldblock.  In fact, this is not a mistake, just an anomaly.  Additionally, if EINTR is present as errno 4, the error description interrupted system call, the operation should also continue. Finally, if the return value of recv is 0, it indicates that the connection has been disconnected and our receive operation should end. (c) The following is another explanation if the sending end of traffic is greater than the traffic at the receiving end (meaning that the Epoll program reads faster than the forwarded socket), the Send () function returns if the socket is non-blocking, but the actual buffer data is not actually sent to the receiving end. This constant reading and sending, when the buffer full will produce a eagain error (refer to man Send), at the same time, disregard the data sent by the request. Therefore, a function that encapsulates the socket_send () is used to handle this situation, and the function tries to write and return the data as soon as possible.-1 indicates an error. Inside the Socket_send (), when the write buffer is full (send () returns-1, and errno is Eagain), then wait and try again. This method is not perfect, in theory it may be a long time to block the inside of the Socket_send (), but there is no better way. This approach is similar to the READN and Writen encapsulation (written by myself, in the Advanced Programming for the UNIX environment is also introduced) [CPP] View plain copy size_t socket_send (intSOCKFD,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 send receives a signal, it can continue writing, but here it returns-1.                 if(errno = =eintr) {                      return-1; }                        //when the socket is non-blocking, such as returning this error, indicates that the write buffer queue is full,//do the delay here and try again.                 if(errno = =Eagain) {Usleep ( +); Continue; }                        return-1; }                    if((size_t) tmp = =Total ) {                  returnBuflen; } Total-=tmp; P+=tmp; }                returntmp; }         

Linux non-blocking socket eagain error handling "Go"

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.