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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:49:50+00:00 2026-05-19T00:49:50+00:00

I am trying to Create a simple Tcp Server in C++ using Boost ASio

  • 0

I am trying to Create a simple Tcp Server in C++ using Boost ASio Library. I wrote two classes TcpConnection and TcpServer.

The behaviour I need is that The Tcp Server should be able to send messages to all the connected clients and clients should be able to register/deregister with the server.

I was able to achieve the message sending from server. I was unsuccessful with the reading from clients part. My client is written in java, using apache mina.

Server Code

message = message + "\r\n"; 
const int bytesToSend = message.length(); 
boost::system::error_code error;
boost::asio::write(socket, boost::asio::buffer(message, bytesToSend), boost::asio::transfer_all(), error);

Client code

ConnectFuture future = ioConnector.connect(new InetSocketAddress(Port),
                new TriggerReceiverHandler();
System.out.println("Message Receiver started and listening on port "+ Port);
IoSession session = future.getSession();
session.write(new String("TEst Message From Client"));

On the server the code to read the messages is done using the async_read

boost::array<char, 1> buf;
    boost::asio::async_read(socket, boost::asio::buffer(buf),
        boost::bind(&TcpConnection::handleRead, this, buf, boost::asio::placeholders::error));


void TcpConnection::handleRead(boost::array<char, 1> buf, const boost::system::error_code& error)
{
    if(!error)
    {
        std::cout << "Message: " << buf.data() << std::endl;
    }
    else
    {
        std::cout << "Error occurred." << std::endl;
    }
}

But the handleRead is not getting called when I trigger a message by writing to the session from the client. Please tell me if I am doing anything wrong.

I would appreciate any Help..Please let me know if any further info is needed.

Thanks in advance.

  • 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-19T00:49:50+00:00Added an answer on May 19, 2026 at 12:49 am

    Firstly this appears to be incorrect!

    boost::array<char, 1> buf;
    

    You are declaring an array of size 1! I’d suggest something bigger!

    boost::array<char, 1500> buf;
    

    Aside from the buffer, I can’t see anything wrong with your code, just check that you are not interleaving async_read calls on the same socket.

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

Sidebar

Related Questions

I'm trying to create simple function that will work two ways. Basically, when I
Im trying to create something that looks like this using php gd library: I've
I am trying to create a simple menu system using repeaters. In the system
I am trying to create simple GPS application just to display simple data in
I was trying to create simple program that would perform some trivial operation with
I am trying to write a simple IO::Socket connection in perl. However, I am
I'm trying to get a basic Apache 2.2 load balancer running on top of
I create my host with the endpointaddress it needs to use, like this: Uri
I've had a similar question but the answer did not entirely fix my problem.

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.