用asio的定时器实现带超时的connect,备忘
- // 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;
- }
用asio的定时器实现带超时的connect,备忘的更多相关文章
- Poj 3356 ACGT(LCS 或 带备忘的递归)
题意:把一个字符串通过增.删.改三种操作变成另外一个字符串,求最少的操作数. 分析: 可以用LCS求出最大公共子序列,再把两个串中更长的那一串中不是公共子序列的部分删除. 分析可知两个字符串的距离肯定 ...
- boost::asio学习(定时器)
#include <boost/asio.hpp> #include <iostream> void handle1(const boost::system::error_co ...
- boost::asio::io_service::定时器任务队列
使用io_service和定时器写的一个同步和异步方式的任务队列 #pragma once #include <string> #include <iostream> #inc ...
- 重构file_get_contents实现一个带超时链接访问的函数
function wp_file_get_contents($url, $timeout = 30) { $context = stream_context_create(array( 'http' ...
- Java发送邮件必带超时时间配置
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在线上遇到了一个发送邮件的问题,记录一下. 一.先说 ...
- 重构file_get_contents实现一个带超时POST传值函数
function wp_file_post_contents($url, $post = null) { $context = array(); if (is_array($post)) { ksor ...
- 备忘DES带向量的加密和解密与DES简单加密与解密
package com.ego.util; import java.security.Key; import java.security.SecureRandom; import java.secur ...
- 备忘-Sql server Timeout expired 超时时间已到. 达到了最大池大小 错误及Max Pool Size设置
select * from sysprocesses where dbid= db_id('数据库名') 通过此语句可查看目前所有的连接进程 不够了就必须设置Max Pool Size,理论最大值为3 ...
- 服务器22端口连接超时 ssh: connect to host *** port 22: Operation timed out
最近酸酸乳出问题,连接v社服务器发现碰到 ssh: connect to host master port 22: Connection timed out 的问题.现在对该问题做一下可能出现的问题 ...
随机推荐
- HDU 3923 Invoker 【裸Polya 定理】
参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...
- struts2中根对象以及ognl .
Struts2中的OGNL表达式语言是对Xwork的OGNL的封装.我们要理解一下几点: 1. Struts2中将ActionContext作为OGNL的上下文环境(ActionContext内部含有 ...
- DG创建和提取虚拟机文件
http://www.cr173.com/soft/33359.html http://www.diskgenius.cn/help/newvmdk.php
- linux 学习:环境变量设置
一.临时环境变量 临时环境变量,只对当前打开的shell生效,shell关闭后,环境变量失效. 设置方法一: 分两步 MYPARA=hello export MYPARA 设置方法二:一步完成 exp ...
- mong 备份和恢复
[root@hy-mrz01 bin]# ./mongofiles list -h114.55.5.57 -db pics 20160602152850deeabcb1bd2644afa0c3a9a8 ...
- 《白手起家Win32SDK应用程序》(完整版+目录)
<白手起家Win32SDK应用程序> 目 录 <白手起家Win32SDK应用程序> 第一篇.预备知识 第二篇.创建Win32工程和主函数 第三篇.增加一个回调函数 第四篇.注册 ...
- T-sql表表达式
内联表值函数 可以理解是个带参数的视图的表达式,好处就是创建后,可永久保存在数据库中,查询复用. 创建的格式: create function 函数名 (参数名 as 参数类型) return tab ...
- redis安装及数据类型简介(string、list、set、sorted_set、hash)
一:简介: redis国内最大的案例--->新浪微博 memcache:是key-value数据库 数据类型:只支持key value数据 过期策略:支持 持久化:不支持(可以通过三方程序) 主 ...
- Kivy: Crossplatform Framework for NUI
Kivy: Crossplatform Framework for NUI ivy - Open source Python library for rapid development of appl ...
- Codeforces Round #189 (Div. 2)
题目地址:http://codeforces.com/contest/320 第一题:基本题,判断mod 1000,mod 100.,mod 10是不是等于144.14.1,直到为0 代码如下: #i ...