Where to Find Asio Examples in the Repository: Complete Directory Guide
All Asio example programs are stored in the src/examples directory, organized by C++ standard version and grouped into subdirectories by feature category such as timers, SSL, and composed operations.
The chriskohlhoff/asio repository provides a comprehensive collection of self-contained, header-only sample programs that demonstrate how to use Asio's asynchronous I/O facilities. Each example file in the src/examples hierarchy is designed to compile independently, making it easy to browse, build, and experiment with specific features.
Repository Structure for Asio Examples
The Asio repository organizes sample code under a single root location to simplify navigation and discovery.
Root Directory Layout
All examples reside in src/examples/, which serves as the parent directory for every sample program. This folder is subdivided primarily by the C++ language standard required for compilation, followed by functional categories that reflect the specific Asio features being demonstrated.
The typical path structure follows this pattern:
src/examples/<cpp_standard>/<feature_category>/<example_file>
C++ Standard Organization
Examples are segregated into version-specific folders to ensure compatibility:
src/examples/cpp11/– Samples targeting C++11, including basic tutorials, SSL clients, and timeout handlingsrc/examples/cpp14/– Examples leveraging C++14 features, particularly composed asynchronous operations usingasync_composesrc/examples/cpp20/– Modern samples utilizing C++20 coroutines, channels, and advanced concurrency primitives
Feature Categories
Within each standard folder, examples are grouped by functionality:
tutorial/– Introductory text examples explaining basic timer and I/O operationsoperations/– Custom composed operation implementationsssl/– SSL/TLS client and server demonstrationstimers/– Blocking and asynchronous timer usage patternschannels/– C++20 channel-based synchronization examplestimeouts/– Examples handling connection and operation timeouts
Navigating Asio Examples by C++ Standard
Each C++ standard folder contains idiomatic code samples that reflect best practices for that language version.
C++11 Examples (src/examples/cpp11/)
The C++11 directory contains foundational examples that work with older compilers while demonstrating core Asio concepts. Key files include:
src/examples/cpp11/tutorial/timer_dox.txt– A tutorial-style example showing how to create a timer, set an expiry time, and block on thewait()methodsrc/examples/cpp11/ssl/client.cpp– Demonstrates establishing an SSL/TLS client connection and retrieving server certificate informationsrc/examples/cpp11/timeouts/blocking_tcp_client.cpp– Implements a blocking TCP client using theasync_resulttrait with timeout handling
These files compile with standard C++11 support and require only the Asio headers.
C++14 Examples (src/examples/cpp14/)
The C++14 folder focuses on composed operations and advanced asynchronous patterns:
src/examples/cpp14/operations/composed_1.cppthroughcomposed_8.cpp– A progressive series showing how to build custom asynchronous operations usingasync_compose
These examples demonstrate how to encapsulate multiple low-level async calls into reusable, high-level operations while maintaining zero-overhead abstraction.
C++20 Examples (src/examples/cpp20/)
The C++20 directory showcases modern asynchronous programming with coroutines:
src/examples/cpp20/channels/mutual_exclusion_2.cpp– Implements mutual exclusion semantics using C++20 channelssrc/examples/cpp20/operations/– C++20 equivalents of the composed operation examples found in the C++14 folder
Compiling Asio Examples
Since Asio is a header-only library, each example file is self-contained and ready to compile. You can build any example using a standard C++ compiler with the appropriate standard flag and include path.
To compile a C++11 timer example:
g++ -std=c++11 -I/path/to/asio/include src/examples/cpp11/tutorial/timer_dox.txt -o timer_example -lpthread
To build a C++14 composed operation example:
g++ -std=c++14 -I/path/to/asio/include src/examples/cpp14/operations/composed_1.cpp -o composed_example -lpthread
For C++20 coroutine examples, ensure your compiler supports C++20 coroutines:
g++ -std=c++20 -fcoroutines -I/path/to/asio/include src/examples/cpp20/channels/mutual_exclusion_2.cpp -o channel_example -lpthread
Summary
- Asio examples are located exclusively in the
src/examplesdirectory of the chriskohlhoff/asio repository. - The directory is organized first by C++ standard (
cpp11,cpp14,cpp20), then by feature category (timers, SSL, operations, channels). - Each file is a self-contained program that compiles against Asio's header-only library.
- C++11 examples cover basic I/O and SSL, C++14 examples focus on composed operations, and C++20 examples demonstrate coroutines and channels.
- Examples can be compiled directly with standard compilers using
-std=c++11(or higher) and the Asio include path.
Frequently Asked Questions
Where are the Asio tutorial examples located?
The tutorial examples are located in src/examples/cpp11/tutorial/. This directory contains introductory samples such as timer_dox.txt, which demonstrates basic timer creation and blocking waits. These text files serve as the foundation for learning Asio's asynchronous I/O model.
How do I compile Asio examples from the source repository?
You can compile any example directly using a C++ compiler with the appropriate standard flag. For example, run g++ -std=c++14 -I/path/to/asio/include src/examples/cpp14/operations/composed_1.cpp -lpthread. Since Asio is header-only, no separate library linking is required beyond system threading libraries.
What is the difference between C++14 and C++20 examples in Asio?
The C++14 examples focus on async_compose and custom completion handlers for building composed operations. The C++20 examples utilize coroutines and channels, providing simpler sequential-looking code for asynchronous flows. Both demonstrate best practices for their respective language standards.
Are there SSL examples in the Asio repository?
Yes, SSL examples are located in src/examples/cpp11/ssl/. These include client.cpp and server implementations that demonstrate how to establish encrypted connections, verify certificates, and perform SSL/TLS handshakes using Asio's SSL wrappers around OpenSSL.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →