我在带有 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
}
}
如果在没有调试器和断点的情况下运行,有时不会返回所有数据。
如果我尝试: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/