Boost library consists of asio which is a free, cross-platform C++ library for network and low-level I/O programming that provides a consistent asynchronous model using a modern C++ approach. This article will help to develop a client-server synchronous chatting application using boost::asio. We are explicitly mentioning “synchronous” because in the synchronous model one of our client or server has to wait for another.
Server-Side Application: Below are the various steps to create the Server Side application:
- Importing boost/asio.hpp (Version: 220.127.116.11)
- Creating object of io_service (for server) which is mandatory for using boost::asio.
- Creating object of acceptor, passing io_service object and endpoint of connection i.e. IPv4 and port number 9999 (IPv6 protocol is also supported in boost::asio, also note that port 0 – 1233 are reserved).
boost::asio::ip::tcp::acceptor acceptor_object( io_service_object, tcp::endpoint(boost::asio::ip::tcp::v4(), 9999));
- Creating tcp::socket object for our server.
- Invoking accept method of acceptor object to establish connection.
- read_until() method fetches message from the buffer which stores data during communication. Here we are using “\n” as out delimiter, which means we shall keep reading data from the buffer until we encounter “\n” and store it.
// Create buffer for storing boost::asio::streambuf buf; boost::asio::read_until(socket, buf, "\n"); string data = boost::asio::buffer_cast(buf.data());
- write() method writes data to the buffer taking socket object and message as parameter.
boost::asio::write( socket, boost::asio::buffer(message + "\n"));
Client-Side Application: Below are the various steps to create the Client Side application:
- Importing boost/asio.hpp.
- Creating object of io_service for client.
- Creating tcp::socket object for client.
- Invoking connect method of socket object to initiate connection with server using localhost (IP 127.0.0.1) and connecting to same port 9999.
client_socket.connect( tcp::endpoint( address::from_string("127.0.0.1"), 9999 ));
- read_until() and write() will remain same for our client application as well, as the Server side.
- Compile and run the server first by executing:
$ g++ client.cpp -o client -lboost_system ./client
- Open another cmd/terminal and run the client by executing:
$ g++ server.cpp -o server -lboost_system ./server
The above socket programming explains our simple synchronous TCP server and client chatting application. One of the major drawbacks of the synchronous client-server application is that one request has to be served before we request for another one, thus blocking our later requests. In case we want our program to perform multiple operations simultaneously, we can use multi-threaded TCP client-server to handle the situation. However, the multi-threaded application is not recommended because of various complexities involved in creating threads. Another option can come handy, and that is the asynchronous server. This is where boost::asio shines, we shall understand this in the next article.
Attention reader! Don't stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
- Chat application between two processes using signals and shared memory
- C++ bitset and its application
- unordered_multimap and its application
- is_permutation() in C++ and its application for anagram search
- isspace() in C/C++ and its application to count whitespace characters
- iscntrl() in C++ and its application to find control characters
- fesetround() and fegetround() in C++ and their application
- Full screen OpenCV / GtK application in C++ running on Raspberry PI
- Count number of unique Triangles using STL | Set 1 (Using set)
- Print pattern using only one loop | Set 1 (Using setw)
- How will you print numbers from 1 to 100 without using loop?
- Print "Even" or "Odd" without using conditional statement
- Can we access private data members of a class without using a member or a friend function?
- How to check whether a number is in the range[low, high] using one comparison ?
- Print individual digits as words without using if or switch
- Database Connectivity using C/C++
- How to find size of array in C/C++ without using sizeof ?
- How to sum two integers without using arithmetic operators in C/C++?
- std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++