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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:25:28+00:00 2026-06-17T07:25:28+00:00

Below is my sample code for socket server using boost asio. This server will

  • 0

Below is my sample code for socket server using boost asio.

This server will wait on port 10001 for any client to connect. When any client connects it will start thread to read from that client and wait for another client. But what happens when my client disconnects the server socket hangs in my_socket->close() call.

And if new client tries to connect the server crashes.

I am using
g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <sys/socket.h>
#include <unistd.h>
#include <string>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>

using namespace std;
using boost::asio::ip::tcp;

void run(boost::shared_ptr<tcp::socket> my_socket)
{
    while (1)
    {
        char buf[128];
        boost::system::error_code error;

        size_t len = my_socket->read_some(boost::asio::buffer(buf, 128), error);
        std::cout << "len : " << len << std::endl;

        if (error == boost::asio::error::eof)
        {
            cout << "\t(boost::asio::error::eof)" << endl;
            if (my_socket->is_open())
            {
                boost::system::error_code ec;
                cout << "\tSocket closing" << endl;
                my_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
                cout << "\tShutdown " << ec.message() << endl;
//                cout << "normal close : " << ::close(my_socket->native_handle()) << endl;
                my_socket->close(ec);
                cout << "\tSocket closed" << endl;
            }
            break; // Connection closed cleanly by peer.
        }
        else if (error)
        {
            std::cout << "Exception : " << error.message() << std::endl;
            break;
        }
        else
        {
            for (unsigned int i = 0; i < len; i++)
                printf("%02x ", buf[i] & 0xFF);
            printf("\n");
        }
    }
}

int main()
{
    const int S = 1000;
    vector<boost::shared_ptr<boost::thread> > arr_thr(S);

    try
    {
        for (uint32_t i = 0;; i++)
        {
            boost::asio::io_service io_service;

            tcp::endpoint endpoint(tcp::v6(), 10001);

            boost::shared_ptr<tcp::socket> my_socket(new tcp::socket(io_service));
            tcp::endpoint end_type;

            tcp::acceptor acceptor(io_service, endpoint);

            std::cout << "before accept" << endl;
            acceptor.accept(*my_socket, end_type);

            std::cout << "connected... hdl : " << my_socket->native_handle() << std::endl;

            boost::asio::ip::address addr = end_type.address();
            std::string sClientIp = addr.to_string();

            std::cout << "\tclient IP : " << sClientIp << std::endl;
            arr_thr[i] = boost::shared_ptr<boost::thread>(new boost::thread(&run, my_socket));
        }
    } catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}
  • 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-17T07:25:30+00:00Added an answer on June 17, 2026 at 7:25 am

    After you start the run thread, the for loop in main starts again, destroying and reinitializing the local io_service variable, the next event on the socket will still assume the old io_service object though, leading to your crash.

    You should use only one instance of io_service.

    Also, you should have a look at the asynchronous functions which boost::asio provides, like async_accept and async_read, see for instance this example: http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/chat/chat_server.cpp

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

Sidebar

Related Questions

I have written grammar for a language (sample code below) //this is a procedure
I am building html at server side. The code sample is as below. My
I am writing a simple client program socket programming when compiling the code using
Below is the source code of a client and a server. The client just
UPDATE: Full code example: Server: ServerSocket serverSocket = new ServerSocket(8888); Socket client = serverSocket.accept();
Based on examples here , the simple socket server client will terminate when .bye
In the sample code below I am dividing by zero which when I step
Below is some sample code. The window does not resize (python3.2, on a Mac,
When I run the sample code below the width of JTextArea is fixed (100px)
I've put together a small code-sample below (Currently in C# 3.5 but would also

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.