#include <iostream>
#include <boost/asio.hpp>
#include <boost/config/compiler/visualc.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/foreach.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <cassert>
#include <exception>
#include <sstream>
#include <string>
#include <boost/thread.hpp> void test_http()
{
using namespace boost::asio;
ip::tcp::iostream stream; stream.expires_from_now(boost::posix_time::seconds());
stream.connect("www.boost.org", "http");
stream << "GET /LICENSE_1_0.txt HTTP/1.0\r\n";
stream << "Host: www.boost.org\r\n";
stream << "Accept: */*\r\n";
stream << "Connection: close\r\n\r\n"; stream.flush();
std::cout << stream.rdbuf();
} void udp_echo_client()
{ using boost::asio::ip::udp;
boost::asio::io_service my_io_service;
udp::socket s(my_io_service, udp::endpoint(udp::v4(), )); udp::resolver resolver(my_io_service);
} int read_file_content(std::string fileName, std::stringstream& ss)
{
namespace fs = boost::filesystem;
fs::path file(fileName);
if (!fs::exists(file))
{
std::cerr << "file does not exist." << std::endl;
return EXIT_FAILURE;
} //std::ifstream t(file) >> ss;
fs::ifstream t(file);
ss << t.rdbuf();
// boost::iostreams::mapped_file mf(file);
// mf.data() >> ss; return EXIT_SUCCESS;
} void print_json_data(boost::property_tree::ptree const& pt)
{
using boost::property_tree::ptree;
ptree::const_iterator end = pt.end();
for (ptree::const_iterator it = pt.begin(); it != end; it++)
{
std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
print_json_data(it->second);
}
} int parse_json_data()
{
try
{
std::stringstream ss; read_file_content("data.json",ss);
//ss << "{ \"root\": { \"values\": [1, 2, 3, 4, 5 ] } }"; std::cout << ss.str() << std::endl; boost::property_tree::ptree pt;
boost::property_tree::read_json(ss, pt); BOOST_FOREACH(boost::property_tree::ptree::value_type& v, pt.get_child("editorialMarket"))
{
assert(v.first.empty());
std::cout << v.second.data() << std::endl;
} print_json_data(pt);
}
catch (std::exception& e)
{
std::cerr << "error: " << e.what() << std::endl;
} return EXIT_SUCCESS;
} /*
函数 main() 首先定义了一个 I/O 服务 io_service,用于初始化 I/O 对象 timer。
就象 boost::asio::deadline_timer 那样,所有 I/O 对象通常都需要一个 I/O 服务作为它们的构造函数的第一个参数。
由于 timer 的作用类似于一个闹钟,所以 boost::asio::deadline_timer 的构造函数可以传入第二个参数,
用于表示在某个时间点或是在某段时长之后闹钟停止。 以上例子指定了五秒的时长,该闹钟在 timer 被定义之后立即开始计时
*/ void handler1(const boost::system::error_code& ec)
{
std::cout << "5 s" << std::endl;
for (int i = ; i < ; i++)
{
printf("%s: %d\n",__FUNCTION__, i);
boost::this_thread::sleep(boost::posix_time::seconds());
}
} void handler2(const boost::system::error_code& ec)
{
std::cout << "5 s" << std::endl;
for (int i = ; i < ;i++)
{
printf("%s: %d\n", __FUNCTION__, i);
boost::this_thread::sleep(boost::posix_time::seconds());
}
} boost::asio::io_service io_service1;
boost::asio::io_service io_service2;
void run1()
{
printf("in %s\n", __FUNCTION__);
io_service1.run();
} void run2()
{
printf("in %s\n", __FUNCTION__);
io_service2.run();
} void test_io_srv()
{
//boost::asio::io_service io_service;
//创建5s定时器
boost::asio::deadline_timer timer1(io_service1, boost::posix_time::seconds());
timer1.async_wait(handler1); //创建5s定时器
boost::asio::deadline_timer timer2(io_service2, boost::posix_time::seconds());
timer2.async_wait(handler2); //io_service.run();
boost::thread thread1(run1);
boost::thread thread2(run2); thread1.join();
thread2.join();
} int main()
{
//parse_json_data();
test_io_srv();
std::cout << "test_io_srv..." << std::endl;
}

