首页 > 技术文章 > Boost C++: 网络编程1

kernel0815 2014-12-25 14:54 原文

  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 }

 

推荐阅读