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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:38:19+00:00 2026-05-22T14:38:19+00:00

I have written a program that accepts N client connections and then writes data

  • 0

I have written a program that accepts N client connections and then writes data into them. The problem I am having now is: I can only write to N-1 clients, the first one is never written to. I have no idea why this is happening and so I wish some of you might be able to provide some assistance.

I have provided the portion of code that may be associated with this problem:

void ClientPartitionServer::AcceptClientConnections(int port) {
  cout << "Listening to connections..." << endl;
  cout << "Number of PartitionInstanceConnections: " <<
    m_partitionInstanceConnections.size() << endl;
  m_acceptor = new boost::asio::ip::tcp::acceptor(m_IOService);
  m_endpoint = new boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),
    m_port);
  m_acceptor->open(m_endpoint->protocol());
  m_acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  m_acceptor->bind(*m_endpoint);
  m_acceptor->listen();
  boost::asio::ip::tcp::socket* acceptingSocket =
    new boost::asio::ip::tcp::socket(m_IOService);
  m_acceptor->async_accept(*acceptingSocket, boost::bind(
    &ClientPartitionServer::HandleAccept, this, acceptingSocket,
    boost::asio::placeholders::error));
}
void ClientPartitionServer::HandleAccept(boost::asio::ip::tcp::socket* socket,
    const boost::system::error_code& error) {
  cout << "Connection established..." << endl;
  m_clientSockets.push_back(new boost::asio::ip::tcp::socket(m_IOService));
  cout << m_clientSockets.back()->is_open() << endl;
  ++m_clientSocketsCounter;
  cout << "ClientPartitionServer identifier: " << m_identifier << endl;
  cout << "Client connected on port:         " << m_port << endl;
  cout << "Number of clients on port:        " << m_clientSocketsCounter <<
    endl;
  m_acceptor->async_accept(*m_clientSockets.back(), boost::bind(
    &ClientPartitionServer::HandleAccept, this, m_clientSockets.back(),
    boost::asio::placeholders::error));
}

void ClientPartitionServer::HandleSignal(char* content, int transferSize,
    int identifier) {
  if(identifier == m_identifier) {
    TransferToQueueBuffer(content, transferSize);
    if(m_writeCompleteFlag) {
      TransferToWriteBuffer(m_queueBuffer, m_queueBufferSize);
      if(m_clientSockets.size() != 0) {
        for(vector<boost::asio::ip::tcp::socket*>::const_iterator i =
            m_clientSockets.begin(); i != m_clientSockets.end(); ++i) {
          WriteToClient(m_writeBuffer, m_queueBufferSize, *i);
        }
      }
    }
  }
}

void ClientPartitionServer::WriteToClient(char* content, int transferSize,
    boost::asio::ip::tcp::socket* clientSocket) {
  boost::lock_guard<boost::mutex> lock(m_writeToClientMutex);
  m_writeCompleteFlag = false;
  boost::asio::async_write(*clientSocket, boost::asio::buffer("ABC ",
    4), boost::bind(&ClientPartitionServer::HandleWrite,
      this, boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));
}

void ClientPartitionServer::HandleWrite(const boost::system::error_code& ec,
    size_t bytes_transferred) {
  cout << "handlewrite" << endl;
  m_writeCompleteFlag = true;
}

Thank you for any assistance.

  • 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-22T14:38:20+00:00Added an answer on May 22, 2026 at 2:38 pm

    The first async_accept() is called on acceptingSocket which is new‘d in AcceptClientConnections() and leaked.

    The subsequent async_accept()s are called on sockets that are new‘d in HandleAccept() and push_back()‘ed into m_clientSockets.

    WriteToClient() is executed only on the sockets found in m_clientSockets, never on the first socket.

    Solution: push_back that first socket in AcceptClientConnections() into m_clientSockets too.

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

Sidebar

Related Questions

I have written an ActionScript client program that tries to connect to a local
I have written a program that in the client section an error occures frequently,
I have written a php program that generates rss feeds, however I am having
I have written PLSQL program that generates table having usually more than 200 columns.
I have written a program that relies on a proxy to function. I now
I have written a program that generates random expressions and then uses genetic techniques
I have written a program that will etablish a network connection with a remote
I have written a program that uses qhttp to get a webpage. This works
I have written a java program that is actually works as a gui to
I have written a c# program that calls a c++ dll that echoes the

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.