Start New Discussion within our Software Development Community

I am trying to write some simple programs related to network
and I boost::asio as a tool to start with
Below is one of the example from a blog(I alter it a little bit)

void OnConnect(const boost::system::error_code & ec)
{}

void asio_7b()
{
  asio::io_service io_service;      
  asio::ip::tcp::socket sock(std::ref(io_service) );
  try
  {
    boost::asio::ip::tcp::resolver resolver( std::ref(io_service) );
    boost::asio::ip::tcp::resolver::query query( 
            "www.google.com", 
            "80"
    );
    
    boost::asio::ip::tcp::endpoint endpoint = resolver.resolve( query );
			
    std::cout << "Connecting to: " << endpoint << std::endl;
		
    sock.async_connect( endpoint, boost::bind( OnConnect, _1) ); #1
  }
  catch( std::exception & ex )
  {    
    std::cout << "[" << boost::this_thread::get_id()
              << "] Exception: " << ex.what() << std::endl;    
  }

  std::cin.get();

  boost::system::error_code ec;
  sock.shutdown( boost::asio::ip::tcp::socket::shutdown_both, ec ); #2
  sock.close( ec );

  io_service.stop();
}

#1 : where are the parameter of the place holder 1? I haven't seen any parameter
pass to it.

#2 : what if I neglect shutdown but close the socket directly?

I would like to develop a simple p2p program in the future,
this is a game and an exercise for me since this could
force me to take care of the problems of synchronous, asynchronous and
multi-threads.

Thanks a lot.
Thank you very much.

sorry, this program has a bug
I should call io_service.run() after #1
because

Asynchronous completion handlers will only be called from threads that are currently calling io_service::run().

This article has been dead for over six months. Start a new discussion instead.