2

我在带有 IIS 的远程计算机上使用 .net 编写了一个 Web 服务,我正在尝试使用一个 C 程序连接到它,该 C 程序使用 socker 来执行 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;
   }
}

这是非常可悲的...

4

2 回答 2

3
#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
于 2016-02-10T18:50:51.787 回答
1

它在哪里说它填充了缓冲区?阅读男人形象。它会一直阻塞,直到可以传输至少一个字节的数据,然后传输到达的任何数据。

于 2016-02-10T17:53:46.037 回答