Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Can anybody explain me this new modified recv() function (http://www.go4expert.com/forums/explain-modified-recv-function-t22246/)

lionaneesh 29May2010 09:36

Can anybody explain me this new modified recv() function
 
Code:

/* This function accepts a socket FD and a ptr to a destination
 * buffer.  It will receive from the socket until the EOL byte
 * sequence in seen.  The EOL bytes are read from the socket, but
 * the destination buffer is terminated before these bytes.
 * Returns the size of the read line (without EOL bytes).
 */
int recv_line(int sockfd, unsigned char *dest_buffer) {
#define EOL "\r\n" // End-of-line byte sequence
#define EOL_SIZE 2
  unsigned char *ptr;
  int eol_matched = 0;

  ptr = dest_buffer;
  while(recv(sockfd, ptr, 1, 0) == 1) { // Read a single byte.
      if(*ptr == EOL[eol_matched]) { // Does this byte match terminator?
        eol_matched++;
        if(eol_matched == EOL_SIZE) { // If all bytes match terminator,
            *(ptr+1-EOL_SIZE) = '\0'; // terminate the string.
            return strlen(dest_buffer); // Return bytes received
        }
      } else {
        eol_matched = 0;
      }
      ptr++; // Increment the pointer to the next byter.
  }
  return 0; // Didn't find the end-of-line characters.
}

Hey guyz I have a problem in the following lines and am not understanding what's going on here please explain me!

Code:

while(recv(sockfd, ptr, 1, 0) == 1) { // Read a single byte.
      if(*ptr == EOL[eol_matched]) { // Does this byte match terminator?
        eol_matched++;
        if(eol_matched == EOL_SIZE) { // If all bytes match terminator,
            *(ptr+1-EOL_SIZE) = '\0'; // terminate the string.
            return strlen(dest_buffer); // Return bytes received
        }



All times are GMT +5.5. The time now is 23:30.