c - 套接字 recv() 卡在带有 MSG_WAITALL 的大消息上

标签 c linux sockets networking tcp

我有一个应用程序,它从服务器读取大文件并经常卡在特定计算机上。它在RHEL5.2下已经成功运行了很长时间。我们最近升级到了 RHEL6.1,现在它经常挂起。

我创建了一个测试应用程序来重现该问题。 100 次中,它大约有 98 次挂起。

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/time.h>

int mFD = 0;

void open_socket()
{
  struct addrinfo hints, *res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_family = AF_INET;

  if (getaddrinfo("localhost", "60000", &hints, &res) != 0)
  {
    fprintf(stderr, "Exit %d\n", __LINE__);
    exit(1);
  }

  mFD = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

  if (mFD == -1)
  {
    fprintf(stderr, "Exit %d\n", __LINE__);
    exit(1);
  }

  if (connect(mFD, res->ai_addr, res->ai_addrlen) < 0)
  {
    fprintf(stderr, "Exit %d\n", __LINE__);
    exit(1);
  }

  freeaddrinfo(res);
}

void read_message(int size, void* data)
{
  int bytesLeft = size;
  int numRd = 0;

  while (bytesLeft != 0)
  {
    fprintf(stderr, "reading %d bytes\n", bytesLeft);

    /* Replacing MSG_WAITALL with 0 works fine */
    int num = recv(mFD, data, bytesLeft, MSG_WAITALL);

    if (num == 0)
    {
      break;
    }
    else if (num < 0 && errno != EINTR)
    {
      fprintf(stderr, "Exit %d\n", __LINE__);
      exit(1);
    }
    else if (num > 0)
    {
      numRd += num;
      data += num;
      bytesLeft -= num;
      fprintf(stderr, "read %d bytes - remaining = %d\n", num, bytesLeft);
    }
  }

  fprintf(stderr, "read total of %d bytes\n", numRd);
}

int main(int argc, char **argv)
{
  open_socket();

  uint32_t raw_len = atoi(argv[1]);
  char raw[raw_len];

  read_message(raw_len, raw);

  return 0;
}

我的测试的一些笔记:

  • 如果“localhost”映射到环回地址 127.0.0.1,则应用程序会挂起对 recv() 的调用,并且永远不会返回。
  • 如果“localhost”映射到计算机的 IP,从而通过以太网接口(interface)路由数据包,则应用程序成功完成。
  • 当我遇到挂起时,服务器会发送“TCP Window Full”消息,客户端会用“TCP ZeroWindow”消息进行响应(请参阅图像和附加的 tcpdump 捕获)。从此时起,它会永远挂起,服务器发送保持事件状态,客户端发送 ZeroWindow 消息。客户端似乎永远不会扩展其窗口以允许传输完成。
  • 在挂起期间,如果我检查“netstat -a”的输出,会发现服务器发送队列中有数据,但客户端接收队列为空。
  • 如果我从 recv() 调用中删除 MSG_WAITALL 标志,应用程序就会成功完成。
  • 仅在一台特定计算机上使用环回接口(interface)时才会出现挂起问题。我怀疑这可能都与时序依赖性有关。
  • 当我减小"file"的大小时,发生挂起的可能性就会降低

可以在此处找到测试应用程序的源代码:

Socket test source

可以在此处找到环回接口(interface)的 tcpdump 捕获:

tcpdump capture

我通过发出以下命令重现该问题:

>  gcc socket_test.c -o socket_test
>  perl -e 'for (1..6000000){ print "a" }' | nc -l 60000
>  ./socket_test 6000000

这会看到 6000000 字节发送到测试应用程序,该应用程序尝试使用对 recv() 的单次调用来读取数据。

我很想听到有关我可能做错的事情的任何建议或任何进一步的方法来调试问题。

最佳答案

MSG_WAITALL 应该阻塞,直到收到所有数据。来自 manual page on recv :

This flag requests that the operation block until the full request is satisfied.

但是,网络堆栈中的缓冲区可能不够大,无法容纳所有内容,这就是服务器上出现错误消息的原因。客户端网络堆栈根本无法容纳那么多数据。

解决方案是增加缓冲区大小(setsockoptSO_RCVBUF 选项)、将消息分割成更小的 block ,或者接收更小的 block 并将其放入您自己的缓冲区中。最后是我推荐的。

编辑:我在您的代码中看到您已经按照我的建议进行了操作(使用自己的缓冲读取较小的 block ),因此只需删除 MSG_WAITALL 标志即可.

哦,当 recv 返回零时,这意味着另一端已关闭连接,您也应该这样做。

关于c - 套接字 recv() 卡在带有 MSG_WAITALL 的大消息上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12258588/

相关文章:

c - 以下C代码编译错误的原因

linux - 在端口 8080 上安装 Varnish 会破坏本地环境

linux - 使用 awk 命令计数

linux - 如何更改 Fedora 上 Jenkins 工作区目录的默认权限?

sockets - Java Socket 接受来自一个 IP 的连接

将2个字节转换成12位数字?

c - getaddrinfo() 如何进行 DNS 查找?

objective-c - 为什么结构和基本类型不需要指针?

java - 发送文件到服务器java错误

sockets - 三向握手后是否发生接受事件?