Boost C++: 网络编程1

  1 #include <iostream>
  2 #include <boost/asio.hpp>
  3 #include <boost/config/compiler/visualc.hpp>
  4 #include <boost/property_tree/ptree.hpp>
  5 #include <boost/property_tree/json_parser.hpp>
  6 #include <boost/foreach.hpp>
  7 #include <boost/filesystem.hpp>
  8 #include <boost/filesystem/fstream.hpp>
  9 #include <boost/iostreams/device/mapped_file.hpp>
 10 #include <cassert>
 11 #include <exception>
 12 #include <sstream>
 13 #include <string>
 14 #include <boost/thread.hpp>
 15
 16
 17
 18 void test_http()
 19 {
 20     using namespace boost::asio;
 21     ip::tcp::iostream stream;
 22
 23     stream.expires_from_now(boost::posix_time::seconds(60));
 24     stream.connect("www.boost.org", "http");
 25     stream << "GET /LICENSE_1_0.txt HTTP/1.0\r\n";
 26     stream << "Host: www.boost.org\r\n";
 27     stream << "Accept: */*\r\n";
 28     stream << "Connection: close\r\n\r\n";
 29
 30     stream.flush();
 31     std::cout << stream.rdbuf();
 32 }
 33
 34 void udp_echo_client()
 35 {
 36
 37     using boost::asio::ip::udp;
 38     boost::asio::io_service my_io_service;
 39     udp::socket s(my_io_service, udp::endpoint(udp::v4(), 0));
 40
 41     udp::resolver resolver(my_io_service);
 42 }
 43
 44 int read_file_content(std::string fileName, std::stringstream& ss)
 45 {
 46     namespace fs = boost::filesystem;
 47     fs::path file(fileName);
 48     if (!fs::exists(file))
 49     {
 50         std::cerr << "file does not exist." << std::endl;
 51         return EXIT_FAILURE;
 52     }
 53
 54     //std::ifstream t(file) >> ss;
 55     fs::ifstream t(file);
 56     ss << t.rdbuf();
 57 //     boost::iostreams::mapped_file mf(file);
 58 //     mf.data() >> ss;
 59
 60     return EXIT_SUCCESS;
 61 }
 62
 63 void print_json_data(boost::property_tree::ptree const& pt)
 64 {
 65     using boost::property_tree::ptree;
 66     ptree::const_iterator end = pt.end();
 67     for (ptree::const_iterator it = pt.begin(); it != end; it++)
 68     {
 69         std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
 70         print_json_data(it->second);
 71     }
 72 }
 73
 74 int parse_json_data()
 75 {
 76     try
 77     {
 78         std::stringstream ss;
 79
 80         read_file_content("data.json",ss);
 81         //ss << "{ \"root\": { \"values\": [1, 2, 3, 4, 5 ] } }";
 82
 83         std::cout << ss.str() << std::endl;
 84
 85         boost::property_tree::ptree pt;
 86         boost::property_tree::read_json(ss, pt);
 87
 88         BOOST_FOREACH(boost::property_tree::ptree::value_type& v, pt.get_child("editorialMarket"))
 89         {
 90             assert(v.first.empty());
 91             std::cout << v.second.data() << std::endl;
 92         }
 93
 94         print_json_data(pt);
 95     }
 96     catch (std::exception& e)
 97     {
 98         std::cerr << "error: " << e.what() << std::endl;
 99     }
100
101     return EXIT_SUCCESS;
102 }
103
104 /*
105 函数 main() 首先定义了一个 I/O 服务 io_service,用于初始化 I/O 对象 timer。
106 就象 boost::asio::deadline_timer 那样,所有 I/O 对象通常都需要一个 I/O 服务作为它们的构造函数的第一个参数。
107 由于 timer 的作用类似于一个闹钟,所以 boost::asio::deadline_timer 的构造函数可以传入第二个参数,
108 用于表示在某个时间点或是在某段时长之后闹钟停止。 以上例子指定了五秒的时长,该闹钟在 timer 被定义之后立即开始计时
109 */
110
111 void handler1(const boost::system::error_code& ec)
112 {
113     std::cout << "5 s" << std::endl;
114     for (int i = 0; i < 10; i++)
115     {
116         printf("%s: %d\n",__FUNCTION__, i);
117         boost::this_thread::sleep(boost::posix_time::seconds(1));
118     }
119 }
120
121 void handler2(const boost::system::error_code& ec)
122 {
123     std::cout << "5 s" << std::endl;
124     for (int i = 0; i < 10;i++)
125     {
126         printf("%s: %d\n", __FUNCTION__, i);
127         boost::this_thread::sleep(boost::posix_time::seconds(1));
128     }
129 }
130
131 boost::asio::io_service io_service1;
132 boost::asio::io_service io_service2;
133 void run1()
134 {
135     printf("in %s\n", __FUNCTION__);
136     io_service1.run();
137 }
138
139 void run2()
140 {
141     printf("in %s\n", __FUNCTION__);
142     io_service2.run();
143 }
144
145 void test_io_srv()
146 {
147     //boost::asio::io_service io_service;
148     //创建5s定时器
149     boost::asio::deadline_timer timer1(io_service1, boost::posix_time::seconds(5));
150     timer1.async_wait(handler1);
151
152     //创建5s定时器
153     boost::asio::deadline_timer timer2(io_service2, boost::posix_time::seconds(5));
154     timer2.async_wait(handler2);
155
156     //io_service.run();
157     boost::thread thread1(run1);
158     boost::thread thread2(run2);
159
160     thread1.join();
161     thread2.join();
162 }
163
164
165 int main()
166 {
167     //parse_json_data();
168     test_io_srv();
169     std::cout << "test_io_srv..." << std::endl;
170 }
时间: 2024-07-30 23:39:40

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

