Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6869415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:33:17+00:00 2026-05-27T03:33:17+00:00

I need getline() to read the request header sent by my browser to the

  • 0

I need getline() to read the request header sent by my browser to the webserver I’m programming. This is the getMessage function which is supposed to do that task:

char *getMessage(int fd) {
  FILE *sstream = fdopen(fd, "r");
  // initialise block to 1 char and set it to null
  char *block = malloc(sizeof(char));
  *block = '\0';
  int size = 1;

  // Read from the file descriptor fd (using a FILE stream) until a blank line is
  // received.
  // Read 100 lines (buffersize) from sstream and put into the buffer. If lines have
  // been successfully read concatenate them with block.
  int buffersize = 100;
  char *buffer = malloc (buffersize + 1);

  while(getline(&buffer,&buffersize,sstream) != -1){
     int length = strlen(buffer);
     printf("Buffer length: %d\n",length);
     block = realloc(block,strlen(block)+strlen(buffer)+1);
     strcat(block,buffer);
     if(strcmp(buffer,"\r\n") == 0) break;
 }

  int len = strlen(block);
  printf("Block length: %d\n", len);
  printf("%s \n", block);
  return block;
} 

Basically the input of the getMessage function (fd), is the input from my listening socket declared in my main method. I have verified that the output is correct. Now I need to convert the output from the file descriptor to a string and return that string. But every time I run my server it gets stuck in the while loop. Not executing the statements in the loop.
EDIT: Added a loop-terminating condition: Now it jumps to “Block length” immediatley.
Help is much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:33:18+00:00Added an answer on May 27, 2026 at 3:33 am

    If you are using the POSIX 2008 getline() function, then you’re throwing away useful information (it returns the length of the line it reads, so if you capture that information, you would not need the strlen() in the loop.

    If the code blocks on a getline() call, it probably means that the upstream socket is not closed, but there is no data being sent any more. Your sending code needs to close the socket so that this code can detect EOF.

    Or, since you discuss ‘a blank line’, then maybe your code should be checking for a line containing just \r\n (or maybe just \n) and break the loop; your code is not doing that at the moment.

    Your loop also exhibits quadratic behaviour because you are repeatedly using strcat(). You would do better to keep tabs on the end of the string and simply strcpy() the new data after the old, then adjust the pointer to the end of the string.


    On further review, I note that you use fdopen() to open a file stream based on the file descriptor, but you neither close it nor return the file stream to the caller for closing. This leads to a leakage problem.

    Rule of Thumb: if you allocate a resource, you should release it, or pass it back to be released.

    I recommend changing the interface to use an already-open FILE *, and doing the fdopen() in the calling code. Alternatively, if you won’t need the file descriptor again, you can keep the current interface and use fclose() before returning, but this will close the underlying file descriptor too.

    This code works for me (MacOS X 10.7.2; XCode 4.2.1):

    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    extern char *getMessage(FILE *);
    
    char *getMessage(FILE *fp)
    {
        char    *block = 0;
        size_t   size = 0;
        size_t   buffersize = 0;
        char    *buffer = 0;
        ssize_t  newlen;
    
        while ((newlen = getline(&buffer, &buffersize, fp)) > 0)
        {
            printf("Buffer length: %ld\n", (long)newlen);
            block = realloc(block, size + newlen + 1);
            strcat(&block[size], buffer);
            size += newlen;
            if (strcmp(buffer, "\r\n") == 0)
                break;
        }
    
        printf("Block length: %zd\n", size);
        if (size > 0)
            printf("<<%s>>\n", block);
        return block;
    }
    
    int main(void)
    {
        char *msg;
        while ((msg = getMessage(stdin)) != 0)
        {
            printf("Double check: <<%s>>\n", msg);
            free(msg);
        }
        return 0;
    }
    

    I tested it with a file with DOS-style line endings as standard input, with both a blank line as the last line and with a non-blank line. Two blank lines in a row also seemed to be OK.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this function I need to replace all chars in a file that is
Need a function that takes a character as a parameter and returns true if
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
I am just trying to open this file and use the getline function to
I'm using VS 2008 and need to read text files that have UTF-8 Chinese
My instructor said the way to start this is to use the getline() function
Need to an expression that returns only things with an I followed by either
Need to call a filter function on some options based on a radio selected
For Example: If I need to read a multiple line input like(and I dont
I am participating in some programming competitions, and on many problems there's the need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.