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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:59:33+00:00 2026-06-17T13:59:33+00:00

I have following server code. which waits for client to connect and once the

  • 0

I have following server code. which waits for client to connect and once the client connects it will start thread which receives data from client connected and main thread will wait for another client to connect. This code works fine.

Now I have to specify that server will wait for some time say 10 sec to receive data from connected client. or otherwise server will close the communication if no data received within specified time. I have implemented timer for the same but somehow it doesnt work and timer_callback is not called after time elapsed.

#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");
        }
    }
}

void timer_callback(boost::shared_ptr<tcp::socket> my_socket, const boost::system::error_code& error)
{
    std::cout << "timer expired.... " << std::endl;
    if (error != boost::asio::error::operation_aborted)
    {
        // do something
        std::string str_error = "timer expired  " + error.message();
        my_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
        my_socket->close();
    }
    else
    {
        std::cout << "timer cancelled ..." << error.message() << std::endl;
    }
}

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

    try
    {
        for (uint32_t i = 0;; i++)
        {

            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);

            usleep(10000);

            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;

            timer.expires_from_now(boost::posix_time::seconds(10));
            timer.async_wait(
                    boost::bind(&timer_callback, my_socket, boost::asio::placeholders::error));

            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-17T13:59:35+00:00Added an answer on June 17, 2026 at 1:59 pm

    It looks like you want to do the accept and async_wait from the main thread and do some blocking read on the separate thread.

    There are multiple problems with that approach. First your accept is blocking, so the timer can’t run while the main thread waits for an accept. Also there is no call to io_service run or poll, so the async handler will never be called.

    You have to use async_accept if you want to mix it with async_wait in the same thread.

    However you still can’t cancel a blocking read on another thread. The documentation states that it’s unsafe to use a socket from multiple threads simultaneously (Shared objects: Unsafe).

    I would recommend that you use a fully asynchronous design.

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

Sidebar

Related Questions

I have the following code which easily connects to the FTP server and opens
I have come up with the following code to call a webservice using client/server
I have simple client/server SSL code which worked fine on Python 3.2. However, I
I have the following basic server/client java code, that works when server and client
I have the following code which bypass the proxy server for local machine and
I have the following code which sets up the SMTP server: ini_set(send_from, test@gmail.com); ini_set(SMTP,
I have the following code which handles files upload on the server. But how
I have the following code which works fine on my Windows 2003 server: static
I have the following code which is working fine <asp:TemplateField> <ItemTemplate> <div runat=server id=divFileName
I have the following code which posts file to ASP.net page: using (var Client

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.