用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 的问题.现在对该问题做一下可能出现的问题 ...
随机推荐
- Android有效解决加载大图片时内存溢出的问题
首先,您需要了解一下,图片占用内存的计算方法,传送门:http://blog.csdn.net/scry5566/article/details/11568751 尽量不要使用setImageBitm ...
- python中multiprocessing.pool函数介绍_正在拉磨_新浪博客
python中multiprocessing.pool函数介绍_正在拉磨_新浪博客 python中multiprocessing.pool函数介绍 (2010-06-10 03:46:5 ...
- 期权put和call
今天和朋友谈到了FB的期权操作问题,保证一个固定收益或者得到一个以低价买入FB的机会. 首先说下基本概念:call:是指买权put :是指卖权但是这两种期权又分别对应了long和short的两种操作, ...
- android大牛高焕堂最新力作-android架构师之路
android大牛高焕堂 个人介绍: Android专家顾问,台湾Android论坛主席,现任亚太地区Android技术大会主席,台湾Android领域框架开发联盟总架构师.发表100多篇Androi ...
- 嵌入式环境:挂载开发板根NFS文件系统失败
挂载开发板根NFS文件系统的时候,发现了下面的问题: Looking up port of RPC 100003/2 on 192.168.2.109rpcbind: server 192.168.2 ...
- c: c代码书写规范
排版: 较长的语句或函数过程参数(>80字符)要分成多行书写, 长表达式要在低优先级操作符处划分新行,操作符放在新行之首, 划分出的新行要进行适当的缩进,使排版整齐,语句可读 参考: 1. 运算 ...
- 32位程序在64位系统上获取系统安装时间(要使用KEY_WOW64_64KEY标记)
众所周知,取系统的安装时间可取注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion的子项InstallDate,此值是个 ...
- <脱机手写汉字识别若干关键技术研究>
脱机手写汉字识别若干关键技术研究 对于大字符集识别问题,一般采用模板匹配的算法,主要是因为该算法比较简单,识别速度快.但直接的模板匹配算法往往无法满足实际应用中对识别精度的需求.为此任俊玲编著的< ...
- 基于visual Studio2013解决C语言竞赛题之0801信息输出
题目
- PredictionIO Open Source Machine Learning Server
PredictionIO Open Source Machine Learning Server Build Smarter Software with Machine Learning Predic ...