UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-07-25 08:17 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: 3.8 - What to do to recv' unknown data size

/*************************************************************** 

  ReadBufferedData 

  Given a socket to read from, read data until 
  the socket is empty or the buffer is full. 
  Return the number of bytes that were read. 

  readSocket I the socket to read from 

  buffer O the buffer to read into 

  bufLen I the size of the buffer 

  bytesRead O the number of bytes read 

  errnoBack O the value of errno, if any, 
encountered in the function 

  Returns: status code indicating success - noErr = success 
   
  ***************************************************************/ 

OSErr ReadBufferedData(const int readSocket,char *const buffer, 
const size_t bufLen,size_t *const bytesRead, 
int *const errnoBack) 
{ 
OSErr readErr=noErr; 
size_t bytesLeft,bytesThisTime,bytesSoFar=0UL; 
Boolean done=FALSE; 

bytesLeft=bufLen; 

do 
{ 
bytesThisTime=recv(readSocket,buffer+bytesSoFar,bytesLeft,0); 

if(bytesThisTime==0) 
{ 
readErr=socketEOFErr; 
done=TRUE; 
} 

if(bytesThisTime==-1) 
{ 
readErr=socketReadErr; 
*errnoBack=errno; 
done=TRUE; 
} 
else 
{ 
bytesSoFar+=bytesThisTime; 
bytesLeft-=bytesThisTime; 

if(bytesLeft==0) 
done=TRUE; 
} 
}while(!done); 

if((readErr==noErr)||(readErr==socketEOFErr)) 
*bytesRead=bytesSoFar; 

return readErr; 
}

Offline

Board footer

Powered by FluxBB