分享程序员开发的那些事...
首页
NTFS
漏洞
Android
Magento
服务器
Python
powershell
技术问答
Linux
网站首页
且构网 - 分享程序员编程开发的那些事
您所在的位置:
首页
>
用asio的定时器实现带超时的connect,备忘
用asio的定时器实现带超时的connect,备忘
更新时间:2022-09-17 14:05:38
// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <iostream>
using
namespace
boost::asio;
using
boost::asio::ip::tcp;
class
connect_handler
{
public
:
connect_handler(io_service& ios)
: io_service_(ios),
timer_(ios),
socket_(ios)
{
socket_.async_connect(
tcp::endpoint(boost::asio::ip::address_v4::loopback(), 3212),
boost::bind(&connect_handler::handle_connect,
this
,
boost::asio::placeholders::error));
timer_.expires_from_now(boost::posix_time::seconds(5));
timer_.async_wait(boost::bind(&connect_handler::close,
this
));
}
void
handle_connect(
const
boost::system::error_code& err)
{
if
(err)
{
std::cout <<
"Connect error: "
<< err.message() <<
"\n"
;
}
else
{
std::cout <<
"Successful connection\n"
;
}
}
void
close()
{
socket_.close();
}
private
:
io_service& io_service_;
deadline_timer timer_;
tcp::socket socket_;
};
int
main()
{
try
{
io_service ios;
tcp::acceptor a(ios, tcp::endpoint(tcp::v4(), 32123), 1);
// Make lots of connections so that at least some of them will block.
connect_handler ch1(ios);
//connect_handler ch2(ios);
//connect_handler ch3(ios);
//connect_handler ch4(ios);
//connect_handler ch5(ios);
//connect_handler ch6(ios);
//connect_handler ch7(ios);
//connect_handler ch8(ios);
//connect_handler ch9(ios);
ios.run();
}
catch
(std::exception& e)
{
std::cerr <<
"Exception: "
<< e.what() <<
"\n"
;
}
return
0;
}
上一篇 : :Boost Thread学习笔记二
下一篇 : 编译Boost 详细步骤 适用 VC6 VS2003 VS2005 VS2008 VS2010
相关阅读
用asio的定时器实现带超时的connect,备忘
推荐文章
[CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字
[LintCode] Perfect Squares 完全平方数
[CareerCup] 17.5 Game of Master Mind 猜字游戏
[CareerCup] 17.6 Sort Array 排列数组
[CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
[CareerCup] 17.12 Sum to Specific Value 和为特定数
[CareerCup] 17.9 Word Frequency in a Book 书中单词频率
[CareerCup] 17.3 Factorial Trailing Zeros 求阶乘末尾零的个数
[LeetCode] Transpose File 转置文件
[LeetCode] Word Frequency 单词频率