Boost Asio 网络编程 基本用法

Boost Asio 网络编程 基本用法 flyfish 2015-2-9 IP地址 boost::asio::ip::address表示IP地址,同时支持ipv4和ipv6. boost::asio::ip::address addr; addr = addr.from_string("127.0.0.1"); assert(addr.is_v4()); OutputDebugStringA(addr.to_string().c_str()); addr = addr.from_st

boost Asio网络编程简介

.markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(171, 178, 191); background-color: rgb(40, 44, 52); overflow: auto } .markdown-preview:not([data-use-github-style])>:first-child { margin-top: 0px } .markdown-

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

Boost.Asio-其他特性 这章我们讲了解一些Boost.Asio不那么为人所知的特性.标准的stream和streambuf对象有时候会更难用一些,但正如你所见.它们也有它们的益处.最后,你会看到姗姗来迟的Boost.Asio协程的入口,它能够让你的异步代码变的很易读.这是很惊人的一个特性. 标准stream和标准I/O buffer 读这一章节之前你须要对STL stream和STL streambuf对象有所了解. Boost.Asio在处理I/O操作时支持两种类型的buffer: b

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

io_service类 你应该已经发现大部分使用Boost.Asio编写的代码都会使用几个ios_service的实例.ios_service是这个库里面最重要的类:它负责和操作系统打交道,等待所有异步操作的结束,然后为每一个异步操作调用完成处理程序. 如果你选择用同步的方式来创建你的应用,你不需要考虑我将在这一节向你展示的东西. 你可以用几种不同的方式来使用io_service.在下面的例子中,我们有3个异步操作,2个socket连接和一个计时器等待: 有一个io_service和一个处理线程

谈谈Boost网络编程(3)—— 一些坑

很多时候,我们以为采用了一种新技术(尤其是成熟的技术),过程应该是一马平川的.然而实际上,采用新技术的过程却是掉入了各种坑里.究其原因,或者是使用方式有问题,或者是效率的白白浪费.这一章,我想讲讲,我在使用Boost Asio进程网络编程时,所遇到的各种坑. 其一.CPU占用100%问题. 在没有采用异步编程之前,程序占用100%基本是不敢想象的事情,因为一旦程序占用100%的CPU,那必然是代码中出现了死循环的BUG.但是采用了Boost Asio后,我发现新系统很容易就跑满了CPU.Why?

Boost.Asio c++ 网络编程翻译(30)[完结]

PS:至此终于完成了Boost.Asio C++ network programming一书的翻译,这是我人生第一本完整翻译的书,从开始的磕磕绊绊,到最后小有心得,我收获很多.我将把这个系列的博客进行整理和校对,希望有兴趣的人可以帮我一起,来给大家提供更好更专业的阅读体验. 句柄追踪信息到文件 默认情况下,句柄的追踪信息被输出到标准错误流(相当于std::cerr).你想把输出重定向到其他地方的可能性是非常高的.对于控制台应用,输出和错误输出都被默认输出到相同的地方,也就是控制台.但是对于一个w

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

第一次翻译,希望大家多多指正 实战出精华 Boost.Asio C++ 网络编程 用具体的C++网络编程例子来提升你的技能 John Torjan 用具体的C++网络编程例子来提升你的技能 Copyright ? 2013 Packt Publishing 版权所有,除了在鉴定文章或者评论中进行简单引用,如果没有经过出版者事先的书面授权,该书的任何部分都不能被转载.存储在检索系统中.或者以任何形式和方式传阅. 在这本书准备发行之前,我们已经尽我们最大的努力去保证书中信息的准确性.但是,这本书中包

网络编程中的关键问题总结

总结下网络编程中关键的细节问题,包含连接建立.连接断开.消息到达.发送消息等等: 连接建立 包括服务端接受 (accept) 新连接和客户端成功发起 (connect) 连接. accept接受连接的问题在本文最后会聊到,这里谈谈connect的关键点:     使用非阻塞连接建立需要注意:     connect/select返回后,可能没有连接上:需要再次确认是否成功连接: 步骤为: 使用异步connect直接连接一次,因为使用了非阻塞,函数立刻返回: 检查返回值,为0成功连接,否则加入到s

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

有哪些适合学生参与的 C++,网络编程方面的开源项目? Tinyhttpd是一个超轻量型Http Server,使用C语言开发,全部代码只有502行(包括注释),附带一个简单的Client,可以通过阅读这段代码理解一个 Http Server 的本质.下载链接链接:LippiOuYang/Tinyhttpd · GitHub nginx: download高性能web服务器 libevent/libevent · GitHubC语言写的事件驱动框架 ACE:C++面向对象网络编程工具包 Boos