1. // test.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include <boost/asio.hpp>
  5. #include <boost/bind.hpp>
  6. #include <boost/date_time/posix_time/posix_time_types.hpp>
  7. #include <iostream>
  8. using namespace boost::asio;
  9. using boost::asio::ip::tcp;
  10. class connect_handler
  11. {
  12. public:
  13. connect_handler(io_service& ios)
  14. : io_service_(ios),
  15. timer_(ios),
  16. socket_(ios)
  17. {
  18. socket_.async_connect(
  19. tcp::endpoint(boost::asio::ip::address_v4::loopback(), 3212),
  20. boost::bind(&connect_handler::handle_connect, this,
  21. boost::asio::placeholders::error));
  22. timer_.expires_from_now(boost::posix_time::seconds(5));
  23. timer_.async_wait(boost::bind(&connect_handler::close, this));
  24. }
  25. void handle_connect(const boost::system::error_code& err)
  26. {
  27. if (err)
  28. {
  29. std::cout << "Connect error: " << err.message() << "\n";
  30. }
  31. else
  32. {
  33. std::cout << "Successful connection\n";
  34. }
  35. }
  36. void close()
  37. {
  38. socket_.close();
  39. }
  40. private:
  41. io_service& io_service_;
  42. deadline_timer timer_;
  43. tcp::socket socket_;
  44. };
  45. int main()
  46. {
  47. try
  48. {
  49. io_service ios;
  50. tcp::acceptor a(ios, tcp::endpoint(tcp::v4(), 32123), 1);
  51. // Make lots of connections so that at least some of them will block.
  52. connect_handler ch1(ios);
  53. //connect_handler ch2(ios);
  54. //connect_handler ch3(ios);
  55. //connect_handler ch4(ios);
  56. //connect_handler ch5(ios);
  57. //connect_handler ch6(ios);
  58. //connect_handler ch7(ios);
  59. //connect_handler ch8(ios);
  60. //connect_handler ch9(ios);
  61. ios.run();
  62. }
  63. catch (std::exception& e)
  64. {
  65. std::cerr << "Exception: " << e.what() << "\n";
  66. }
  67. return 0;
  68. }

用asio的定时器实现带超时的connect,备忘的更多相关文章

  1. Poj 3356 ACGT(LCS 或 带备忘的递归)

    题意:把一个字符串通过增.删.改三种操作变成另外一个字符串,求最少的操作数. 分析: 可以用LCS求出最大公共子序列,再把两个串中更长的那一串中不是公共子序列的部分删除. 分析可知两个字符串的距离肯定 ...

  2. boost::asio学习(定时器)

    #include <boost/asio.hpp> #include <iostream> void handle1(const boost::system::error_co ...

  3. boost::asio::io_service::定时器任务队列

    使用io_service和定时器写的一个同步和异步方式的任务队列 #pragma once #include <string> #include <iostream> #inc ...

  4. 重构file_get_contents实现一个带超时链接访问的函数

    function wp_file_get_contents($url, $timeout = 30) { $context = stream_context_create(array( 'http' ...

  5. Java发送邮件必带超时时间配置

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在线上遇到了一个发送邮件的问题,记录一下. 一.先说 ...

  6. 重构file_get_contents实现一个带超时POST传值函数

    function wp_file_post_contents($url, $post = null) { $context = array(); if (is_array($post)) { ksor ...

  7. 备忘DES带向量的加密和解密与DES简单加密与解密

    package com.ego.util; import java.security.Key; import java.security.SecureRandom; import java.secur ...

  8. 备忘-Sql server Timeout expired 超时时间已到. 达到了最大池大小 错误及Max Pool Size设置

    select * from sysprocesses where dbid= db_id('数据库名') 通过此语句可查看目前所有的连接进程 不够了就必须设置Max Pool Size,理论最大值为3 ...

  9. 服务器22端口连接超时 ssh: connect to host *** port 22: Operation timed out

    最近酸酸乳出问题,连接v社服务器发现碰到 ssh: connect to host master port 22: Connection timed out 的问题.现在对该问题做一下可能出现的问题 ...

随机推荐

  1. A valid provisioning profile for this executable was not found.

  2. django perm用法

    定义用户model时可以给用户分配权限: class Meta: permissions = (       ("can_mark", "Can mark"), ...

  3. redis(三)redis+Keepalived主从热备秒级切换

    一 简介 安装使用centos 5.10 Master 192.168.235.135 Slave 192.168.235.152 Vip 192.168.235.200 编译环境 yum -y in ...

  4. Pathchirp—有效的带宽估计方法(二)

    上一个blog介绍了有效带宽估计方法:pathload.http://blog.csdn.net/ice110956/article/details/11126491. 做一个小小的总结:pathlo ...

  5. Android自定义组合控件:UIScrollLayout(支持界面滑动及左右菜单滑动)

    一.前言: 我之前很早的时候,写过一篇<左右滑出菜单>的文章: http://blog.csdn.net/qingye_love/article/details/8776650 用的是对V ...

  6. 基于visual Studio2013解决C语言竞赛题之0417四倍数

       题目 解决代码及点评 这道题目还是考察循环,通过循环遍历1234~9876,然后将每个数都用算法判断其是否符合条件#include <stdio.h> #include ...

  7. 快速解决PDF文档加密不能打印问题_百度经验

    快速解决PDF文档加密不能打印问题_百度经验     快速解决PDF文档加密不能打印问题         |        浏览:182        |        更新:2014-01-06 1 ...

  8. H面试程序(28):字符串处理转换

    //2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...

  9. 上证A股股指跌破1900

    上证A股股指跌破1900 有钱的同学赶紧买哦,机会难得哈哈!

  10. IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例

    IOS开发中UIBarButtonItem上按钮切换或隐藏案例实现案例是本文要介绍的内容,这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add bu ...