Boost Asio connect_async永不调用处理程序 [英] Boost Asio connect_async never call hander

查看:376
本文介绍了Boost Asio connect_async永不调用处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个需要异步方法的网络应用程序.现在,我正在尝试Boost Asio的实现并搜索了示例代码.但是我发现的所有示例都使用带有boost :: bind的类方法作为处理程序.我想为处理程序使用一个更简单的普通c样式函数,而不是类方法.我尝试了下面的代码,编译顺利,但是在运行时从未调用过处理程序on_connect().

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?

谢谢.

推荐答案

在调用_getch();

这篇关于Boost Asio connect_async永不调用处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