gpt4 book ai didi

c - recv停顿或不返回所有数据(C代码)

转载 作者:行者123 更新时间:2023-12-03 11:51:05 26 4
gpt4 key购买 nike

我在带有IIS的远程计算机上以.net编写了一个Web服务,我试图使用Socker通过C程序连接到它,以执行SOAP请求。

我的问题是我有一些探针接收数据:

接收数据循环无法以某种方式工作。

如果我写:

nByte = 1;
while(nByte!=512)
{
nByte = recv(sockfd,buffer,512, 0);
if( nByte < 0 )
{
// check the error
}
if( nByte > 0)
{
// append buffer to received data
}
}

如果它在没有调试器和breackpoints的情况下运行,则有时不会返回所有数据。

如果我尝试:在数据末尾添加 while(nByte!=0),它将停顿并出错。

应该怎么做?
谢谢,
安东诺

** 编辑 **
我以另一种方式解决了我的情况,我检查了soap xml end的返回值:
nByte = 1;
while(nByte!=0)
{
nByte = recv(sockfd,buffer,512, 0);
if( nByte < 0 )
{
// check the error
}
if( nByte > 0)
{
// append nByte buffer to received data
if( strstr("</soap:Envelope>", buffer) != NULL)
break;
}
}

这是非常可悲的...

最佳答案

#define BUFFERSIZE 512  

byte buffer[BUFFERSIZE];
int nByte = BUFFERSIZE;
int rByte;

while(nByte!=0)
{
rByte = recv(sockfd, &buffer[BUFFERSIZE-nByte], nByte, 0);
if( rByte < 0 )
{
// socket error
break;
}
if( rByte == 0)
{
// connection closed by remote side or network breakdown, buffer is incomplete
break;
}
if(rByte>nByte)
{
// impossible but you must check it: memory crash, system error
break;
}
nByte -= rByte; // rByte>0 all is ok
// if nByte==0 automatically end of loop, you read all
// if nByte >0 goto next recv, you need read more bytes, recv is prtialy in this case
}

//**EDIT**

if(nByte!=0) return false;

// TO DO - buffer complete

关于c - recv停顿或不返回所有数据(C代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com