Boost C++: 网络编程1
#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的更多相关文章
- boost Asio网络编程简介
:first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...
- Boost.Asio 网络编程([译]Boost.Asio基本原理)
转自:https://m.w3cschool.cn/nlzbw/nlzbw-3vs825ya.html Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情.我们也将 ...
- [Boost基础]并发编程——asio网络库——定时器deadline_timer
asio库基于操作系统提供的异步机制,采用前摄器设计模式(Proactor)实现了可移植的异步(或者同步)IO操作,而且并不要求使用多线程和锁定,有些的避免了多线程编程带来的诸多有害副作用(如条件竞争 ...
- Boost.Asio c++ 网络编程翻译(26)
Boost.Asio-其他特性 这章我们讲了解一些Boost.Asio不那么为人所知的特性.标准的stream和streambuf对象有时候会更难用一些,但正如你所见.它们也有它们的益处.最后,你会看 ...
- 有哪些适合学生参与的 C++,网络编程方面的开源项目?
有哪些适合学生参与的 C++,网络编程方面的开源项目? Tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通 ...
- Muduo 网络编程示例之零:前言
陈硕 (giantchen_AT_gmail)Blog.csdn.net/Solstice Muduo 全系列文章列表: http://blog.csdn.net/Solstice/category/ ...
- 基于ASIO的协程与网络编程
协程 协程,即协作式程序,其思想是,一系列互相依赖的协程间依次使用CPU,每次只有一个协程工作,而其他协程处于休眠状态.协程可以在运行期间的某个点上暂停执行,并在恢复运行时从暂停的点上继续执行. 协程 ...
- 使用juggle简化网络编程
常规的网络编程,在消息处理上大概会采用如下方式 struct msg{ int msg_id; int msg_len; //...msg_info }; 定义如上的消息结构 接收方接收后,按如上的消 ...
- Socket网络编程--Libev库学习(1)
这一节是安装篇. Socket网络编程不知不觉已经学了快两个月了.现在是时候找个网络库学学了.搜索了很多关于如何学网络编程的博客和问答.大致都是推荐学一个网络库,至于C++网络库有那么几个,各有各的好 ...
随机推荐
- linux下的g++编译器安装
再debian下直接apt-get install gcc g++就可以了.按照类似的逻辑,再Fedora下yum install gcc g++ 报告无法找到g++包. 查了一下,原来这个包的名字叫 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
- HDU 5428 The Factor
话说这题意真的是好难懂啊,尽管搜到了中文题意,然而还是没懂,最后看到了一个题解才懂的.http://www.cnblogs.com/Apro/p/4784808.html#3470972 题意:给出n ...
- Linux命令(16)压缩,解压文件
tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...
- [物理学与PDEs]书中出现的向量公式汇总
P 11 1. $\rot (\phi{\bf A})=\n \phi\times{\bf A}+\phi\ \rot{\bf A}$. 2. $-\lap {\bf A}=\rot\rot {\bf ...
- wcf Svcutil用法
[转] WCF中可以使用SVCUtil.exe生成客户端代理类和配置文件 1.找到如下地址“C:\Windows\System32\cmd.exe” 命令行工具,右键以管理员身份运行(视系统是否为w ...
- centos7编译安装nginx1.8
安装pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz tar -zxvf pcre-8. ...
- http://www.imooc.com/video/4767 zepto教学视频笔记
一.介绍js移动端框架:zepto.js与jquery mobile 对比:zepto特点 1.与jquery相似度95%,会jquery基本会zepto: 2.API少,轻量级框架 3.移动端无缝接 ...
- maven问题
pom.xml ... </dependencies> <repositories> <repository> <id>sf-nexus</id& ...
- Mysql设置字符编码及varchar宽度问题
ubuntu16.04通过仓库安装的mysql5.7的配置文件在 /etc/mysql/mysql.conf.d/mysqld.cnf 修改字符只需要 在[mysqld] character-set- ...