用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 的问题.现在对该问题做一下可能出现的问题 ...
随机推荐
- oracle数据库连接无响应的解决
昨天中午时,查询到服务器的数据流水最晚记录是早上8点的,现场查看服务日志很奇怪,日志输出显示挂死在数据库连接这一步.多次调试无果,随后百度发现有资料显示oracle 10.2.1的版本有登录无响应的B ...
- redis(二)redis+TCMALLOC高性能的缓存服务器的安装配置
安装 1准备编译环境 yum -y install gcc gcc+ gcc-c++ openssl openssl-devel pcre pcre-devel 2 下载源码包(由于goog ...
- ZooKeeper - Perl bindings for Apache ZooKeeper Perl绑定用于 Apache ZooKeeper
ZooKeeper - Perl bindings for Apache ZooKeeper Perl绑定用于 Apache ZooKeeper 监控 master/slave 需要使用zk的临时节点 ...
- Axis2(10):使用soapmonitor模块监视soap请求与响应消息
在Axis2中提供了一个Axis2模块(soapmonitor),该模块实现了与<WebService大讲堂之Axis2(9):编写Axis2模块(Module)>中实现的logging模 ...
- 第一次PS练习
嘿嘿,自己第一次的PS,虽然把在大神眼里是小KS,但是了,对我来说值得劲纪念.加油,我会努力的.
- Excel VBA ——批量工作表重命名
虽然平常在用excel 2010重命名工作表的时候,一般可能会用"双击工作表"的方法来重名,但是遇到大批量重名的时候就很麻烦. 我的方法,先建一张新表,然后在第一列写好要命名的表名 ...
- Ibatis调用存储过程实现增删改以及分页查询
1.Ibatis实现增删改操作很简单了,通常我是将某一模块的增删改功能写在一个存储过程里,通过一个标识符去区分执行增加还是修改抑或删除操作. statement: <!-- 存储过程:实现学生的 ...
- CentOS 如何安装git server + Gitolite 【配置不成功需要再测试2015-8-20】
安装git 关于安装git 可以参考 http://gitolite.com/gitolite/install.html 里面有官方的介绍 1. Git 的工作需要调用 curl,zlib,open ...
- WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的?
原文:WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的? 服务端只有抛出FaultException异常才能被正常地序列化成Fault消息,并实现向客户 ...
- 高级特性(7)- 高级AWT
7.1 绘图操作流程7.2 形状7.3 区域7.4 笔划7.5 着色7.6 坐标变换7.7 剪切7.8 透明与组合7.9 绘图提示7.10 图像的读取器和写入器 7.10.1 获得图像文件类型的读取器 ...