2

I have a web service written in .net on a remote computer with IIS, I am trying to connect to it with a C program using socker to do a SOAP request.

My problem is that I have some probem receiving the data:

The receiving data loop does not work in a way or in another.

If I write:

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
   }
}

sometime does not return all data, if it run without debugger and breackpoints.

If I try: while(nByte!=0) at the end of data it stalls and go in error.

How is it supposed to be done? Thanks, Antonino

** EDIT ** I resolved my situation in another way, I check the returned value for 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;
   }
}

It is very sad...

Perry
  • 1,113
  • 2
  • 10
  • 22

2 Answers2

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
theWalker
  • 2,022
  • 2
  • 18
  • 27
  • in this way it receives only READSIZE bytes, I want it download all stream until connection closed, but It never return 0. – Perry Feb 10 '16 at 18:56
  • "in this way it receives only READSIZE bytes" - not, we probe read all, but not always socket return all and we must read more – theWalker Feb 10 '16 at 18:58
  • in my answer, without errors you read all automatically and go out from loop without probe read 0 bytes. if rByte==0 and you not read all (nByte>0) - conection is closed by the remote side while communication, its incomplete connection. – theWalker Feb 10 '16 at 19:00
  • I'v rename READSIZE to BUFFERSIZE – theWalker Feb 10 '16 at 19:20
  • The state you label as impossible is indeed impossible. It is therefore unnecessary to check for it. – user207421 Feb 10 '16 at 21:26
  • programmer must check all paths, no bad data only bad alghoritms exists – theWalker Feb 10 '16 at 21:28
1

Where does it say it fills the buffer? Read the man image. It blocks until at least one byte of data can be transferred, then transfers whatever data has arrived.

user207421
  • 305,947
  • 44
  • 307
  • 483