Synchronous Chatting Application using C++ boost::asio
Last Updated :
22 Apr, 2021
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: 1.65.1.0)
#include <boost/asio.hpp>
- Creating object of io_service (for server) which is mandatory for using boost::asio.
boost::asio::io_service io_service_object;
- 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.
boost::asio::ip::tcp::socket socket_object(io_service_object)
- Invoking accept method of acceptor object to establish connection.
acceptor_server.accept(server_socket);
- 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.
#include <boost/asio.hpp>
- Creating object of io_service for client.
boost::asio::io_service io_service_object;
- Creating tcp::socket object for client.
boost::asio::ip::tcp::socket
socket_object(io_service_object)
- 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.
Below is the implementation of the above approach: Program:
server.cpp
// Server-side Synchronous Chatting Application
// using C++ boost::asio
#include <boost/asio.hpp>
#include <iostream>
using namespace std;
using namespace boost::asio;
using namespace boost::asio::ip;
// Driver program for receiving data from buffer
string getData(tcp::socket& socket)
{
streambuf buf;
read_until(socket, buf, "\n");
string data = buffer_cast<const char*>(buf.data());
return data;
}
// Driver program to send data
void sendData(tcp::socket& socket, const string& message)
{
write(socket,
buffer(message + "\n"));
}
int main(int argc, char* argv[])
{
io_service io_service;
// Listening for any new incomming connection
// at port 9999 with IPv4 protocol
tcp::acceptor acceptor_server(
io_service,
tcp::endpoint(tcp::v4(), 9999));
// Creating socket object
tcp::socket server_socket(io_service);
// waiting for connection
acceptor_server.accept(server_socket);
// Reading username
string u_name = getData(server_socket);
// Removing "\n" from the username
u_name.pop_back();
// Replying with default message to initiate chat
string response, reply;
reply = "Hello " + u_name + "!";
cout << "Server: " << reply << endl;
sendData(server_socket, reply);
while (true) {
// Fetching response
response = getData(server_socket);
// Popping last character "\n"
response.pop_back();
// Validating if the connection has to be closed
if (response == "exit") {
cout << u_name << " left!" << endl;
break;
}
cout << u_name << ": " << response << endl;
// Reading new message from input stream
cout << "Server"
<< ": ";
getline(cin, reply);
sendData(server_socket, reply);
if (reply == "exit")
break;
}
return 0;
}
client.cpp
// Client-side Synchronous Chatting Application
// using C++ boost::asio
#include <boost/asio.hpp>
#include <iostream>
using namespace std;
using namespace boost::asio;
using namespace boost::asio::ip;
string getData(tcp::socket& socket)
{
streambuf buf;
read_until(socket, buf, "\n");
string data = buffer_cast<const char*>(buf.data());
return data;
}
void sendData(tcp::socket& socket, const string& message)
{
write(socket,
buffer(message + "\n"));
}
int main(int argc, char* argv[])
{
io_service io_service;
// socket creation
ip::tcp::socket client_socket(io_service);
client_socket
.connect(
tcp::endpoint(
address::from_string("127.0.0.1"),
9999));
// Getting username from user
cout << "Enter your name: ";
string u_name, reply, response;
getline(cin, u_name);
// Sending username to another end
// to initiate the conversation
sendData(client_socket, u_name);
// Infinite loop for chit-chat
while (true) {
// Fetching response
response = getData(client_socket);
// Popping last character "\n"
response.pop_back();
// Validating if the connection has to be closed
if (response == "exit") {
cout << "Connection terminated" << endl;
break;
}
cout << "Server: " << response << endl;
// Reading new message from input stream
cout << u_name << ": ";
getline(cin, reply);
sendData(client_socket, reply);
if (reply == "exit")
break;
}
return 0;
}
[tabbyending]
- 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.
Similar Reads
Chat application between two processes using signals and shared memory Prerequisite: C signal handling, IPC through shared memory A signal is used in the UNIX system to notify a process that a particular event has occurred. A signal may be received either synchronously or asynchronously depending on the source and the reason for the event being signalled. A signal must
3 min read
C++20 std::basic_syncbuf In C++, the std::basic_syncbuf class template is part of the Standard Library, used for creating custom stream buffers. This class template is primarily intended for use in creating custom stream classes that work seamlessly with multithreaded programs. This means that if multiple threads are trying
3 min read
Socket Programming in C/C++: Handling multiple clients on server without multi threading This tutorial assumes you have a basic knowledge of socket programming, i.e you are familiar with basic server and client model. In the basic model, server handles only one client at a time, which is a big assumption if you want to develop any scalable server model. The simple way to handle multiple
9 min read
Socket Programming in C++ In C++, socket programming refers to the method of communication between two sockets on the network using a C++ program. We use the socket API to create a connection between the two programs running on the network, one of which receives the data by listening to the particular address port, and the o
5 min read
C++ 20 - <stop_token> Header C++20 has introduced the <stop_token> Header, presenting an alternative method for canceling asynchronous operations. A new class std::stop_token is provided by the <stop_token> header that allows for seamless communication of a termination request between two threads. In this article, w
4 min read