Skip to content

C++ Asio Standalone Network Library

Asio C++ Library

Asio is a cross-platform C++ library for network and low-level I/O programming that provides developers with a consistent asynchronous model using a modern C++ approach.

Asio provides the basic building blocks for C++ networking, concurrency and other kinds of I/O.

Asio is used in all kinds of applications, from phone apps to the world's fastest share markets.

Asio comes in two variants: (non-Boost) Asio and Boost.Asio.

Asio is released under the Boost Software License.

https://think-async.com

Build Asio on FreeBSD

Install boost-libs
pkg install boost-libs

Boost.Asio requires boost-libs. Non-Boost asio application code doesn't require boost calling but requires boost to build your code.

Get asio sources and build asio
git clone https://github.com/chriskohlhoff/asio
cd asio/asio
./autogen.sh
./configure --help
./configure --with-boost=/usr/local/include --prefix=/usr/local/asio
/usr/bin/make    # FreeBSD make or bmake works.
su
/usr/bin/make install

asio has been built and installed to /usr/local/asio now.

Why not use ports or pkg asio?

A: It lacks some critical features.

Asio C++ Example

asio c++ TCP Socket Server and Client Example:

// server.cpp
#include <asio.hpp>
#include <iostream>
#include <string>

int main() {
        asio::io_service io;
        asio::ip::tcp::acceptor acceptor(io, 
                asio::ip::tcp::endpoint(asio::ip::tcp::v4(), 5050));
        while (true) {
                asio::ip::tcp::socket socket(io);
                acceptor.accept(socket);
                std::cout << "Connected From " << socket.remote_endpoint().address() << std::endl;
                std::string data = "Hello, FreeBSD Server!";
                asio::write(socket, asio::buffer(data.c_str(), data.length()));
        }
}
// client.cpp
#include <asio.hpp>
#include <string>
#include <iostream>

int main() {
        asio::io_service io;
        asio::ip::tcp::socket socket(io);
        asio::ip::tcp::resolver resolver(io);
        asio::connect(socket, resolver.resolve("127.0.0.1", "5050"));
        char buffer[80];
        // 22 is the request length, must be smaller than that value.
        std::size_t len = asio::read(socket, asio::buffer(buffer, 22));
        std::cout.write(buffer, len);
        std::cout << std::endl;
}
Build the Example Code
% alias build='clang++ -stdlib=libc++ -std=c++14 -I/usr/local/asio/include -I/usr/local/include -L/usr/local/lib -lboost_system -lthr'
% build server.cpp -o server
% build client.cpp -o client

Run server in one terminal:

% ./server

Run client in another terminal:

% ./client                  
Hello, FreeBSD Server!
% ./client
Hello, FreeBSD Server!
% ./client
Hello, FreeBSD Server!
% 

The server side prompt:

% ./server
Connected From 127.0.0.1
Connected From 127.0.0.1
Connected From 127.0.0.1

asio examples fossil repo: https://www.fossil.tiv.cc/xjail/asio-examples

 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.