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 8798575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:07:37+00:00 2026-06-14T00:07:37+00:00

I am working on a C program that uses sockets to retrieve a file

  • 0

I am working on a C program that uses sockets to retrieve a file using the HTTP GET request. I use the recv function to write to a buffer, then append a new file with the contents of the buffer. The program works fine except for one problem: The top of every file includes the HTTP response.

For example, I can successfully download and open a PDF file from the web using my program and it will open with no issues. However if I edit the PDF in Notepad++, I see the following at the top:

HTTP/1.1 200 OK
Date: Wed, 07 Nov 2012 19:57:54 GMT
Server: Apache/2.2.21 (Unix) mod_python/3.3.1 Python/2.6.6 PHP/5.3.8
Last-Modified: Wed, 01 Aug 2012 21:31:31 GMT
ETag: "f2ae8c-4134aa-4c63b04c07df2"
Accept-Ranges: bytes
Content-Length: 4273322
Content-Type: application/pdf

%PDF-1.4
%äðíø
10 0 obj
<</Filter/FlateDecode/Length 2722>>
...

If I download the PDF file using my browser, the files match except for the HTML response at the top of the file retrieved by my program. I have verified this by removing the offending lines and comparing the file hashes.

I feel that there are much more elegant and proper ways of approaching this. I know that there are always two newline characters after the HTTP response before the file begins, so here is my (sloppy, non-working) attempt at extracting the response:

FILE* ptr_file = fopen("PDF_TEST.pdf", "w+");
char* buffer[BUFFER_SIZE];
int file_pos   = 0;
int bytes_rcvd = 0;
int first_iter = 1;

while((bytes_rcvd = recv(socket_server, buffer, BUFFER_SIZE, 0)) > 0)
{
    if(first_iter)
    {// Need to remove the HTTP response from the buffer
        char* str_buffer;
        char* html_resp = strstr(buffer, "\n\n");
        int   html_resp_length = strlen(html_resp) + 2;
        printf("HTML RESPONSE:\n%s\n\n", html_resp);
        char* first_buffer[BUFFER_SIZE - html_resp_length];
        memcpy(first_buffer, buffer+html_resp_length-1, sizeof(first_buffer));
        printf("\n\nREST OF BUFFER:%s\n", first_buffer);
        bytes_rcvd -= html_resp_length;
        fwrite(first_buffer, 1, bytes_rcvd, ptr_file);
        first_iter = 0;
        continue;
    }           
    fwrite(buffer, 1, bytes_rcvd, ptr_file); 
    file_pos += bytes_rcvd;
}  

I get segmentation faults with this code, but I believe that’s due to the fact that my buffer is an array of char* and I’m using it as if it where a char array.

My questions:
1.) What is the best way of separating the HTTP response from the file?
2.) Is it better to use the Content-Length specified by the HTML response for writing to the file, or should I use my current method of writing the number of bytes received?

Any input is 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-06-14T00:07:38+00:00Added an answer on June 14, 2026 at 12:07 am

    One way is to have two loops: The first for the response header, read until you get an empty line. The second receive loop for the data.

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

Sidebar

Related Questions

I was working with a program that uses a function to set a new
I'm currently working on a program (in C++, using Code::Blocks) that uses a lot
I am working on a reliable file transfer program that uses UDP. (for a
I'm working on a program that deals with a file that uses hashes. The
I am using C# to write a program that uses a web service from
I am working on a program that uses IE to display its help pages.
Greetings, Forum. I'm working on a program in Python that uses Twisted to manage
I'm working on a program that accesses StarTeam using the StarTeam SDK for .NET.
I'm working on a program that takes in a Wavefront .obj file and eventually
Working with a program that uses 16bytes 4v4 one byte matrices : unsigned char

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.