examples.qbk 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. [/
  2. / Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:examples Examples]
  8. * [link boost_asio.examples.cpp03_examples C++03 Examples]: Illustrates the use of
  9. Boost.Asio using only C++03 language and library features. Where necessary, the
  10. examples make use of selected Boost C++ libraries.
  11. * [link boost_asio.examples.cpp11_examples C++11 Examples]: Contains a limited set of
  12. the C++03 Boost.Asio examples, updated to use only C++11 library and language
  13. facilities. These examples do not make direct use of Boost C++ libraries.
  14. * [link boost_asio.examples.cpp17_examples C++17 Examples]: Selected examples
  15. illustrating C++17 usage in conjunction with Technical Specifications.
  16. [section:cpp03_examples C++03 Examples]
  17. [heading Allocation]
  18. This example shows how to customise the allocation of memory associated with
  19. asynchronous operations.
  20. * [@boost_asio/example/cpp03/allocation/server.cpp]
  21. [heading Buffers]
  22. This example demonstrates how to create reference counted buffers that can be
  23. used with socket read and write operations.
  24. * [@boost_asio/example/cpp03/buffers/reference_counted.cpp]
  25. [heading Chat]
  26. This example implements a chat server and client. The programs use a custom
  27. protocol with a fixed length message header and variable length message body.
  28. * [@boost_asio/example/cpp03/chat/chat_message.hpp]
  29. * [@boost_asio/example/cpp03/chat/chat_client.cpp]
  30. * [@boost_asio/example/cpp03/chat/chat_server.cpp]
  31. The following POSIX-specific chat client demonstrates how to use the
  32. [link boost_asio.reference.posix__stream_descriptor posix::stream_descriptor] class to
  33. perform console input and output.
  34. * [@boost_asio/example/cpp03/chat/posix_chat_client.cpp]
  35. [heading Echo]
  36. A collection of simple clients and servers, showing the use of both synchronous
  37. and asynchronous operations.
  38. * [@boost_asio/example/cpp03/echo/async_tcp_echo_server.cpp]
  39. * [@boost_asio/example/cpp03/echo/async_udp_echo_server.cpp]
  40. * [@boost_asio/example/cpp03/echo/blocking_tcp_echo_client.cpp]
  41. * [@boost_asio/example/cpp03/echo/blocking_tcp_echo_server.cpp]
  42. * [@boost_asio/example/cpp03/echo/blocking_udp_echo_client.cpp]
  43. * [@boost_asio/example/cpp03/echo/blocking_udp_echo_server.cpp]
  44. [heading Fork]
  45. These POSIX-specific examples show how to use Boost.Asio in conjunction with the
  46. `fork()` system call. The first example illustrates the steps required to start
  47. a daemon process:
  48. * [@boost_asio/example/cpp03/fork/daemon.cpp]
  49. The second example demonstrates how it is possible to fork a process from
  50. within a completion handler.
  51. * [@boost_asio/example/cpp03/fork/process_per_connection.cpp]
  52. [heading HTTP Client]
  53. Example programs implementing simple HTTP 1.0 clients. These examples show how
  54. to use the [link boost_asio.reference.read_until read_until] and [link
  55. boost_asio.reference.async_read_until async_read_until] functions.
  56. * [@boost_asio/example/cpp03/http/client/sync_client.cpp]
  57. * [@boost_asio/example/cpp03/http/client/async_client.cpp]
  58. [heading HTTP Server]
  59. This example illustrates the use of asio in a simple single-threaded server
  60. implementation of HTTP 1.0. It demonstrates how to perform a clean shutdown by
  61. cancelling all outstanding asynchronous operations.
  62. * [@boost_asio/example/cpp03/http/server/connection.cpp]
  63. * [@boost_asio/example/cpp03/http/server/connection.hpp]
  64. * [@boost_asio/example/cpp03/http/server/connection_manager.cpp]
  65. * [@boost_asio/example/cpp03/http/server/connection_manager.hpp]
  66. * [@boost_asio/example/cpp03/http/server/header.hpp]
  67. * [@boost_asio/example/cpp03/http/server/main.cpp]
  68. * [@boost_asio/example/cpp03/http/server/mime_types.cpp]
  69. * [@boost_asio/example/cpp03/http/server/mime_types.hpp]
  70. * [@boost_asio/example/cpp03/http/server/reply.cpp]
  71. * [@boost_asio/example/cpp03/http/server/reply.hpp]
  72. * [@boost_asio/example/cpp03/http/server/request.hpp]
  73. * [@boost_asio/example/cpp03/http/server/request_handler.cpp]
  74. * [@boost_asio/example/cpp03/http/server/request_handler.hpp]
  75. * [@boost_asio/example/cpp03/http/server/request_parser.cpp]
  76. * [@boost_asio/example/cpp03/http/server/request_parser.hpp]
  77. * [@boost_asio/example/cpp03/http/server/server.cpp]
  78. * [@boost_asio/example/cpp03/http/server/server.hpp]
  79. [heading HTTP Server 2]
  80. An HTTP server using an io_context-per-CPU design.
  81. * [@boost_asio/example/cpp03/http/server2/connection.cpp]
  82. * [@boost_asio/example/cpp03/http/server2/connection.hpp]
  83. * [@boost_asio/example/cpp03/http/server2/header.hpp]
  84. * [@boost_asio/example/cpp03/http/server2/io_context_pool.cpp]
  85. * [@boost_asio/example/cpp03/http/server2/io_context_pool.hpp]
  86. * [@boost_asio/example/cpp03/http/server2/main.cpp]
  87. * [@boost_asio/example/cpp03/http/server2/mime_types.cpp]
  88. * [@boost_asio/example/cpp03/http/server2/mime_types.hpp]
  89. * [@boost_asio/example/cpp03/http/server2/reply.cpp]
  90. * [@boost_asio/example/cpp03/http/server2/reply.hpp]
  91. * [@boost_asio/example/cpp03/http/server2/request.hpp]
  92. * [@boost_asio/example/cpp03/http/server2/request_handler.cpp]
  93. * [@boost_asio/example/cpp03/http/server2/request_handler.hpp]
  94. * [@boost_asio/example/cpp03/http/server2/request_parser.cpp]
  95. * [@boost_asio/example/cpp03/http/server2/request_parser.hpp]
  96. * [@boost_asio/example/cpp03/http/server2/server.cpp]
  97. * [@boost_asio/example/cpp03/http/server2/server.hpp]
  98. [heading HTTP Server 3]
  99. An HTTP server using a single io_context and a thread pool calling `io_context::run()`.
  100. * [@boost_asio/example/cpp03/http/server3/connection.cpp]
  101. * [@boost_asio/example/cpp03/http/server3/connection.hpp]
  102. * [@boost_asio/example/cpp03/http/server3/header.hpp]
  103. * [@boost_asio/example/cpp03/http/server3/main.cpp]
  104. * [@boost_asio/example/cpp03/http/server3/mime_types.cpp]
  105. * [@boost_asio/example/cpp03/http/server3/mime_types.hpp]
  106. * [@boost_asio/example/cpp03/http/server3/reply.cpp]
  107. * [@boost_asio/example/cpp03/http/server3/reply.hpp]
  108. * [@boost_asio/example/cpp03/http/server3/request.hpp]
  109. * [@boost_asio/example/cpp03/http/server3/request_handler.cpp]
  110. * [@boost_asio/example/cpp03/http/server3/request_handler.hpp]
  111. * [@boost_asio/example/cpp03/http/server3/request_parser.cpp]
  112. * [@boost_asio/example/cpp03/http/server3/request_parser.hpp]
  113. * [@boost_asio/example/cpp03/http/server3/server.cpp]
  114. * [@boost_asio/example/cpp03/http/server3/server.hpp]
  115. [heading HTTP Server 4]
  116. A single-threaded HTTP server implemented using stackless coroutines.
  117. * [@boost_asio/example/cpp03/http/server4/file_handler.cpp]
  118. * [@boost_asio/example/cpp03/http/server4/file_handler.hpp]
  119. * [@boost_asio/example/cpp03/http/server4/header.hpp]
  120. * [@boost_asio/example/cpp03/http/server4/main.cpp]
  121. * [@boost_asio/example/cpp03/http/server4/mime_types.cpp]
  122. * [@boost_asio/example/cpp03/http/server4/mime_types.hpp]
  123. * [@boost_asio/example/cpp03/http/server4/reply.cpp]
  124. * [@boost_asio/example/cpp03/http/server4/reply.hpp]
  125. * [@boost_asio/example/cpp03/http/server4/request.hpp]
  126. * [@boost_asio/example/cpp03/http/server4/request_parser.cpp]
  127. * [@boost_asio/example/cpp03/http/server4/request_parser.hpp]
  128. * [@boost_asio/example/cpp03/http/server4/server.cpp]
  129. * [@boost_asio/example/cpp03/http/server4/server.hpp]
  130. [heading ICMP]
  131. This example shows how to use raw sockets with ICMP to ping a remote host.
  132. * [@boost_asio/example/cpp03/icmp/ping.cpp]
  133. * [@boost_asio/example/cpp03/icmp/ipv4_header.hpp]
  134. * [@boost_asio/example/cpp03/icmp/icmp_header.hpp]
  135. [heading Invocation]
  136. This example shows how to customise handler invocation. Completion handlers are
  137. added to a priority queue rather than executed immediately.
  138. * [@boost_asio/example/cpp03/invocation/prioritised_handlers.cpp]
  139. [heading Iostreams]
  140. Two examples showing how to use [link boost_asio.reference.ip__tcp.iostream
  141. ip::tcp::iostream].
  142. * [@boost_asio/example/cpp03/iostreams/daytime_client.cpp]
  143. * [@boost_asio/example/cpp03/iostreams/daytime_server.cpp]
  144. * [@boost_asio/example/cpp03/iostreams/http_client.cpp]
  145. [heading Multicast]
  146. An example showing the use of multicast to transmit packets to a group of
  147. subscribers.
  148. * [@boost_asio/example/cpp03/multicast/receiver.cpp]
  149. * [@boost_asio/example/cpp03/multicast/sender.cpp]
  150. [heading Serialization]
  151. This example shows how Boost.Serialization can be used with asio to encode and
  152. decode structures for transmission over a socket.
  153. * [@boost_asio/example/cpp03/serialization/client.cpp]
  154. * [@boost_asio/example/cpp03/serialization/connection.hpp]
  155. * [@boost_asio/example/cpp03/serialization/server.cpp]
  156. * [@boost_asio/example/cpp03/serialization/stock.hpp]
  157. [heading Services]
  158. This example demonstrates how to integrate custom functionality (in this case,
  159. for logging) into asio's [link boost_asio.reference.io_context io_context], and
  160. how to use a custom service with [link
  161. boost_asio.reference.basic_stream_socket basic_stream_socket<>].
  162. * [@boost_asio/example/cpp03/services/basic_logger.hpp]
  163. * [@boost_asio/example/cpp03/services/daytime_client.cpp]
  164. * [@boost_asio/example/cpp03/services/logger.hpp]
  165. * [@boost_asio/example/cpp03/services/logger_service.cpp]
  166. * [@boost_asio/example/cpp03/services/logger_service.hpp]
  167. * [@boost_asio/example/cpp03/services/stream_socket_service.hpp]
  168. [heading SOCKS 4]
  169. Example client program implementing the SOCKS 4 protocol for communication via
  170. a proxy.
  171. * [@boost_asio/example/cpp03/socks4/sync_client.cpp]
  172. * [@boost_asio/example/cpp03/socks4/socks4.hpp]
  173. [heading SSL]
  174. Example client and server programs showing the use of the [link
  175. boost_asio.reference.ssl__stream ssl::stream<>] template with asynchronous operations.
  176. * [@boost_asio/example/cpp03/ssl/client.cpp]
  177. * [@boost_asio/example/cpp03/ssl/server.cpp]
  178. [heading Timeouts]
  179. A collection of examples showing how to cancel long running asynchronous
  180. operations after a period of time.
  181. * [@boost_asio/example/cpp03/timeouts/async_tcp_client.cpp]
  182. * [@boost_asio/example/cpp03/timeouts/blocking_tcp_client.cpp]
  183. * [@boost_asio/example/cpp03/timeouts/blocking_token_tcp_client.cpp]
  184. * [@boost_asio/example/cpp03/timeouts/blocking_udp_client.cpp]
  185. * [@boost_asio/example/cpp03/timeouts/server.cpp]
  186. [heading Timers]
  187. Example showing how to customise basic_waitable_timer using a different clock type.
  188. * [@boost_asio/example/cpp03/timers/time_t_timer.cpp]
  189. [heading Porthopper]
  190. Example illustrating mixed synchronous and asynchronous operations, and how to
  191. use Boost.Lambda with Boost.Asio.
  192. * [@boost_asio/example/cpp03/porthopper/protocol.hpp]
  193. * [@boost_asio/example/cpp03/porthopper/client.cpp]
  194. * [@boost_asio/example/cpp03/porthopper/server.cpp]
  195. [heading Nonblocking]
  196. Example demonstrating reactor-style operations for integrating a third-party
  197. library that wants to perform the I/O operations itself.
  198. * [@boost_asio/example/cpp03/nonblocking/third_party_lib.cpp]
  199. [heading Spawn]
  200. Example of using the boost::asio::spawn() function, a wrapper around the
  201. [@http://www.boost.org/doc/libs/release/libs/coroutine/index.html Boost.Coroutine]
  202. library, to implement a chain of asynchronous operations using stackful
  203. coroutines.
  204. * [@boost_asio/example/cpp03/spawn/echo_server.cpp]
  205. [heading UNIX Domain Sockets]
  206. Examples showing how to use UNIX domain (local) sockets.
  207. * [@boost_asio/example/cpp03/local/connect_pair.cpp]
  208. * [@boost_asio/example/cpp03/local/iostream_client.cpp]
  209. * [@boost_asio/example/cpp03/local/stream_server.cpp]
  210. * [@boost_asio/example/cpp03/local/stream_client.cpp]
  211. [heading Windows]
  212. An example showing how to use the Windows-specific function `TransmitFile`
  213. with Boost.Asio.
  214. * [@boost_asio/example/cpp03/windows/transmit_file.cpp]
  215. [endsect]
  216. [section:cpp11_examples C++11 Examples]
  217. [heading Allocation]
  218. This example shows how to customise the allocation of memory associated with
  219. asynchronous operations.
  220. * [@boost_asio/example/cpp11/allocation/server.cpp]
  221. [heading Buffers]
  222. This example demonstrates how to create reference counted buffers that can be
  223. used with socket read and write operations.
  224. * [@boost_asio/example/cpp11/buffers/reference_counted.cpp]
  225. [heading Chat]
  226. This example implements a chat server and client. The programs use a custom
  227. protocol with a fixed length message header and variable length message body.
  228. * [@boost_asio/example/cpp11/chat/chat_message.hpp]
  229. * [@boost_asio/example/cpp11/chat/chat_client.cpp]
  230. * [@boost_asio/example/cpp11/chat/chat_server.cpp]
  231. [heading Echo]
  232. A collection of simple clients and servers, showing the use of both synchronous
  233. and asynchronous operations.
  234. * [@boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp]
  235. * [@boost_asio/example/cpp11/echo/async_udp_echo_server.cpp]
  236. * [@boost_asio/example/cpp11/echo/blocking_tcp_echo_client.cpp]
  237. * [@boost_asio/example/cpp11/echo/blocking_tcp_echo_server.cpp]
  238. * [@boost_asio/example/cpp11/echo/blocking_udp_echo_client.cpp]
  239. * [@boost_asio/example/cpp11/echo/blocking_udp_echo_server.cpp]
  240. [heading Fork]
  241. These POSIX-specific examples show how to use Boost.Asio in conjunction with the
  242. `fork()` system call. The first example illustrates the steps required to start
  243. a daemon process:
  244. * [@boost_asio/example/cpp11/fork/daemon.cpp]
  245. The second example demonstrates how it is possible to fork a process from
  246. within a completion handler.
  247. * [@boost_asio/example/cpp11/fork/process_per_connection.cpp]
  248. [heading Futures]
  249. This example demonstrates how to use std::future in conjunction with
  250. Boost.Asio's asynchronous operations.
  251. * [@boost_asio/example/cpp11/futures/daytime_client.cpp]
  252. [heading Handler Tracking]
  253. This example shows how to implement custom handler tracking.
  254. * [@boost_asio/example/cpp11/handler_tracking/custom_tracking.hpp]
  255. [heading HTTP Server]
  256. This example illustrates the use of asio in a simple single-threaded server
  257. implementation of HTTP 1.0. It demonstrates how to perform a clean shutdown by
  258. cancelling all outstanding asynchronous operations.
  259. * [@boost_asio/example/cpp11/http/server/connection.cpp]
  260. * [@boost_asio/example/cpp11/http/server/connection.hpp]
  261. * [@boost_asio/example/cpp11/http/server/connection_manager.cpp]
  262. * [@boost_asio/example/cpp11/http/server/connection_manager.hpp]
  263. * [@boost_asio/example/cpp11/http/server/header.hpp]
  264. * [@boost_asio/example/cpp11/http/server/main.cpp]
  265. * [@boost_asio/example/cpp11/http/server/mime_types.cpp]
  266. * [@boost_asio/example/cpp11/http/server/mime_types.hpp]
  267. * [@boost_asio/example/cpp11/http/server/reply.cpp]
  268. * [@boost_asio/example/cpp11/http/server/reply.hpp]
  269. * [@boost_asio/example/cpp11/http/server/request.hpp]
  270. * [@boost_asio/example/cpp11/http/server/request_handler.cpp]
  271. * [@boost_asio/example/cpp11/http/server/request_handler.hpp]
  272. * [@boost_asio/example/cpp11/http/server/request_parser.cpp]
  273. * [@boost_asio/example/cpp11/http/server/request_parser.hpp]
  274. * [@boost_asio/example/cpp11/http/server/server.cpp]
  275. * [@boost_asio/example/cpp11/http/server/server.hpp]
  276. [heading Multicast]
  277. An example showing the use of multicast to transmit packets to a group of
  278. subscribers.
  279. * [@boost_asio/example/cpp11/multicast/receiver.cpp]
  280. * [@boost_asio/example/cpp11/multicast/sender.cpp]
  281. [heading Nonblocking]
  282. Example demonstrating reactor-style operations for integrating a third-party
  283. library that wants to perform the I/O operations itself.
  284. * [@boost_asio/example/cpp11/nonblocking/third_party_lib.cpp]
  285. [heading Operations]
  286. Examples showing how to implement composed asynchronous operations as reusable library functions.
  287. * [@boost_asio/example/cpp11/operations/composed_1.cpp]
  288. * [@boost_asio/example/cpp11/operations/composed_2.cpp]
  289. * [@boost_asio/example/cpp11/operations/composed_3.cpp]
  290. * [@boost_asio/example/cpp11/operations/composed_4.cpp]
  291. * [@boost_asio/example/cpp11/operations/composed_5.cpp]
  292. * [@boost_asio/example/cpp11/operations/composed_6.cpp]
  293. * [@boost_asio/example/cpp11/operations/composed_7.cpp]
  294. * [@boost_asio/example/cpp11/operations/composed_8.cpp]
  295. [heading SOCKS 4]
  296. Example client program implementing the SOCKS 4 protocol for communication via
  297. a proxy.
  298. * [@boost_asio/example/cpp11/socks4/sync_client.cpp]
  299. * [@boost_asio/example/cpp11/socks4/socks4.hpp]
  300. [heading Spawn]
  301. Example of using the boost::asio::spawn() function, a wrapper around the
  302. [@http://www.boost.org/doc/libs/release/libs/coroutine/index.html Boost.Coroutine]
  303. library, to implement a chain of asynchronous operations using stackful
  304. coroutines.
  305. * [@boost_asio/example/cpp11/spawn/echo_server.cpp]
  306. [heading SSL]
  307. Example client and server programs showing the use of the [link
  308. boost_asio.reference.ssl__stream ssl::stream<>] template with asynchronous operations.
  309. * [@boost_asio/example/cpp11/ssl/client.cpp]
  310. * [@boost_asio/example/cpp11/ssl/server.cpp]
  311. [heading Timeouts]
  312. A collection of examples showing how to cancel long running asynchronous
  313. operations after a period of time.
  314. * [@boost_asio/example/cpp11/timeouts/async_tcp_client.cpp]
  315. * [@boost_asio/example/cpp11/timeouts/blocking_tcp_client.cpp]
  316. * [@boost_asio/example/cpp11/timeouts/blocking_token_tcp_client.cpp]
  317. * [@boost_asio/example/cpp11/timeouts/blocking_udp_client.cpp]
  318. * [@boost_asio/example/cpp11/timeouts/server.cpp]
  319. [heading Timers]
  320. Example showing how to customise basic_waitable_timer using a different clock type.
  321. * [@boost_asio/example/cpp11/timers/time_t_timer.cpp]
  322. [heading UNIX Domain Sockets]
  323. Examples showing how to use UNIX domain (local) sockets.
  324. * [@boost_asio/example/cpp11/local/connect_pair.cpp]
  325. * [@boost_asio/example/cpp11/local/iostream_client.cpp]
  326. * [@boost_asio/example/cpp11/local/stream_server.cpp]
  327. * [@boost_asio/example/cpp11/local/stream_client.cpp]
  328. [endsect]
  329. [section:cpp14_examples C++14 Examples]
  330. [heading Operations]
  331. Examples showing how to implement composed asynchronous operations as reusable library functions.
  332. * [@boost_asio/example/cpp14/operations/composed_1.cpp]
  333. * [@boost_asio/example/cpp14/operations/composed_2.cpp]
  334. * [@boost_asio/example/cpp14/operations/composed_3.cpp]
  335. * [@boost_asio/example/cpp14/operations/composed_4.cpp]
  336. * [@boost_asio/example/cpp14/operations/composed_5.cpp]
  337. * [@boost_asio/example/cpp14/operations/composed_6.cpp]
  338. * [@boost_asio/example/cpp14/operations/composed_7.cpp]
  339. * [@boost_asio/example/cpp14/operations/composed_8.cpp]
  340. [endsect]
  341. [section:cpp17_examples C++17 Examples]
  342. [heading Coroutines TS Support]
  343. Examples showing how to implement a chain of asynchronous operations using the
  344. Coroutines TS.
  345. * [@boost_asio/example/cpp17/coroutines_ts/echo_server.cpp]
  346. * [@boost_asio/example/cpp17/coroutines_ts/refactored_echo_server.cpp]
  347. * [@boost_asio/example/cpp17/coroutines_ts/double_buffered_echo_server.cpp]
  348. * [@boost_asio/example/cpp17/coroutines_ts/chat_server.cpp]
  349. * [@boost_asio/example/cpp17/coroutines_ts/range_based_for.cpp]
  350. [endsect]
  351. [endsect]