Question

I'm trying to develop a network application which requires an async method. Now I'm trying Boost Asio for the implementation and searched sample code. But all samples I found is using class methods with boost::bind for handlers. I want to use a simpler normal c-style function for the handler, not class method. I tried following code and compile finished fine, but the hander on_connect() was never called in runtime.

#include <iostream>
#include <conio.h>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost;
using namespace boost::asio;

void on_connect(const system::error_code& error)
{
    cout << "on_connect called!" << endl;
}

int main()
{
    io_service io_service;
    ip::tcp::socket socket(io_service);
    socket.async_connect(ip::tcp::endpoint(ip::address::from_string("192.168.0.2"), 12345), &on_connect);
    _getch();
}

How should I fix the code to call the handler correctly?

Thanks in advance.

Was it helpful?

Solution

Callio_service.run(); after the call to _getch();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top