Boost C++: 网络编程1的更多相关文章

  1. boost Asio网络编程简介

    :first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...

  2. Boost.Asio 网络编程([译]Boost.Asio基本原理)

    转自:https://m.w3cschool.cn/nlzbw/nlzbw-3vs825ya.html Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将 ...

  3. [Boost基础]并发编程——asio网络库——定时器deadline_timer

    asio库基于操作系统提供的异步机制,采用前摄器设计模式(Proactor)实现了可移植的异步(或者同步)IO操作,而且并不要求使用多线程和锁定,有些的避免了多线程编程带来的诸多有害副作用(如条件竞争 ...

  4. Boost.Asio c++ 网络编程翻译(26)

    Boost.Asio-其他特性 这章我们讲了解一些Boost.Asio不那么为人所知的特性.标准的stream和streambuf对象有时候会更难用一些,但正如你所见.它们也有它们的益处.最后,你会看 ...

  5. 有哪些适合学生参与的 C++,网络编程方面的开源项目?

    有哪些适合学生参与的 C++,网络编程方面的开源项目?   Tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通 ...

  6. Muduo 网络编程示例之零:前言

    陈硕 (giantchen_AT_gmail)Blog.csdn.net/Solstice Muduo 全系列文章列表: http://blog.csdn.net/Solstice/category/ ...

  7. 基于ASIO的协程与网络编程

    协程 协程,即协作式程序,其思想是,一系列互相依赖的协程间依次使用CPU,每次只有一个协程工作,而其他协程处于休眠状态.协程可以在运行期间的某个点上暂停执行,并在恢复运行时从暂停的点上继续执行. 协程 ...

  8. 使用juggle简化网络编程

    常规的网络编程,在消息处理上大概会采用如下方式 struct msg{ int msg_id; int msg_len; //...msg_info }; 定义如上的消息结构 接收方接收后,按如上的消 ...

  9. Socket网络编程--Libev库学习(1)

    这一节是安装篇. Socket网络编程不知不觉已经学了快两个月了.现在是时候找个网络库学学了.搜索了很多关于如何学网络编程的博客和问答.大致都是推荐学一个网络库,至于C++网络库有那么几个,各有各的好 ...

随机推荐

  1. Mobirise

    网站建设器Mobirise v1.9.2 免费版 - 绿色软件联盟 2015年9月5日 - 网站建设器Mobirise是一个用户友好的程序,使您可以构建桌面和移动网站,准备在Javas cript中. ...

  2. Eclipse Tomcat配置/管理/调试指南

    从myeclipse转到Eclipse最不方便的之一莫过于Web项目部署了,老是在想怎么不能把myeclipse的那个移植过来,或者有没有高人能按照Myeclipse开发一个,非常遗憾. 原版的Ecl ...

  3. python 批量重命名文件后缀

    # batch_file_rename.py # Created: 6th August 2012 ''' This will batch rename a group of files in a g ...

  4. java学习_文件工具类

    工具类里面的方法全部都是静态的,调用的时候不需要实例化

  5. ASP.NET MVC4 WebAPI若干要点

    本文仅仅是将一些可以运行无误的WebAPI示例的要点,记录下来,供自己查阅,也供刚刚学习WebAPI的读者参考之. 1.默认的API是不会过滤到action这个级别的,如果要过滤到这个级别,必须在路由 ...

  6. c# WinForm加载焦点

    1.c# WinForm在加载时把焦点设在按钮上 this.AcceptButton = button1; 这样在WinForm窗口中, 按钮的状态会变成窗口的默认按钮, 只要按下Enter键,就会触 ...

  7. CVU介绍

    ORA.CVU  New resource (Cluster Verification Utility) is added in 11.2.0.2 Unlike the previous resour ...

  8. QT5.3.2在ARM上的移植

    ubuntu10.04 准备移植phonon,4.5移植失败.播放声音就出错...没办法.转移到QtMutimedia 安装交叉编译工具这里就不提了... 1.下载QT5.3.2:http://dow ...

  9. OC基础(26)

    集合对象的内存管理 Copy copy与内存管理 @property中的copy关键字 自定义的类实现copy操作 *:first-child { margin-top: 0 !important; ...

  10. github入门

    一.先了解 相比CVS\SVN优势: - 支持离线开发,离线Repository- 强大的分支功能,适合多个独立开发者协作- 速度快 github 本地有仓库,储存着所有repository的历史: ...