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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:13:49+00:00 2026-05-15T06:13:49+00:00

#include <cstdlib> #include <iostream> #include <boost/bind.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; class session { public:

  • 0
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

class session
{
public:
    session(boost::asio::io_service& io_service)
        : socket_(io_service)
    {
    }

    tcp::socket& socket()
    {
        return socket_;
    }

    void start()
    {
        socket_.async_read_some(boost::asio::buffer(data_, max_length - 1),
            boost::bind(&session::handle_read, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }

    void handle_read(const boost::system::error_code& error,
        size_t bytes_transferred)
    {
        if (!error)
        {
            data_[bytes_transferred] = '\0';
            if(NULL != strstr(data_, "quit"))
            {
                this->socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both);
                this->socket().close(); // how to make this dispatch "handle_read()" with a "disconnected" flag?
            }
            else
            {
                boost::asio::async_write(socket_,
                    boost::asio::buffer(data_, bytes_transferred),
                    boost::bind(&session::handle_write, this,
                    boost::asio::placeholders::error));

                socket_.async_read_some(boost::asio::buffer(data_, max_length - 1),
                    boost::bind(&session::handle_read, this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
            }
        }
        else
        {
            delete this;
        }
    }

    void handle_write(const boost::system::error_code& error)
    {
        if (!error)
        {
            //
        }
        else
        {
            delete this;
        }
    }

private:
    tcp::socket socket_;
    enum { max_length = 1024 };
    char data_[max_length];
};

class server
{
public:
    server(boost::asio::io_service& io_service, short port)
        : io_service_(io_service),
        acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
    {
        session* new_session = new session(io_service_);
        acceptor_.async_accept(new_session->socket(),
            boost::bind(&server::handle_accept, this, new_session,
            boost::asio::placeholders::error));
    }

    void handle_accept(session* new_session,
        const boost::system::error_code& error)
    {
        if (!error)
        {
            new_session->start();
            new_session = new session(io_service_);
            acceptor_.async_accept(new_session->socket(),
                boost::bind(&server::handle_accept, this, new_session,
                boost::asio::placeholders::error));
        }
        else
        {
            delete new_session;
        }
    }

private:
    boost::asio::io_service& io_service_;
    tcp::acceptor acceptor_;
};

int main(int argc, char* argv[])
{
    try
    {
        if (argc != 2)
        {
            std::cerr << "Usage: async_tcp_echo_server <port>\n";
            return 1;
        }

        boost::asio::io_service io_service;

        using namespace std; // For atoi.
        server s(io_service, atoi(argv[1]));

        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}

While experimenting with boost::asio I’ve noticed that within the calls to async_write()/async_read_some() there is a usage of the C++ “new” keyword.

Also, when stressing this echo server with a client (1 connection) that sends for example 100,000 times some data, the memory usage of this program is getting higher and higher.

What’s going on? Will it allocate memory for every call? Or am I wrong? Asking because it doesn’t seem right that a server app will allocate, anything. Can I handle it, say with a memory pool?

Another side-question:

See the “this->socket().close();” ?
I want it, as the comment right to it says, to dispatch that same function one last time, with a disconnection error. Need that to do some clean-up. How do I do that?

Thank you all gurus (:

  • 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-15T06:13:49+00:00Added an answer on May 15, 2026 at 6:13 am

    In the hope that someone will contribute something…

    Further in my experiments at boost::asio I’ve decided that right after the server app is up & running I’ll put a breakpoint at C++’s ‘new’ code, i.e at “new.cpp” @ function “void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)”. Note, I’m using MSVC 2008.

    Using the code above, of the original post:

    Now that the BP is on I’m connecting one client.
    Allocation is done (several times) (as expected) (I know it because the debugger stops at the ‘new’ keyword as I set) and the new client is now ready to send/receive data.
    I send “hi” from the client to the server.
    The BP at ‘new’ is hit at handle_read().
    The source to is was the call to async_write() (I stack trace with MSVC).
    Hitting F5 (continue) generates another breakpoint at ‘new’ – this time the async_read_some() call generated it.

    Conclusion:
    Each such operation generates a call to ‘new’ !!!!!! Worst case a real server might have!

    So, further on looking for some way to use some sort of memory pool so these ‘new’ calls won’t exist brought me to the example: “allocation”.
    Path to it: “…….\boost_1_43_0\libs\asio\example\allocation\”.

    Doing the same with this new code (written below) gave me cheering results;
    Calls to async_write() and async_read_some() do not generate a call to ‘new’.

    So far it’s nice, but to be honest I can’t say I understand exactly how this is done; The allocator is broke down into several pieces as you can see, and that makes things a bit confusing to me.

    make_custom_alloc_handler() <— what exactly does it do?
    What’s shared_from_this()??
    I see that a “session” object has the member “handler_allocator allocator_”. Does every “session” object holds a pool of these objects?! Can I have one of this, at the “server” class level which will be shared or something?

    “allocator” example code:

    //
    // server.cpp
    // ~~~~~~~~~~
    //
    // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    //
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    //
    
    #include <cstdlib>
    #include <iostream>
    #include <boost/aligned_storage.hpp>
    #include <boost/array.hpp>
    #include <boost/bind.hpp>
    #include <boost/enable_shared_from_this.hpp>
    #include <boost/noncopyable.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/asio.hpp>
    
    using boost::asio::ip::tcp;
    
    // Class to manage the memory to be used for handler-based custom allocation.
    // It contains a single block of memory which may be returned for allocation
    // requests. If the memory is in use when an allocation request is made, the
    // allocator delegates allocation to the global heap.
    class handler_allocator
        : private boost::noncopyable
    {
    public:
        handler_allocator()
            : in_use_(false)
        {
        }
    
        void* allocate(std::size_t size)
        {
            if (!in_use_ && size < storage_.size)
            {
                in_use_ = true;
                return storage_.address();
            }
            else
            {
                return ::operator new(size);
            }
        }
    
        void deallocate(void* pointer)
        {
            if (pointer == storage_.address())
            {
                in_use_ = false;
            }
            else
            {
                ::operator delete(pointer);
            }
        }
    
    private:
        // Storage space used for handler-based custom memory allocation.
        boost::aligned_storage<1024> storage_;
    
        // Whether the handler-based custom allocation storage has been used.
        bool in_use_;
    };
    
    // Wrapper class template for handler objects to allow handler memory
    // allocation to be customised. Calls to operator() are forwarded to the
    // encapsulated handler.
    template <typename Handler>
    class custom_alloc_handler
    {
    public:
        custom_alloc_handler(handler_allocator& a, Handler h)
            : allocator_(a),
            handler_(h)
        {
        }
    
        template <typename Arg1>
        void operator()(Arg1 arg1)
        {
            handler_(arg1);
        }
    
        template <typename Arg1, typename Arg2>
        void operator()(Arg1 arg1, Arg2 arg2)
        {
            handler_(arg1, arg2);
        }
    
        friend void* asio_handler_allocate(std::size_t size,
            custom_alloc_handler<Handler>* this_handler)
        {
            return this_handler->allocator_.allocate(size);
        }
    
        friend void asio_handler_deallocate(void* pointer, std::size_t /*size*/,
            custom_alloc_handler<Handler>* this_handler)
        {
            this_handler->allocator_.deallocate(pointer);
        }
    
    private:
        handler_allocator& allocator_;
        Handler handler_;
    };
    
    // Helper function to wrap a handler object to add custom allocation.
    template <typename Handler>
    inline custom_alloc_handler<Handler> make_custom_alloc_handler(
        handler_allocator& a, Handler h)
    {
        return custom_alloc_handler<Handler>(a, h);
    }
    
    class session
        : public boost::enable_shared_from_this<session>
    {
    public:
        session(boost::asio::io_service& io_service)
            : socket_(io_service)
        {
        }
    
        tcp::socket& socket()
        {
            return socket_;
        }
    
        void start()
        {
            socket_.async_read_some(boost::asio::buffer(data_),
                make_custom_alloc_handler(allocator_,
                boost::bind(&session::handle_read,
                shared_from_this(),
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred)));
        }
    
        void handle_read(const boost::system::error_code& error,
            size_t bytes_transferred)
        {
            if (!error)
            {
                boost::asio::async_write(socket_,
                    boost::asio::buffer(data_, bytes_transferred),
                    make_custom_alloc_handler(allocator_, boost::bind(&session::handle_write, shared_from_this(), boost::asio::placeholders::error))
                    );
            }
        }
    
        void handle_write(const boost::system::error_code& error)
        {
            if (!error)
            {
                socket_.async_read_some(boost::asio::buffer(data_),
                    make_custom_alloc_handler(allocator_,
                    boost::bind(&session::handle_read,
                    shared_from_this(),
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred)));
            }
        }
    
    private:
        // The socket used to communicate with the client.
        tcp::socket socket_;
    
        // Buffer used to store data received from the client.
        boost::array<char, 1024> data_;
    
        // The allocator to use for handler-based custom memory allocation.
        handler_allocator allocator_;
    };
    
    typedef boost::shared_ptr<session> session_ptr;
    
    class server
    {
    public:
        server(boost::asio::io_service& io_service, short port)
            : io_service_(io_service),
            acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
        {
            session_ptr new_session(new session(io_service_));
            acceptor_.async_accept(new_session->socket(),
                boost::bind(&server::handle_accept, this, new_session,
                boost::asio::placeholders::error));
        }
    
        void handle_accept(session_ptr new_session,
            const boost::system::error_code& error)
        {
            if (!error)
            {
                new_session->start();
                new_session.reset(new session(io_service_));
                acceptor_.async_accept(new_session->socket(),
                    boost::bind(&server::handle_accept, this, new_session,
                    boost::asio::placeholders::error));
            }
        }
    
    private:
        boost::asio::io_service& io_service_;
        tcp::acceptor acceptor_;
    };
    
    int main(int argc, char* argv[])
    {
        try
        {
            if (argc != 2)
            {
                std::cerr << "Usage: server <port>\n";
                return 1;
            }
    
            boost::asio::io_service io_service;
    
            using namespace std; // For atoi.
            server s(io_service, atoi(argv[1]));
    
            io_service.run();
        }
        catch (std::exception& e)
        {
            std::cerr << "Exception: " << e.what() << "\n";
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <cstdlib> #include<iostream> using namespace std; template <typename T> class stack { public: T
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
#include <cstdlib> #include <iostream> #include <string> #include <string.h> using namespace std; class Portfolio{ public
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
#include <iostream> #include <cassert> #include <vector> #include <ctime> #include <cstdlib> #include <Windows.h> using namespace
Code looks like that #include <cstdlib> #include <iostream> using namespace std; #define n 3;
Take the following program: #include <cstdlib> using std::rand; #include <iostream> using std::cout; int main()
#include <iostream> #include <cstdlib> using std::cout; using std::endl; using std::rand; int man(){ int t=rand();
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int FILENAME_MAX=20; int main()

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.