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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:19:11+00:00 2026-05-31T04:19:11+00:00

There is a server that response a .png file over http: #include server.h string

  • 0

There is a server that response a .png file over http:

#include "server.h"
string Server::header(int contentLength)
{
    string h =
    "HTTP/1.1 200 OK\n"
    "Content-Length: " + boost::lexical_cast<string>(contentLength) + "\n"
    "Content-Type: image/png;\n"
    "Connection: close\n"
    "\n";
    return h;
}
string Server::readMap(const string &filename)
{
    ifstream file (filename.c_str(), ios::in|ios::binary);
    string reply;
    char buf[512];
    while (file.read(buf, sizeof(buf)).gcount() > 0)
        reply.append(buf, file.gcount());
    return reply;
}
void Server::run(const string &filename, int port)
{
    string data = readMap(filename);
    try
    {
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port));
        for (;;)
        {
          tcp::socket socket(io_service);
          acceptor.accept(socket);
          boost::asio::write(socket, boost::asio::buffer(header( data.size() )));
          boost::asio::write(socket, boost::asio::buffer(data));
        }
    }
    catch (std::exception& e)
    {
        cerr << "exception: " << e.what() << endl;
    }
}

Every time an error happens:

exception: Connection reset by peer

I can see some part of an image in my browser, and sometimes the image is almost complete, but it never works without the error.

If I use wget it looks like

wget http://localhost:8089
--2012-03-07 12:07:19--  http://localhost:8089/
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:8089... connected.
HTTP request sent, awaiting response... 200 OK
Length: 760032 (742K) [image/png]
Saving to: `index.html'

62% [========================================================>                                   ] 475,136     --.-K/s   in 0.002s  

2012-03-07 12:07:19 (287 MB/s) - Read error at byte 475136/760032 (Connection reset by peer). Retrying.

--2012-03-07 12:07:20--  (try: 2)  http://localhost:8089/
Connecting to localhost|127.0.0.1|:8089... connected.
HTTP request sent, awaiting response... 200 OK
Length: 760032 (742K) [image/png]
Saving to: `index.html'

73% [==================================================================>                         ] 557,056     --.-K/s   in 0.001s  

... many failes and finally

--2012-03-07 12:09:01--  (try: 9)  http://localhost:8089/
Connecting to localhost|127.0.0.1|:8089... connected.
HTTP request sent, awaiting response... 200 OK
Length: 760032 (742K) [image/png]
Saving to: `index.html'

100%[===========================================================================================>] 760,032     --.-K/s   in 0.001s 

Any ideas how to fix it?

  • 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-31T04:19:12+00:00Added an answer on May 31, 2026 at 4:19 am

    There are several more complete HTTP-implementations in the ASIO-docs, including static file serving. One way to go would be to reuse some of that sample code for your application.

    In this particular case, there’s an example of how to correctly open and buffer a file at http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/example/http/server/request_handler.cpp

      std::ifstream is(full_path.c_str(), std::ios::in | std::ios::binary);
        ...
      char buf[512];
      while (is.read(buf, sizeof(buf)).gcount() > 0)
        rep.content.append(buf, is.gcount());
    

    The docs also has examples for actual asynchronous HTTP-implementations. (I assume you’re using boost::asio to eventually make it asynchronous?)

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

Sidebar

Related Questions

There is a page on our server that's reachable via two different URLs. http://www.spotlight.com/6213-5613-0721
There is a file that hosted remotely on a server that is not supporting
There is a simple http server API that allow you do some simple stuff
I'm using Response.AddHeader(Content-Disposition, attachment; filename= + Server.HtmlEncode(FileName)); to pop a 'open/save file' dialog for
Jboss 4.2.0A Is there a /server directory that I need to change or something?
Is there a method of accessing an Exchange server that does not have IMAP
I there a PHP based source control 'server' that is compatible with SVN clients?
Is there any way you can find the IP of the server that the
We are getting intermittent problems on a production server that we cannot recreate. There
I'm curious, is there a way to tell the SQL Server that a specific

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.