Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas!! You all must be familiar with C++ but what you may not know is asio as C++ library. Asio is for those who wonder how to connect language to networking and have input and output connections, which is done in C++ through asio.
Let us systematically understand it in the article with proper code and the installation process. This article specifies how to install and run it on visual studio with the application in asio.
Before moving to asio, We suggest you to brush up your C++ concepts.
Asio
Asio is an open-source, cross-platform C++ library used for network programming and is also free. Asio full form is Asynchronous Input/Output, which makes the data asynchronous. It was developed in 2003 by Christopher M. Kohlhoff and was released under Boost Software License.
It helps programmers and developers with a consistent asynchronous I/O model built with a modern C++ technique.
Applications ranging from mobile apps to the quickest stock markets in the world using Asio.
There are two types of Asio:
Boost
Non-Boost
Setup for Windows
As we have seen above, asio is a C++ library. Following are the steps to download asio.
1️⃣ Download the asio C++ library zip file from here and extract it into your desired directory.
2️⃣ Download visual studio there; we will include the main header file of Asio.
1️⃣ First of all, download and install visual studio and create a new project.
2️⃣ After you click on “ Create a new project.” Select Empty Project and click on Next.
3️⃣ Give your Project name and click on Create
4️⃣ In the Solution Explorer panel, right-click on the created project and select properties to open your created project property pages.
5️⃣ In the property pages dialog box, select VC++Directories > Include Directories.
6️⃣ Include the path of your asio C++ library files(header files).
Now, we have included the path of the asio C++ library files(header files). Alright!! Let's start with the first Asio code and implement some basic networking concepts.
Code Implementation
#include <iostream>
#include <string>
/* Need to define WIN32 because each version of windows has slightly different ways of handling networking*/
#ifdef _WIN32
#define _WIN32_WINNT 0x0A00
#endif
#define ASIO_STANDALONE
#include <C:\Users\ASUS\Downloads\asio-1.24.0\asio-1.24.0\include\asio.hpp>
int main() {
std::string baseURI = "127.0.0.1";
int port = 80;
asio::error_code ecVAR;
asio::io_context context;
/* IP Address we are going to connect to*/
asio::ip::tcp::endpoint endpoint(asio::ip::make_address(baseURI, ecVAR), port);
asio::ip::tcp::socket socket(context);
/* Telling socket to try and connect*/
socket.connect(endpoint, ecVAR);
if (!ecVAR) {
std::cout << "Connected to " << baseURI << ":" << port << std::endl;
}
else {
std::cout << "Failed to connect to address:" << ecVAR.message() << std::endl;
}
system("pause");
return 0;
}
In the above code, we first included the required header files. Then we defined WIN32 because each Windows version has slightly different ways of handling networking. Then we declared and initialized baseURI of string type and port of int type. After that, we created the asio error_code variable ecVAR to handle errors. Also, we created a context (space) to perform various operations.
After this, we tried to get the IP address that we wanted to connect to and used a socket to try and connect to that address.
A successful connection to that IP address would give us the output in the form of the syntax “Connected to baseURI:port”.
In the above case, baseURI was "127.0.0.1" and the port was 80. And the failed connection would give us output in the form of syntax "Failed to connect to address:ecVAR.message()"
Applications of Asio
Following are some applications of Asio:
🙇 Restbed - RESTful C++ embedded framework
This framework adds RESTful asynchronous functionality to C++ 11 applications.
🙇 WebSocket++ (WebSocketPP) - WebSocket framework
WebSocketPP is a C++ library that implements both client and server WebSocket functionality. It is an asio-based asynchronous application.
🙇 Loggly - high-performance cloud-based log aggregation and analytics.
Build high-performance Collectors using the Boost ASIO framework.
🙇 Remobo — create your Instant Private Network (IPN).
It quickly creates an Instant Private Network (IPN) between your computers and your friends.
🙇 Osiris - Serverless Portal System
It is portal creation software.
Frequently Asked Questions
What does ASIO mean in C++?
Asio is a shortened form for asynchronous input/output. This library enables asynchronous data processing. When operations are initiated, the initiating program does not need to wait for the operation to complete.
What is the function of the Boost library in C++?
Boost is a collection of C++ libraries that support tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing.
Asio is written in which language?
Asio is a C++ library written in C++. It is as simple as C++ and supports objected oriented programming. It has various classes to perform certain functions specific to i/o and networking.
How many C++ libraries exist?
C++ includes the old C library (libc. lib) and the new C++ library (libcp. lib).
What are the advantages of the asio library?
Asio is used in projects such as the WebSocketPP library and the DDT3 remote debugger for the Lua programming language. Asio is free and open-source software licensed under the Boost Software License, and it runs on Linux, Windows, macOS, and FreeBSD.
Conclusion
We have discussed getting started with asio in this article. From setup for windows to applications, everything is covered. We hope this article helps you enhance your knowledge of asio. We recommend you read some more articles on asio.