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

  • SEARCH
  • Home
  • 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 7719629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:27:47+00:00 2026-06-01T03:27:47+00:00

I am getting start to socket use in C programming language. I am trying

  • 0

I am getting start to socket use in C programming language. I am trying to make an simple http request and store the buffer obtained from read() in my buffer. For this, I use pointers/realloc(), the C programs works fine, compile no errors, but it is reading only a part of the http response.

For example, if I try to get the binary of google’s logo: http://www.google.com/images/srpr/logo3w.png the Content-Length say 7007 bytes, but strlen(buffer) say 5146 for me.I belive that the mistake out here is my buf_size and realloc() why bytesreaded is 7337 the 330 bytes I belive that is of headers.

Here is my code:

char *
httpget(const char * domain, const int port, const char * headers)
{
    int sockfd; /* Socket file descrption */
    int buf_size = MAX_BUFFER_SIZE;

    struct sockaddr_in  sock_addr; 
    struct hostent  *   host;

    char * buffer;
    char * newbuf;
    char * tbuf;

    sockfd = socket(AF_INET, /* Uses IPV4 Internet protocols */
                    SOCK_STREAM, /* Uses the TCP (Transfer Communication Protocol) */
                    0  /* "0" for socket () function choose the correct protocol based on the socket type. */
                    );

    if( sockfd == -1 )
    {
        return NULL;
    }

    host = gethostbyname(domain);

    if( NULL == host )
    {
        close(sockfd);
        return NULL;
    }

    memset(&sock_addr, '\0', sizeof(sock_addr));
    sock_addr.sin_family = AF_INET;
    memcpy( &sock_addr.sin_addr.s_addr,
            host -> h_addr,
            host -> h_length );

    sock_addr.sin_port = htons(port);

    if( connect(sockfd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) == -1)
    {
        close(sockfd);
        return NULL;
    }

    if( write(sockfd, headers, strlen(headers) + 1) == -1)
    {
        close(sockfd);
        return NULL;
    }

    buffer = malloc( MAX_BUFFER_SIZE );
    tbuf = malloc( MAX_BUFFER_SIZE );

    if(buffer == NULL || tbuf == NULL)
    {
        return NULL;
    }

    int bytesloaded = 0;
    int readed;

    while( (readed = read(sockfd, tbuf, MAX_BUFFER_SIZE)) > 0 )
    {   

        if(bytesloaded + readed >= buf_size)
        {
            buf_size = buf_size + MAX_BUFFER_SIZE;  
            newbuf = realloc(buffer, buf_size);

            if(newbuf != NULL)
               buffer = newbuf; 
            else 
              return NULL;
        }
          memcpy(buffer + bytesloaded, tbuf, readed);
      bytesloaded += readed;
    }

    //printf("bytesreaded = %d and buffer len is %d\n", bytesloaded, strlen(buffer));
    free(tbuf);
    close(sockfd);

    return buffer;
}

then:

char * domain = "www.google.com\0";
char * sheaders = "GET /images/srpr/logo3w.png HTTP/1.1\r\nHost:www.google.com\r\nConnection:close\r\n\r\n\n\0";
int port = 80;
char * response = httpget(domain, port, sheaders);

  • 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-06-01T03:27:49+00:00Added an answer on June 1, 2026 at 3:27 am

    Don’t use str* functions on arbitrary data. These are made to operate on C strings, which are zero-terminated. Binary data (most image formats) can contain zeros in the middle.

    You should be using memcpy/memmove, and you have to rely on the return value of read to know how much data you actually got. strlen on binary data is meaningless.

    Try replacing this part:

    bytesloaded += readed;
    strcat(buffer, tbuf);
    

    With something like:

    if (bytesloaded+readed >= buf_size) {
      // do the realloc now
    }
    memcpy(buffer+bytesloaded, tbuf, readed);
    bytesloded += readed;
    

    buffer + x (with x an integer type whose value is less than the allocated buffer size) is a pointer to the xth char in buffer. (This is pointer arithmetic. The type of buffer matters. In this case, it is invalid if x is negative.)
    You need to perform the re-allocation before you attempt the memcpy otherwise you risk writing past the end of the buffer.
    memcpy is safe here because you know that buffer and tbuf don’t overlap.

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

Sidebar

Related Questions

After asking so many question about android socket programming and getting valuable answers from
I am trying to make multiprocessing and socket programming work together, but, I am
All of a sudden I start getting this error while trying to open 2
Getting the following error when trying to start a session: Warning: session_start() [function.session-start]: Node
I'm trying to get a simple python script from the learnpythonthehardway tutorial to show
I'm trying to run this simple query over a JDBC connection: select _fk_DeptBillTo from
Well I am familiar with socket programming in c, iOS environment..But now trying to
I'm getting ready to start a new project and I've been researching the entity
I am getting ready to start building a new web project in my spare
I am getting an illegal start of type error at the first nested if

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.