1[/
2 / Copyright (c) 2003-2021 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
8[section:examples Examples]
9
10
11* [link boost_asio.examples.cpp03_examples C++03 Examples]: Illustrates the use of
12Boost.Asio using only C++03 language and library features. Where necessary, the
13examples make use of selected Boost C++ libraries.
14
15* [link boost_asio.examples.cpp11_examples C++11 Examples]: Contains a limited set of
16the C++03 Boost.Asio examples, updated to use only C++11 library and language
17facilities. These examples do not make direct use of Boost C++ libraries.
18
19* [link boost_asio.examples.cpp14_examples C++14 Examples]: Contains a limited set of
20the C++03 Boost.Asio examples, updated to use only C++14 library and language
21facilities. These examples do not make direct use of Boost C++ libraries.
22
23* [link boost_asio.examples.cpp17_examples C++17 Examples]: Selected examples
24illustrating C++17 usage in conjunction with Technical Specifications.
25
26
27[section:cpp03_examples C++03 Examples]
28
29
30[heading Allocation]
31
32This example shows how to customise the allocation of memory associated with
33asynchronous operations.
34
35* [@boost_asio/example/cpp03/allocation/server.cpp]
36
37
38[heading Buffers]
39
40This example demonstrates how to create reference counted buffers that can be
41used with socket read and write operations.
42
43* [@boost_asio/example/cpp03/buffers/reference_counted.cpp]
44
45
46[heading Chat]
47
48This example implements a chat server and client. The programs use a custom
49protocol with a fixed length message header and variable length message body.
50
51* [@boost_asio/example/cpp03/chat/chat_message.hpp]
52* [@boost_asio/example/cpp03/chat/chat_client.cpp]
53* [@boost_asio/example/cpp03/chat/chat_server.cpp]
54
55The following POSIX-specific chat client demonstrates how to use the
56[link boost_asio.reference.posix__stream_descriptor posix::stream_descriptor] class to
57perform console input and output.
58
59* [@boost_asio/example/cpp03/chat/posix_chat_client.cpp]
60
61
62[heading Echo]
63
64A collection of simple clients and servers, showing the use of both synchronous
65and asynchronous operations.
66
67* [@boost_asio/example/cpp03/echo/async_tcp_echo_server.cpp]
68* [@boost_asio/example/cpp03/echo/async_udp_echo_server.cpp]
69* [@boost_asio/example/cpp03/echo/blocking_tcp_echo_client.cpp]
70* [@boost_asio/example/cpp03/echo/blocking_tcp_echo_server.cpp]
71* [@boost_asio/example/cpp03/echo/blocking_udp_echo_client.cpp]
72* [@boost_asio/example/cpp03/echo/blocking_udp_echo_server.cpp]
73
74
75[heading Fork]
76
77These POSIX-specific examples show how to use Boost.Asio in conjunction with the
78`fork()` system call. The first example illustrates the steps required to start
79a daemon process:
80
81* [@boost_asio/example/cpp03/fork/daemon.cpp]
82
83The second example demonstrates how it is possible to fork a process from
84within a completion handler.
85
86* [@boost_asio/example/cpp03/fork/process_per_connection.cpp]
87
88
89[heading HTTP Client]
90
91Example programs implementing simple HTTP 1.0 clients. These examples show how
92to use the [link boost_asio.reference.read_until read_until] and [link
93boost_asio.reference.async_read_until async_read_until] functions.
94
95* [@boost_asio/example/cpp03/http/client/sync_client.cpp]
96* [@boost_asio/example/cpp03/http/client/async_client.cpp]
97
98
99[heading HTTP Server]
100
101This example illustrates the use of asio in a simple single-threaded server
102implementation of HTTP 1.0. It demonstrates how to perform a clean shutdown by
103cancelling all outstanding asynchronous operations.
104
105* [@boost_asio/example/cpp03/http/server/connection.cpp]
106* [@boost_asio/example/cpp03/http/server/connection.hpp]
107* [@boost_asio/example/cpp03/http/server/connection_manager.cpp]
108* [@boost_asio/example/cpp03/http/server/connection_manager.hpp]
109* [@boost_asio/example/cpp03/http/server/header.hpp]
110* [@boost_asio/example/cpp03/http/server/main.cpp]
111* [@boost_asio/example/cpp03/http/server/mime_types.cpp]
112* [@boost_asio/example/cpp03/http/server/mime_types.hpp]
113* [@boost_asio/example/cpp03/http/server/reply.cpp]
114* [@boost_asio/example/cpp03/http/server/reply.hpp]
115* [@boost_asio/example/cpp03/http/server/request.hpp]
116* [@boost_asio/example/cpp03/http/server/request_handler.cpp]
117* [@boost_asio/example/cpp03/http/server/request_handler.hpp]
118* [@boost_asio/example/cpp03/http/server/request_parser.cpp]
119* [@boost_asio/example/cpp03/http/server/request_parser.hpp]
120* [@boost_asio/example/cpp03/http/server/server.cpp]
121* [@boost_asio/example/cpp03/http/server/server.hpp]
122
123
124[heading HTTP Server 2]
125
126An HTTP server using an io_context-per-CPU design.
127
128* [@boost_asio/example/cpp03/http/server2/connection.cpp]
129* [@boost_asio/example/cpp03/http/server2/connection.hpp]
130* [@boost_asio/example/cpp03/http/server2/header.hpp]
131* [@boost_asio/example/cpp03/http/server2/io_context_pool.cpp]
132* [@boost_asio/example/cpp03/http/server2/io_context_pool.hpp]
133* [@boost_asio/example/cpp03/http/server2/main.cpp]
134* [@boost_asio/example/cpp03/http/server2/mime_types.cpp]
135* [@boost_asio/example/cpp03/http/server2/mime_types.hpp]
136* [@boost_asio/example/cpp03/http/server2/reply.cpp]
137* [@boost_asio/example/cpp03/http/server2/reply.hpp]
138* [@boost_asio/example/cpp03/http/server2/request.hpp]
139* [@boost_asio/example/cpp03/http/server2/request_handler.cpp]
140* [@boost_asio/example/cpp03/http/server2/request_handler.hpp]
141* [@boost_asio/example/cpp03/http/server2/request_parser.cpp]
142* [@boost_asio/example/cpp03/http/server2/request_parser.hpp]
143* [@boost_asio/example/cpp03/http/server2/server.cpp]
144* [@boost_asio/example/cpp03/http/server2/server.hpp]
145
146
147[heading HTTP Server 3]
148
149An HTTP server using a single io_context and a thread pool calling `io_context::run()`.
150
151* [@boost_asio/example/cpp03/http/server3/connection.cpp]
152* [@boost_asio/example/cpp03/http/server3/connection.hpp]
153* [@boost_asio/example/cpp03/http/server3/header.hpp]
154* [@boost_asio/example/cpp03/http/server3/main.cpp]
155* [@boost_asio/example/cpp03/http/server3/mime_types.cpp]
156* [@boost_asio/example/cpp03/http/server3/mime_types.hpp]
157* [@boost_asio/example/cpp03/http/server3/reply.cpp]
158* [@boost_asio/example/cpp03/http/server3/reply.hpp]
159* [@boost_asio/example/cpp03/http/server3/request.hpp]
160* [@boost_asio/example/cpp03/http/server3/request_handler.cpp]
161* [@boost_asio/example/cpp03/http/server3/request_handler.hpp]
162* [@boost_asio/example/cpp03/http/server3/request_parser.cpp]
163* [@boost_asio/example/cpp03/http/server3/request_parser.hpp]
164* [@boost_asio/example/cpp03/http/server3/server.cpp]
165* [@boost_asio/example/cpp03/http/server3/server.hpp]
166
167
168[heading HTTP Server 4]
169
170A single-threaded HTTP server implemented using stackless coroutines.
171
172* [@boost_asio/example/cpp03/http/server4/file_handler.cpp]
173* [@boost_asio/example/cpp03/http/server4/file_handler.hpp]
174* [@boost_asio/example/cpp03/http/server4/header.hpp]
175* [@boost_asio/example/cpp03/http/server4/main.cpp]
176* [@boost_asio/example/cpp03/http/server4/mime_types.cpp]
177* [@boost_asio/example/cpp03/http/server4/mime_types.hpp]
178* [@boost_asio/example/cpp03/http/server4/reply.cpp]
179* [@boost_asio/example/cpp03/http/server4/reply.hpp]
180* [@boost_asio/example/cpp03/http/server4/request.hpp]
181* [@boost_asio/example/cpp03/http/server4/request_parser.cpp]
182* [@boost_asio/example/cpp03/http/server4/request_parser.hpp]
183* [@boost_asio/example/cpp03/http/server4/server.cpp]
184* [@boost_asio/example/cpp03/http/server4/server.hpp]
185
186
187[heading ICMP]
188
189This example shows how to use raw sockets with ICMP to ping a remote host.
190
191* [@boost_asio/example/cpp03/icmp/ping.cpp]
192* [@boost_asio/example/cpp03/icmp/ipv4_header.hpp]
193* [@boost_asio/example/cpp03/icmp/icmp_header.hpp]
194
195
196[heading Invocation]
197
198This example shows how to customise handler invocation. Completion handlers are
199added to a priority queue rather than executed immediately.
200
201* [@boost_asio/example/cpp03/invocation/prioritised_handlers.cpp]
202
203
204[heading Iostreams]
205
206Two examples showing how to use [link boost_asio.reference.ip__tcp.iostream
207ip::tcp::iostream].
208
209* [@boost_asio/example/cpp03/iostreams/daytime_client.cpp]
210* [@boost_asio/example/cpp03/iostreams/daytime_server.cpp]
211* [@boost_asio/example/cpp03/iostreams/http_client.cpp]
212
213
214[heading Multicast]
215
216An example showing the use of multicast to transmit packets to a group of
217subscribers.
218
219* [@boost_asio/example/cpp03/multicast/receiver.cpp]
220* [@boost_asio/example/cpp03/multicast/sender.cpp]
221
222
223[heading Serialization]
224
225This example shows how Boost.Serialization can be used with asio to encode and
226decode structures for transmission over a socket.
227
228* [@boost_asio/example/cpp03/serialization/client.cpp]
229* [@boost_asio/example/cpp03/serialization/connection.hpp]
230* [@boost_asio/example/cpp03/serialization/server.cpp]
231* [@boost_asio/example/cpp03/serialization/stock.hpp]
232
233
234[heading Services]
235
236This example demonstrates how to integrate custom functionality (in this case,
237for logging) into asio's [link boost_asio.reference.io_context io_context], and
238how to use a custom service with [link
239boost_asio.reference.basic_stream_socket basic_stream_socket<>].
240
241* [@boost_asio/example/cpp03/services/basic_logger.hpp]
242* [@boost_asio/example/cpp03/services/daytime_client.cpp]
243* [@boost_asio/example/cpp03/services/logger.hpp]
244* [@boost_asio/example/cpp03/services/logger_service.cpp]
245* [@boost_asio/example/cpp03/services/logger_service.hpp]
246* [@boost_asio/example/cpp03/services/stream_socket_service.hpp]
247
248
249[heading SOCKS 4]
250
251Example client program implementing the SOCKS 4 protocol for communication via
252a proxy.
253
254* [@boost_asio/example/cpp03/socks4/sync_client.cpp]
255* [@boost_asio/example/cpp03/socks4/socks4.hpp]
256
257
258[heading SSL]
259
260Example client and server programs showing the use of the [link
261boost_asio.reference.ssl__stream ssl::stream<>] template with asynchronous operations.
262
263* [@boost_asio/example/cpp03/ssl/client.cpp]
264* [@boost_asio/example/cpp03/ssl/server.cpp]
265
266
267[heading Timeouts]
268
269A collection of examples showing how to cancel long running asynchronous
270operations after a period of time.
271
272* [@boost_asio/example/cpp03/timeouts/async_tcp_client.cpp]
273* [@boost_asio/example/cpp03/timeouts/blocking_tcp_client.cpp]
274* [@boost_asio/example/cpp03/timeouts/blocking_token_tcp_client.cpp]
275* [@boost_asio/example/cpp03/timeouts/blocking_udp_client.cpp]
276* [@boost_asio/example/cpp03/timeouts/server.cpp]
277
278
279[heading Timers]
280
281Example showing how to customise basic_waitable_timer using a different clock type.
282
283* [@boost_asio/example/cpp03/timers/time_t_timer.cpp]
284
285
286[heading Porthopper]
287
288Example illustrating mixed synchronous and asynchronous operations, and how to
289use Boost.Lambda with Boost.Asio.
290
291* [@boost_asio/example/cpp03/porthopper/protocol.hpp]
292* [@boost_asio/example/cpp03/porthopper/client.cpp]
293* [@boost_asio/example/cpp03/porthopper/server.cpp]
294
295
296[heading Nonblocking]
297
298Example demonstrating reactor-style operations for integrating a third-party
299library that wants to perform the I/O operations itself.
300
301* [@boost_asio/example/cpp03/nonblocking/third_party_lib.cpp]
302
303
304[heading Spawn]
305
306Example of using the boost::asio::spawn() function, a wrapper around the
307[@http://www.boost.org/doc/libs/release/libs/coroutine/index.html Boost.Coroutine]
308library, to implement a chain of asynchronous operations using stackful
309coroutines.
310
311* [@boost_asio/example/cpp03/spawn/echo_server.cpp]
312
313
314[heading UNIX Domain Sockets]
315
316Examples showing how to use UNIX domain (local) sockets.
317
318* [@boost_asio/example/cpp03/local/connect_pair.cpp]
319* [@boost_asio/example/cpp03/local/iostream_client.cpp]
320* [@boost_asio/example/cpp03/local/stream_server.cpp]
321* [@boost_asio/example/cpp03/local/stream_client.cpp]
322
323
324[heading Windows]
325
326An example showing how to use the Windows-specific function `TransmitFile`
327with Boost.Asio.
328
329* [@boost_asio/example/cpp03/windows/transmit_file.cpp]
330
331
332[endsect]
333
334
335[section:cpp11_examples C++11 Examples]
336
337
338[heading Allocation]
339
340This example shows how to customise the allocation of memory associated with
341asynchronous operations.
342
343* [@boost_asio/example/cpp11/allocation/server.cpp]
344
345
346[heading Buffers]
347
348This example demonstrates how to create reference counted buffers that can be
349used with socket read and write operations.
350
351* [@boost_asio/example/cpp11/buffers/reference_counted.cpp]
352
353
354[heading Chat]
355
356This example implements a chat server and client. The programs use a custom
357protocol with a fixed length message header and variable length message body.
358
359* [@boost_asio/example/cpp11/chat/chat_message.hpp]
360* [@boost_asio/example/cpp11/chat/chat_client.cpp]
361* [@boost_asio/example/cpp11/chat/chat_server.cpp]
362
363
364[heading Echo]
365
366A collection of simple clients and servers, showing the use of both synchronous
367and asynchronous operations.
368
369* [@boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp]
370* [@boost_asio/example/cpp11/echo/async_udp_echo_server.cpp]
371* [@boost_asio/example/cpp11/echo/blocking_tcp_echo_client.cpp]
372* [@boost_asio/example/cpp11/echo/blocking_tcp_echo_server.cpp]
373* [@boost_asio/example/cpp11/echo/blocking_udp_echo_client.cpp]
374* [@boost_asio/example/cpp11/echo/blocking_udp_echo_server.cpp]
375
376
377[heading Fork]
378
379These POSIX-specific examples show how to use Boost.Asio in conjunction with the
380`fork()` system call. The first example illustrates the steps required to start
381a daemon process:
382
383* [@boost_asio/example/cpp11/fork/daemon.cpp]
384
385The second example demonstrates how it is possible to fork a process from
386within a completion handler.
387
388* [@boost_asio/example/cpp11/fork/process_per_connection.cpp]
389
390
391[heading Futures]
392
393This example demonstrates how to use std::future in conjunction with
394Boost.Asio's asynchronous operations.
395
396* [@boost_asio/example/cpp11/futures/daytime_client.cpp]
397
398
399[heading Handler Tracking]
400
401This example header file shows how to implement custom handler tracking.
402
403* [@boost_asio/example/cpp11/handler_tracking/custom_tracking.hpp]
404
405This example program shows how to include source location information in
406the handler tracking output.
407
408* [@boost_asio/example/cpp11/handler_tracking/async_tcp_echo_server.cpp]
409
410
411[heading HTTP Server]
412
413This example illustrates the use of asio in a simple single-threaded server
414implementation of HTTP 1.0. It demonstrates how to perform a clean shutdown by
415cancelling all outstanding asynchronous operations.
416
417* [@boost_asio/example/cpp11/http/server/connection.cpp]
418* [@boost_asio/example/cpp11/http/server/connection.hpp]
419* [@boost_asio/example/cpp11/http/server/connection_manager.cpp]
420* [@boost_asio/example/cpp11/http/server/connection_manager.hpp]
421* [@boost_asio/example/cpp11/http/server/header.hpp]
422* [@boost_asio/example/cpp11/http/server/main.cpp]
423* [@boost_asio/example/cpp11/http/server/mime_types.cpp]
424* [@boost_asio/example/cpp11/http/server/mime_types.hpp]
425* [@boost_asio/example/cpp11/http/server/reply.cpp]
426* [@boost_asio/example/cpp11/http/server/reply.hpp]
427* [@boost_asio/example/cpp11/http/server/request.hpp]
428* [@boost_asio/example/cpp11/http/server/request_handler.cpp]
429* [@boost_asio/example/cpp11/http/server/request_handler.hpp]
430* [@boost_asio/example/cpp11/http/server/request_parser.cpp]
431* [@boost_asio/example/cpp11/http/server/request_parser.hpp]
432* [@boost_asio/example/cpp11/http/server/server.cpp]
433* [@boost_asio/example/cpp11/http/server/server.hpp]
434
435
436[heading Multicast]
437
438An example showing the use of multicast to transmit packets to a group of
439subscribers.
440
441* [@boost_asio/example/cpp11/multicast/receiver.cpp]
442* [@boost_asio/example/cpp11/multicast/sender.cpp]
443
444
445[heading Nonblocking]
446
447Example demonstrating reactor-style operations for integrating a third-party
448library that wants to perform the I/O operations itself.
449
450* [@boost_asio/example/cpp11/nonblocking/third_party_lib.cpp]
451
452
453[heading Operations]
454
455Examples showing how to implement composed asynchronous operations as reusable library functions.
456
457* [@boost_asio/example/cpp11/operations/composed_1.cpp]
458* [@boost_asio/example/cpp11/operations/composed_2.cpp]
459* [@boost_asio/example/cpp11/operations/composed_3.cpp]
460* [@boost_asio/example/cpp11/operations/composed_4.cpp]
461* [@boost_asio/example/cpp11/operations/composed_5.cpp]
462* [@boost_asio/example/cpp11/operations/composed_6.cpp]
463* [@boost_asio/example/cpp11/operations/composed_7.cpp]
464* [@boost_asio/example/cpp11/operations/composed_8.cpp]
465
466
467[heading SOCKS 4]
468
469Example client program implementing the SOCKS 4 protocol for communication via
470a proxy.
471
472* [@boost_asio/example/cpp11/socks4/sync_client.cpp]
473* [@boost_asio/example/cpp11/socks4/socks4.hpp]
474
475
476[heading Spawn]
477
478Example of using the boost::asio::spawn() function, a wrapper around the
479[@http://www.boost.org/doc/libs/release/libs/coroutine/index.html Boost.Coroutine]
480library, to implement a chain of asynchronous operations using stackful
481coroutines.
482
483* [@boost_asio/example/cpp11/spawn/echo_server.cpp]
484
485
486[heading SSL]
487
488Example client and server programs showing the use of the [link
489boost_asio.reference.ssl__stream ssl::stream<>] template with asynchronous operations.
490
491* [@boost_asio/example/cpp11/ssl/client.cpp]
492* [@boost_asio/example/cpp11/ssl/server.cpp]
493
494
495[heading Timeouts]
496
497A collection of examples showing how to cancel long running asynchronous
498operations after a period of time.
499
500* [@boost_asio/example/cpp11/timeouts/async_tcp_client.cpp]
501* [@boost_asio/example/cpp11/timeouts/blocking_tcp_client.cpp]
502* [@boost_asio/example/cpp11/timeouts/blocking_token_tcp_client.cpp]
503* [@boost_asio/example/cpp11/timeouts/blocking_udp_client.cpp]
504* [@boost_asio/example/cpp11/timeouts/server.cpp]
505
506
507[heading Timers]
508
509Example showing how to customise basic_waitable_timer using a different clock type.
510
511* [@boost_asio/example/cpp11/timers/time_t_timer.cpp]
512
513
514[heading UNIX Domain Sockets]
515
516Examples showing how to use UNIX domain (local) sockets.
517
518* [@boost_asio/example/cpp11/local/connect_pair.cpp]
519* [@boost_asio/example/cpp11/local/iostream_client.cpp]
520* [@boost_asio/example/cpp11/local/stream_server.cpp]
521* [@boost_asio/example/cpp11/local/stream_client.cpp]
522
523
524[endsect]
525
526
527[section:cpp14_examples C++14 Examples]
528
529
530[heading Operations]
531
532Examples showing how to implement composed asynchronous operations as reusable library functions.
533
534* [@boost_asio/example/cpp14/operations/composed_1.cpp]
535* [@boost_asio/example/cpp14/operations/composed_2.cpp]
536* [@boost_asio/example/cpp14/operations/composed_3.cpp]
537* [@boost_asio/example/cpp14/operations/composed_4.cpp]
538* [@boost_asio/example/cpp14/operations/composed_5.cpp]
539* [@boost_asio/example/cpp14/operations/composed_6.cpp]
540* [@boost_asio/example/cpp14/operations/composed_7.cpp]
541* [@boost_asio/example/cpp14/operations/composed_8.cpp]
542
543
544[endsect]
545
546
547[section:cpp17_examples C++17 Examples]
548
549
550[heading Coroutines TS Support]
551
552Examples showing how to implement a chain of asynchronous operations using the
553Coroutines TS.
554
555* [@boost_asio/example/cpp17/coroutines_ts/echo_server.cpp]
556* [@boost_asio/example/cpp17/coroutines_ts/refactored_echo_server.cpp]
557* [@boost_asio/example/cpp17/coroutines_ts/chat_server.cpp]
558* [@boost_asio/example/cpp17/coroutines_ts/range_based_for.cpp]
559
560
561[endsect]
562
563
564[endsect]
565