开发者

Will EAGAIN return on send for anything other than buffer full?

If I use send() on a non-blocking tcp socket in Linux will it return EAGAIN for anything other than a send buffer full condition?

I basically need to decide if I want 开发者_如何学运维to use the socket send buffer as the only buffer for my app or if I need my own user space buffer to feed the socket buffer.


It shouldn't but I don't see how that affects your decision about a user space buffer one way or another. Either you'll need a buffer or not depending on what your app is doing, regardless of the specific reason for getting an EAGAIN.

You can also think about changing the tcp buffer size with setsockopt with the SO_SNDBUF option after doing some calculations to see if it is actually a performance win or not.


If I use send() on a non-blocking tcp socket in Linux will it return EAGAIN for anything other than a send buffer full condition?

On non-blocking socket that should be only condition, as send() blocks only when the send buffer is full.

Otherwise, I would suggest not to depend on the behavior as in my experience EAGAIN sometimes returned when something minor fails in kernel, yet socket is still OK. I had recently the experience on HP-UX when EAGAIN was returned since apparently kernel was running low on memory. As error is recoverable, EAGAIN is acceptable error code for the case.


EAGAIN/EWOULDBLOCK can also be returned (for TCP sockets) when the number of unacknowledged packets has reached the congestion window.

To check the status of the socket w.r.t. the congestion window, then try this:

#include <netinet/tcp.h>
static void print_tcp_cwnd(int socket)
{
    struct tcp_info tcp_info;
    uint tcp_info_length = sizeof(tcp_info);
    if ( getsockopt( socket, SOL_TCP, TCP_INFO, (void *)&tcp_info, &tcp_info_length ) == 0 ) 
    {
        printf("tcpi_snd_cwnd: %u, tcpi_unacked: %u\n",
            tcp_info.tcpi_snd_cwnd,
            tcp_info.tcpi_unacked
           );
    }
}

If tcpi_unacked == tcpi_snd_cwnd then send() will return EAGAIN/EWOULDBLOCK for a non-blocking socket.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