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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:28:53+00:00 2026-05-16T00:28:53+00:00

I am learning boost/asio ad wrote 2 programs(client and server) from e-book with minor

  • 0

I am learning boost/asio ad wrote 2 programs(client and server) from e-book with minor changes.
Basically it should connect to my server.
When i try to connect to outside world(some random http server ) all is good and it works but when i change destination to “localhost:40002” it says invalid argument.

client code:

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

int main () {
   try {
      boost::asio::io_service io_service;
      boost::asio::ip::tcp::resolver::query query("localhost", 40002);
      boost::asio::ip::tcp::resolver resolver(io_service);
      boost::asio::ip::tcp::resolver::iterator destination = resolver.resolve(query);
      boost::asio::ip::tcp::resolver::iterator end ;
      boost::asio::ip::tcp::endpoint endpoint;

      while ( destination != end ) {
         endpoint = *destination++;
         std::cout<<endpoint<<std::endl;
      }

      boost::asio::ip::tcp::socket socket(io_service);
      socket.connect(endpoint);
   }
   catch (std::exception& e)
   {
      std::cerr << e.what() << std::endl;
   }
   return 0;
}

i did “netstat -l” and it showed that i am truly listening to my port so server i think works to but never less they don’t connect

server code:

#include <boost/asio.hpp>
#include <iostream>
#include <string>
#include <ctime>
std::string time_string()
{
   using namespace std;
   time_t now = time(0);
   return ctime(&now);
}
int main () {

   try {
      boost::asio::io_service io_service;
      boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 40002));

      for (; ;) {
         std::cout<<"Listening to"<<std::endl;
         boost::asio::ip::tcp::socket socket(io_service);
         acceptor.accept(socket);

         std::string message = time_string();
         boost::system::error_code ignored_error;
         boost::asio::write(socket, boost::asio::buffer(message), boost::asio::transfer_all(), ignored_error);
      }
   }
   catch (std::exception& e)
   {
      std::cerr << e.what() << std::endl;
   }
   return 0;

}

Can someone hint why connection doesn’t happen

  • 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-16T00:28:54+00:00Added an answer on May 16, 2026 at 12:28 am

    The second parameter to ip::tcp::resolver::query is the service name, not a port number:

      boost::asio::ip::tcp::resolver::query query("localhost", 40002);
    

    should be

      boost::asio::ip::tcp::resolver::query query("localhost", "40002");
    

    fyi, when I compiled your code on my system it failed:

    resolve.cc: In function ‘int main()’:
    resolve.cc:7: error: invalid conversion from ‘int’ to ‘boost::asio::ip::resolver_query_base::flags’
    resolve.cc:7: error:   initializing argument 2 of ‘boost::asio::ip::basic_resolver_query<InternetProtocol>::basic_resolver_query(const std::string&, boost::asio::ip::resolver_query_base::flags) [with InternetProtocol = boost::asio::ip::tcp]’
    

    I’m surprised it compiled for you.

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

Sidebar

Related Questions

I am learning boost/asio and writing example program which was in e-book. of course
Learning boost, and compiled their daytime server client example . Since I cant use
I am learning to use Boost ASIO. Here is some code copied from the
I'm learning how to use Boost:asio library with Serial Port. I wrote some code
I am learning how to use the boost asio libraries and I am using
I am learning the boost::lambda library and for that I wrote this sample code
Learning some VBA. So far, I've constructed this piece of code which should allow
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I'm learning the use of boost smart pointers but I'm a bit confused about
I've been playing around with boost threads today as a learning exercise, and I've

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.