我正在通过套接字发送大量数据(以及... 1Mb),但我不知道为什么发送操作阻止程序并且永不结束。小发送完美运行,我无法找到问题在哪里。任何人都可以帮助我吗?通过套接字发送大量数据
非常感谢您提供任何帮助。
int liResult = 1;
int liConnection = 0;
int liSenderOption = 1;
struct addrinfo laiSenderAddrInfo;
struct addrinfo *laiResultSenderAddrInfo;
memset(&laiSenderAddrInfo,0,sizeof(laiSenderAddrInfo));
laiSenderAddrInfo.ai_socktype = SOCK_STREAM;
laiSenderAddrInfo.ai_flags = AI_PASSIVE;
liResult = getaddrinfo(_sIp.c_str(), _sPort.c_str(), &laiSenderAddrInfo, &laiResultSenderAddrInfo);
if (liResult > -1)
{
liConnection = socket(laiResultSenderAddrInfo->ai_family, SOCK_STREAM, laiResultSenderAddrInfo->ai_protocol);
liResult = liConnection;
if (liConnection > -1)
{
setsockopt(liConnection, SOL_SOCKET, SO_REUSEADDR, &liSenderOption, sizeof(liSenderOption));
liResult = connect(liConnection, laiResultSenderAddrInfo->ai_addr, laiResultSenderAddrInfo->ai_addrlen);
}
}
size_t lBufferSize = psText->length();
long lBytesSent = 1;
unsigned long lSummedBytesSent = 0;
while (lSummedBytesSent < lBufferSize and lBytesSent > 0)
{
lBytesSent = send(liConnection, psText->c_str() + lSummedBytesSent, lBufferSize - lSummedBytesSent, MSG_NOSIGNAL);
if (lBytesSent > 0)
{
lSummedBytesSent += lBytesSent;
}
}
OP没有失败;他是封锁的:显然他并不处于非封锁模式,我希望你说'失败'(EAGAIN/EWOULDBLOCK)时指的是你所指的。默认的TCP窗口可能比64k小很多:例如在Windows上尝试8k;除非你分别在调用'accept()'和'connect()'之前采取特殊措施,否则它不能增长到64k-1以上。将数据分成更小的发送块将对这个问题产生零影响。 Downvote。 – EJP