在发送了几百个字节后,send()与EAGAIN失败

时间:2013-07-29 13:07:15

标签: c linux sockets libev

这是在Ubuntu 12.04上发生的。相同的代码在OSX上运行良好。

在下面的日志中,您可以看到SO_SNDBUF是20440,并且在使用EAGAIN失败之前,有几个send()成功。

wsmux started on port 8888
send buffer size = 20440
open wsmux:187.59.165.86-16580
send 129, result 129, errno 115
message wsmux:187.59.165.86-16580 NICK zxc5239
message wsmux:187.59.165.86-16580 USER zxc zxc zxc zxc
message wsmux:187.59.165.86-16580 JOIN #a
send 2, result 2, errno 115
send 66, result 66, errno 115
send 2, result 2, errno 115
send 42, result 42, errno 115
send 2, result 2, errno 115
send 100, result 100, errno 115
send 2, result 2, errno 115
send 43, result 43, errno 115
send 2, result 2, errno 115
send 48, result 48, errno 115
send 2, result -1, errno 11
close wsmux:187.59.165.86-16580 Resource temporarily unavailable

启用的唯一套接字选项是TCP_NODELAY和O_NONBLOCK。 这可能是什么问题?

有问题的代码:

2 个答案:

答案 0 :(得分:0)

如果您有O_NONBLOCK,那么对sendrecv的每次调用都可能会失败EAGAIN,因为您设置了非阻塞套接字(这只表示调用会阻止)。这不是错误,不应该作为一个处理。它在手册页中明确解释为例如send

答案 1 :(得分:0)

TCP_NODELAY会导致每个send立即发送数据以及所有TCP / IP标头。如果您继续为每个2字节数据包执行此操作,则会快速遇到EAGAIN,因为远程端必须先ACK数据包才能发送新数据包。更不用说它效率也很低! 2字节数据包真的很紧急吗?

相关问题