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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:54+00:00 2026-05-23T17:40:54+00:00

Creating a simple TCP server based on examples but still do not get how

  • 0

Creating a simple TCP server based on examples but still do not get how to create a socket that would read some amount of bytes and if there will not be enough would wait. I need this to be NOT asynchronous operation.

#include <iostream>
#include <boost/asio.hpp>

#ifdef _WIN32
#include "Windows.h"
#endif

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

int main(){
    int m_nPort = 12345;
    boost::asio::io_service io_service;
    tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));

    cout << "Waiting for connection..." << endl;

    tcp::socket socket(io_service);
    acceptor.accept(socket);
    cout << "connection accepted" << endl;
    try
    {
        socket.send(boost::asio::buffer("Start sending me data\r\n"));
    }
    catch(exception &e)
    {
        cerr << e.what() << endl; //"The parameter is incorrect" exception
    }
}

How to receive 10000 bytes and do it either until all 10000 arrive OR 1000 millisecond timeout and throw an exception?

  • 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-23T17:40:54+00:00Added an answer on May 23, 2026 at 5:40 pm

    Boost 1.47.0 just introduced a timeout feature for basic_socket_iostream, namely, the expires_at and expires_from_now methods.

    Here’s an example based on your snippet:

    #include <iostream>
    #include <boost/asio.hpp>
    
    using namespace boost::asio::ip;
    using namespace std;
    
    int main(){
        int m_nPort = 12345;
        boost::asio::io_service io_service;
        tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), m_nPort));
    
        cout << "Waiting for connection..." << endl;
    
        tcp::iostream stream;
        acceptor.accept(*stream.rdbuf());
        cout << "Connection accepted" << endl;
        try
        {
            stream << "Start sending me data\r\n";
    
            // Set timeout in 5 seconds from now
            stream.expires_from_now(boost::posix_time::seconds(5));
    
            // Try to read 12 bytes before timeout
            char buffer[12];
            stream.read(buffer, 12);
    
            // Print buffer if fully received
            if (stream) // false if read timed out or other error
            {
                cout.write(buffer, 12);
                cout << endl;
            }
        }
        catch(exception &e)
        {
            cerr << e.what() << endl;
        }
    }
    

    This program works for me on Linux.

    Please note that I’m not advocating that you use timeouts instead of asynchronous operation with a deadline timer. It’s up to you to decide. I just wanted to show that timeouts are possible with basic_socket_iostream.

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

Sidebar

Related Questions

Creating a simple public/private keypair, such as through ssh-keygen does not create a file
I'm creating a simple boolean query parser. I would like to do something like
While creating a simple client for a REST service which I have stubbed out,
Im creating a simple Rails 2.3.8 gem and need a migration file inside it.
Im creating a simple message board with php/mysql. Users enter their name and a
I'm creating a simple application using the Kohana PHP framework, just FYI. This is
I have started creating a simple product image carousel. It basically comprises of 3
I'm creating a simple WPF print dialog box to setup a label printer. I
I'm creating a simple blackberry application for testing purposes and my custom buttons do
I'm creating a simple pie chart with three data points. The first and second

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.