1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Can anybody explain me this new modified recv() function

Discussion in 'C' started by lionaneesh, May 29, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    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
             } 
     

Share This Page