Server::Server(boost::asio::io_service& io_service,std::string ip,short port,std::shared_ptr<ConnectionFactory> factory)
	: acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(ip.data()), port)){

		m_factory = factory;
		start_accept();

		std::cout<<"Socket accepting connections..."<<std::endl;
}

Server::~Server()
{
}


void Server::start_accept(){
	boost::asio::io_service io_service;
	std::shared_ptr<Connection> conn = m_factory->create(io_service);

	acceptor_.async_accept(conn->socket(),
		boost::bind(&Server::handle_accept, this,conn,
          boost::asio::placeholders::error));
	
}


void Server::handle_accept(std::shared_ptr<Connection> conn,const boost::system::error_code& error){

    if (!error)
    {
		std::cout<<"on connected"<<std::endl;
		conn->OnConnected();
		start_accept();
    }
  }

when i run the project i got

Unhandled exception at 0x00c55c8c in AccountServer.exe: 0xC0000005: Access violation reading location 0xfeeeff02.

and the call stack here

>

AccountServer.exe!boost::asio::detail::win_iocp_io_service::register_handle(void * handle, boost::system::error_code & ec)  Line 135 + 0x9 bytes	C++

so what's wrong here?!!

I've just started playing with boost and have implemented both the async server and client sides. One thing that I see wrong is the declaration of io_service inside of start_accept(). The io_service must exist outside start_accept so that you can eventually call io_service.run() and actually perform the accept().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.