php socket_recv() msg_waitall,套接字recv()使用MSG_WAITALL挂在大消息上

在RHEL5.2上稳定运行的应用程序,在升级到RHEL6.1后,遇到一个在回送接口上读取大文件时挂起的问题。挂起主要发生在recv()调用使用MSG_WAITALL标志时。当“localhost”映射到回送地址时,问题尤为明显。服务器发送TCP窗口已满,客户端回应TCP ZeroWindow,然后挂起。移除MSG_WAITALL标志则能解决问题。问题可能与回送接口、TCP窗口管理和时序有关。
摘要由CSDN通过智能技术生成

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

我创建了一个重现问题的测试应用。它可以挂在100中的98次。

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

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,从而通过以太网接口路由数据包,则应用程序成功完成。

当我遇到挂起时,服务器会发送“ TCP窗口已满”消息,而客户端会以“ TCP ZeroWindow”消息进行响应(请参见图像和附带的tcpdump捕获)。从这一点来看,它永远挂起,服务器发送保持活动状态,而客户端发送ZeroWindow消息。客户端似乎从来没有扩大其窗口,从而允许传输完成。

在挂起期间,如果我检查“ netstat -a”的输出,则服务器发送队列中有数据,但是客户端接收队列为空。

如果我从recv()调用中删除了MSG_WAITALL标志,则该应用程序成功完成。

挂起问题仅在1台特定计算机上使用回送接口出现。我怀疑这可能与时序依赖性有关。

当我减小“文件”的大小时,挂起的可能性降低了

可在以下位置找到测试应用程序的源:

从Loopback接口捕获的tcpdump可以在以下位置找到:

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

> gcc socket_test.c -o socket_test

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

> ./socket_test 6000000

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

我很乐意听到关于我可能做错了什么的任何建议,或任何其他调试问题的方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值