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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:23:19+00:00 2026-06-14T06:23:19+00:00

I am just trying to implement a simple web server in C++. For that

  • 0

I am just trying to implement a simple web server in C++. For that depending on the request the web server fetches data from server. For that I am using

int main()
{
std::ifstream file("/home/chaitanya/cpp/net/hello");
int length;
char *buffer;

if(file.is_open())
{
    std::cout << "File is open\n";

    file.seekg(0, std::ios::end);
    length = file.tellg();      
    file.seekg(0, std::ios::beg);

    buffer = new char[length];

    file.read(buffer, length);
    file.close();   
}   

printf("Data:\n\n%s\n", buffer);
delete[] buffer;

return 0;
}

This working fine for small files. Even i am sending the whole file data irrespective of its size thru the socket. Is it a better approach ?

For instance, if file size is huge ? I guess it would be better to send a specific chunk of data at a time.

Can u guys plz suggest as to which approach would be good (or) any other better/faster approaches used by web servers ?

Thanks in advance. 🙂

  • 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-14T06:23:20+00:00Added an answer on June 14, 2026 at 6:23 am

    Almost in every OS you have a better OS dependent option for sending a file over a socket, for example in linux you have sendfile, in Windows you have TransmitFile and …, but if you want to have a simple portable solution, I will derive a class from std::ostream that send data to the socket and then use this:

    my_socket_stream s;
    s << file.rdbuf() << std::flush;
    

    using this technique I can send multi giga byte files without any problem!

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

Sidebar

Related Questions

I'm trying to implement a simple horizontal navigation menu that just shows a single
Just trying to implement a simple audit logging solution with Grails 2.0.2 and it
I'm trying to implement chained filter dropdown using just javascript - jquery. The solution
I'm trying to implement a simple client-middleware-database architecture, where the client send the request
I can upload a file to my Apache web server using Curl just fine:
I am trying to implement a simple chat program in linux using bsd sockets.
i am creating a web project using JSP, and is trying to implement a
I'm trying to implement a simple watermark on a text box that disappears when
I was trying to implement a simple function that can concatenate any number of
Trying to implement 3-layer (not: tier, I just want to separate my project logically,

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.