Boost asio - is it possible to reuse a socket? - c++

After I:
created a tcp-socket
put it in listening
received an Incoming "message" from the Client
"processed" the incoming accomplice and closed the socket...
Whether that it is possible to put this socket on listening again? Experiment shows that no - the accept() function - throws an error.
Is it possible, somehow, to "reset" a closed socket to its original state? In order not to release the previously allocated memory for this socket and not to create a new socket, which in the end will also have to be deleted.
PS:
my_socket_p = new boost::asio::ip::tcp::socket(io_context);
my_acceptor.accept(my_socket_p );

The socket you put in listening mode is typically not the socket that you close when you're done handling a request.
Asio makes this distinction more explicit than in the underlying sockets API. I discussed this before here: Design rationale behind that separate acceptor class exists in ASIO (see also e.g. What does it mean to "open" an acceptor?).
Simple example of a typical server that accepts requests and echoes them back reversed:
Live On Coliru
#include <boost/asio.hpp>
#include <iostream>
namespace asio = boost::asio;
using asio::ip::tcp;
int main() {
boost::asio::io_context ioc;
// listen on port 7878
tcp::acceptor acc(ioc, {{}, 7878});
acc.listen();
while (true) {
tcp::socket conn = acc.accept();
auto peer = conn.remote_endpoint();
try {
std::string request;
read_until(conn, asio::dynamic_buffer(request), "\n");
reverse(begin(request), end(request) - 1); // reverse until line-end
write(conn, asio::buffer("reversed: " + request));
conn.close();
} catch(boost::system::system_error const& se) {
std::cerr << "Error with peer " << peer << ": " << se.code().message() << std::endl;
}
}
}
With example clients:
netcat 127.0.0.1 7878 <<< "Hello world!"
netcat 127.0.0.1 7878 <<< "Bye world!"
Prints
reversed: !dlrow olleH
reversed: !dlrow eyB
Re-using?
There are other overloads of accept that take a socket by reference, and yes they can be re-used:
tcp::socket conn(ioc); // reused
while (true) {
acc.accept(conn);
auto peer = conn.remote_endpoint();
With the rest of the code un-changed: Live On Coliru
More Typical
More typically code would be asynchronous and the "connection handling" would be in some other class, e.g. Session. In such cases conn would be moved into Session. This is also explicitly documented as proper use, e.g.:
Following the move, the moved-from object is in the same state as if constructed using the basic_socket(const executor_type&) constructor.
So, you could write the code as such:
tcp::socket conn(ioc); // reused
while (true) {
acc.accept(conn);
std::make_shared<Session>(std::move(conn))->run();
}
You can see it Live On Coliru again, although I'll include the far more idiomatic version using the move-accept handler overload of async_accept instead here:
Live On Coliru
#include <boost/asio.hpp>
#include <iostream>
namespace asio = boost::asio;
using asio::ip::tcp;
using boost::system::error_code;
struct Session : std::enable_shared_from_this<Session> {
Session(tcp::socket s) : conn_(std::move(s)) {}
void run() { do_reverse_echo(); }
private:
void do_reverse_echo() {
async_read_until( //
conn_, asio::dynamic_buffer(buffer_), "\n",
[this, self = shared_from_this()](error_code ec, size_t) {
if (ec) {
std::cerr << "Error with peer " << peer_ << ": " << ec.message() << std::endl;
return;
}
reverse(begin(buffer_), end(buffer_) - 1); // reverse until line-end
buffer_ = "reversed: " + buffer_;
async_write(conn_, asio::buffer(buffer_), asio::detached);
});
}
tcp::socket conn_;
tcp::endpoint peer_{conn_.remote_endpoint()};
std::string buffer_;
};
struct Listener {
Listener(asio::any_io_executor ex, uint16_t port) : acc(ex, {{}, port}) {
acc.listen();
accept_loop();
}
private:
tcp::acceptor acc;
void accept_loop() {
acc.async_accept([this](error_code ec, tcp::socket conn) {
if (ec) {
std::cerr << "Stopping listener: " << ec.message() << std::endl;
return;
}
accept_loop();
std::make_shared<Session>(std::move(conn))->run();
});
}
};
int main() {
boost::asio::io_context ioc;
Listener s(ioc.get_executor(), 7878);
ioc.run();
}
Still with the same output, obviously.

Related

C++ Asio connect to wss

I am trying to connect to a secure websocket using asio.
This example will work for an ip address:
#include <iostream>
#include <asio.hpp>
int main() {
asio::error_code ec;
asio::io_context context;
asio::io_context::work idleWork(context);
asio::ip::tcp::endpoint endpoint(asio::ip::make_address("51.38.81.49", ec), 80);
asio::ip::tcp::socket socket(context);
socket.connect(endpoint, ec);
if (!ec) {
std::cout << "Connected!" << std::endl;
} else {
std::cout << "Failed to connect to address: \n" << ec.message() << std::endl;
}
return 0;
}
but how would I change it so I connect to "wss://api2.example.com"?
EDIT:
Thanks for your answer karastojko - it seems to get me some of the way. I would though like to know if I am actually connected to the server, so I have updated my example with your input, added a working WSS which I know will answer and created read and write.
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
std::vector<char> vBuffer(1 * 1024);
// This should output the received data
void GrabSomeData(asio::ip::tcp::socket& socket) {
socket.async_read_some(asio::buffer(vBuffer.data(), vBuffer.size()),
[&](std::error_code ec, std::size_t lenght) {
if (!ec) {
std::cout << "\n\nRead " << lenght << " bytes\n\n";
for (int i = 0; i < lenght; i++)
std::cout << vBuffer[i];
GrabSomeData(socket);
}
}
);
}
int main() {
asio::error_code ec;
asio::io_context context;
asio::io_context::work idleWork(context);
std::thread thrContext = std::thread([&]() { context.run(); });
// I hope this is what you meant
asio::ip::tcp::resolver res(context);
asio::ip::tcp::socket socket(context);
asio::connect(socket, res.resolve("echo.websocket.org", std::to_string(443)));
// Check the socket is open
if (socket.is_open()) {
// Start to output incoming data
GrabSomeData(socket);
// Send data to the websocket, which should be sent back
std::string sRequest = "Echo";
socket.write_some(asio::buffer(sRequest.data(), sRequest.size()), ec);
// Wait some time, so the data is received
using namespace std::chrono_literals;
std::this_thread::sleep_for(20000ms);
context.stop();
if (thrContext.joinable()) thrContext.join();
}
return 0;
}
For that purpose use the resolver class:
tcp::resolver res(context);
asio::ip::tcp::socket socket(context);
boost::asio::connect(socket, res.resolve("api2.example.com", 80));

understand boost::asio async_read behavior when there is nothing to read

I am trying to understand what would happen with async_read when there is nothing to read.
For example, a client creates a connection to a server, then start async_read(), but that server does not expect to send anything to this client. So what would happen? Should I receive a EOF?
Updata:
I think #user786653 is right. I made a simple example (see following).
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/asio.hpp>
class test{
public:
test(boost::asio::io_service& io_service):_socket(io_service){
}
void handle_connect(){
std::cout<<"entering test::handle_connect"<<std::endl;
char reply[128];
boost::asio::async_read(_socket, boost::asio::buffer(reply, sizeof(reply)),
[](boost::system::error_code ec, std::size_t /*length*/){
std::cout<<"Read result:"<< ec<<" - "<<ec.message()<<std::endl;
});
}
boost::asio::ip::tcp::socket & socket(){
return _socket;
}
private:
boost::asio::ip::tcp::socket _socket;
};
int main() {
try {
boost::asio::io_service io_service;
boost::asio::ip::tcp::socket s(io_service);
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query("127.0.0.1", "8000");
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
test t(io_service);
t.socket().async_connect(endpoint,boost::bind(&test::handle_connect, &t));
io_service.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
}
Quoting from the latest (1.68.0) documentation:
This function is used to asynchronously read a certain number of bytes of data from a stream. The function call always returns immediately. The asynchronous operation will continue until one of the following conditions is true:
The supplied buffers are full. That is, the bytes transferred is equal to the sum of the buffer sizes.
An error occurred.
So nothing will happen until the server closes the connection (resulting in an error).
You can test this out for yourself:
#include <iostream>
#include <boost/asio.hpp>
int main() {
try {
boost::asio::io_context io_context;
boost::asio::ip::tcp::socket s(io_context);
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::connect(s, resolver.resolve("localhost", "8000"));
char reply[128];
async_read(s, boost::asio::buffer(reply, sizeof(reply)), [](boost::system::error_code ec, std::size_t /*length*/) {
std::cout << "Read result: " << ec << " - " << ec.message() << "\n";
});
io_context.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
}
Start a server that doesn't respond on localhost port 8000 (or change the code). E.g. something like nc -l 8000 or python -m SimpleHTTPServer. Then run the program and wait. Nothing happens. Now stop the server, on my (Windows) machine this results in:
Read result: system:10054 - An existing connection was forcibly closed by the remote host

boost::asio hangs _endthreadx

int main(){
boost::asio::io_context io_context;
Server server(io_context, SOCKET_ADDRESS, SOCKET_PORT);
std::thread thread_server([&]() {
server.start();
io_context.run();
});
std::thread thread_client([&]() {
Client &client = Client::create(SOCKET_ADDRESS, SOCKET_PORT);
client.start();
done = true; // <-----atomic
});
std::thread thread_stop([&]() {
while (done == false) {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
server.stop();
});
thread_server.join();
thread_client.join();
thread_stop.join();
}
I am experimenting with boost::asio and encountered problem which I am unable to solve. When I run program(simpler example above) on Linux(compiled with gcc) everything is fine. Same when I run it on Release in VS2017CE. However when I run it on Debug(VS2017CE as well) it crash with and exception:
cannot dereference string iterator because string iterator was invalidated
It crash on _endthreadx either when exiting thread_stop or thread_server(most likely second one). Here are my questions then:
What are the differences between Release and Debug basic configurations which might affect code execution and point me where should I look.(I am aware of some but couldn't find anything connected with this particular problem.)
What mistakes were made by me which affects code execution.
I've made some classes so I will provide more code if neccessary, but code basically works so I am starting with just a piece of it.
The code shown doesn't do anything with strings. Also you don't show what io_context is being used in Client instances.
As given, everything is a giant race-condition, because none of the client work might ever get run, but you always set done = true immediately after posting the (supposedly) asynchronous operation of Client::start.
(The potentially sane interpretation here would be if Client::start() were actually completely synchronous, but that really would make the whole existence of static Client& Client::create(...) pretty strange and useless?).
Using a thread to sleep is an anti-pattern, and doubly so in asynchronous code.
cannot dereference string iterator because string iterator was invalidated
This is a clear sign that MSVC's Iterator Debugging is working. It just tells you you have a programming error.
Your error causes string iterators to be used when they're no longer valid. I can't see it, but 99% of the time this would be caused by asynchronous operations using a buffer that gets destroyed (or modified) before the async operation completes. In a nutshell:
void foo() {
std::string msg = "message";
boost::asio::async_write(_socket, boost::asio::buffer(msg), /*...*/);
}
Suggested Code
Simplifying from your code, and showing some hints:
Live On Coliru
#include <boost/asio.hpp>
#include <iostream>
#include <iomanip>
using boost::asio::ip::tcp;
using boost::system::error_code;
static std::string const SOCKET_ADDRESS = "127.0.0.1";
static unsigned short const SOCKET_PORT = 6767;
bool check(error_code const& ec, char const* message) {
std::cout << message << " (" << ec.message() << ")\n";
return !ec;
}
struct Server {
boost::asio::io_context& io_;
Server(boost::asio::io_context& io, std::string host, unsigned short port) : io_(io), host_(host), port_(port) {}
void start() {
acc_.set_option(tcp::acceptor::reuse_address(true));
acc_.listen(5);
accept_loop();
}
void stop() {
io_.post([this] { // thread safety
acc_.cancel();
acc_.close();
});
}
private:
void accept_loop() {
acc_.async_accept(sock_, [this](error_code ec) {
if (check(ec, "accepted")) {
std::make_shared<Connection>(std::move(sock_))->start();
accept_loop();
}
});
}
struct Connection : std::enable_shared_from_this<Connection> {
tcp::socket sock_;
std::string buffer_;
Connection(tcp::socket&& sock) : sock_(std::move(sock)) {}
~Connection() {
error_code ec;
std::cout << "Disconnected " << sock_.remote_endpoint(ec) << "\n";
}
void start() {
auto self = shared_from_this();
async_read_until(sock_, boost::asio::dynamic_buffer(buffer_), "\n", [self,this](error_code ec, size_t bytes) {
if (check(ec, "received request")) {
std::cout << "Request: " << std::quoted(buffer_.substr(0, bytes), '\'') << "\n";
if (bytes > 0)
std::reverse(buffer_.begin(), buffer_.begin() + bytes - 1); // reverse the request for the response
async_write(sock_, boost::asio::buffer(buffer_, bytes), [self,this](error_code ec, size_t bytes) {
if (check(ec, "response sent")) {
buffer_.erase(0, bytes);
start(); // handle more requests, if any
}
});
}
});
}
};
std::string host_;
unsigned short port_;
tcp::acceptor acc_{io_, {boost::asio::ip::address_v4::from_string(host_), port_}};
tcp::socket sock_{io_};
};
struct Client {
Client(std::string host, std::string port) : host_(host), port_(port) {}
void start() {
boost::asio::io_context io;
tcp::socket s(io);
tcp::resolver r(io);
connect(s, r.resolve(host_, port_));
send_request(s, "hello world\n");
send_request(s, "bye world\n");
}
private:
void send_request(tcp::socket& s, std::string const& request) {
write(s, boost::asio::buffer(request));
boost::asio::streambuf sb;
read_until(s, sb, "\n");
std::cout << "Received server response: '" << &sb << "'\n";
}
std::string host_;
std::string port_;
};
int main(){
boost::asio::io_context io_context;
Server server(io_context, SOCKET_ADDRESS, SOCKET_PORT);
server.start();
std::thread thread_server([&]() { io_context.run(); });
{
Client client {SOCKET_ADDRESS, std::to_string(SOCKET_PORT)};
client.start();
}
{
Client client {SOCKET_ADDRESS, std::to_string(SOCKET_PORT)};
client.start();
}
server.stop();
thread_server.join();
}
Prints
accepted (Success)
received request (Success)
Request: 'hello world
'
response sent (Success)
Received server response: 'dlrow olleh
'
received request (Success)
Request: 'bye world
'
response sent (Success)
Received server response: 'dlrow eyb
'
received request (End of file)
Disconnected 127.0.0.1:49778
accepted (Success)
received request (Success)
Request: 'hello world
'
response sent (Success)
Received server response: 'dlrow olleh
'
received request (Success)
Request: 'bye world
'
response sent (Success)
Received server response: 'dlrow eyb
'
received request (End of file)
Disconnected 127.0.0.1:49780
accepted (Operation canceled)
Note There's a startup race. Depending on your luck, the first Client might try to connect before the Server has started listening. I'm assuming this is not your worst worry, and I'll leave it as an exercise for the reader.

Permission refused when connecting to domain socket created by Boost.Asio

I'm trying to create a server that receives connections via domain sockets. I can start the server and I can see the socket being created on the filesystem. But whenever I try to connect to it via socat I get the following error:
2015/03/02 14:00:10 socat[62720] E connect(3, LEN=19 AF=1 "/var/tmp/rpc.sock", 19): Connection refused
This is my Asio code (only the .cpp files). Despite the post title I'm using the Boost-free version of Asio but I don't think that would be a problem.
namespace myapp {
DomainListener::DomainListener(const string& addr) : socket{this->service}, Listener{addr} {
remove(this->address.c_str());
stream_protocol::endpoint ep(this->address);
stream_protocol::acceptor acceptor(this->service, ep);
acceptor.async_accept(this->socket, ep, bind(&DomainListener::accept_callback, this, _1));
}
DomainListener::~DomainListener() {
this->service.stop();
remove(this->address.c_str());
}
void DomainListener::accept_callback(const error_code& ec) noexcept {
this->socket.async_read_some(asio::buffer(this->data), bind(&DomainListener::read_data, this, _1, _2));
}
void DomainListener::read_data(const error_code& ec, size_t length) noexcept {
//std::cerr << "AAA" << std::endl;
//std::cerr << this->data[0] << std::endl;
//std::cerr << "BBB" << std::endl;
}
}
Listener::Listener(const string& addr) : work{asio::io_service::work(this->service)} {
this->address = addr;
}
void Listener::listen() {
this->service.run();
}
Listener::~Listener() {
}
In the code that uses these classes I call listen() whenever I want to start listening to the socket for connections.
I've managed to get this to work with libuv and changed to Asio because I thought it would make for more readable code but I'm finding the documentation to be very ambiguous.
The issue is most likely the lifetime of the acceptor.
The acceptor is an automatic variable in the DomainListener constructor. When the DomainListener constructor completes, the acceptor is destroyed, causing the acceptor to close and cancel outstanding operations, such as the async_accept operations. Cancelled operations will be provided an error code of asio::error::operation_aborted and scheduled for deferred invocation within the io_service. Hence, there may not be an active listener when attempting to connect to the domain socket. For more details on the affects of IO object destruction, see this answer.
DomainListener::DomainListener(const string&) : /* ... */
{
// ...
stream_protocol::acceptor acceptor(...);
acceptor.async_accept(..., bind(accept_callback, ...));
} // acceptor destroyed, and accept_callback likely cancelled
To resolve this, consider extending the lifetime of the acceptor by making it a data member for DomainListener. Additionally, checking the error_code provided to asynchronous operations can provide more insight into the asynchronous call chains.
Here is a complete minimal example demonstrating using domain sockets with Asio.
#include <cstdio>
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
/// #brief server demonstrates using domain sockets to accept
/// and read from a connection.
class server
{
public:
server(
boost::asio::io_service& io_service,
const std::string& file)
: io_service_(io_service),
acceptor_(io_service_,
boost::asio::local::stream_protocol::endpoint(file)),
client_(io_service_)
{
std::cout << "start accepting connection" << std::endl;
acceptor_.async_accept(client_,
boost::bind(&server::handle_accept, this,
boost::asio::placeholders::error));
}
private:
void handle_accept(const boost::system::error_code& error)
{
std::cout << "handle_accept: " << error.message() << std::endl;
if (error) return;
std::cout << "start reading" << std::endl;
client_.async_read_some(boost::asio::buffer(buffer_),
boost::bind(&server::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(
const boost::system::error_code& error,
std::size_t bytes_transferred)
{
std::cout << "handle_read: " << error.message() << std::endl;
if (error) return;
std::cout << "read: ";
std::cout.write(buffer_.begin(), bytes_transferred);
std::cout.flush();
}
private:
boost::asio::io_service& io_service_;
boost::asio::local::stream_protocol::acceptor acceptor_;
boost::asio::local::stream_protocol::socket client_;
std::array<char, 1024> buffer_;
};
int main(int argc, char* argv[])
{
if (argc != 2)
{
std::cerr << "Usage: <file>\n";
return 1;
}
// Remove file on startup and exit.
std::string file(argv[1]);
struct file_remover
{
file_remover(std::string file): file_(file) { std::remove(file.c_str()); }
~file_remover() { std::remove(file_.c_str()); }
std::string file_;
} remover(file);
// Create and run the server.
boost::asio::io_service io_service;
server s(io_service, file);
io_service.run();
}
Coliru does not have socat installed, so the following commands use OpenBSD netcat to write "asio domain socket example" to the domain socket:
export SOCKFILE=$PWD/example.sock
./a.out $SOCKFILE &
sleep 1
echo "asio domain socket example" | nc -U $SOCKFILE
Which outputs:
start accepting connection
handle_accept: Success
start reading
handle_read: Success
read: asio domain socket example

Boost::Asio peer-to-peer udp chat

I'm writing peer-to-peer (it shouldn't have server - it's a task) program for exchanging text messages. It's a very tiny chat. Simply messages, nothing else. It's my 1st practice with Boost::Asio, therefore I've some questions.
My chat should be peer-to-peer as I said and it should use udp protocol. I think, the best way is to use broadcast. And the first problem: how can I learn about new connections?
Another problem is in sending message: I send it on broadcast address and then it spreads to all computers in local network. Is it right?
This code sends message and receives its back. Like an echo. Is it right?
#include <iostream>
#include <boost/asio.hpp>
#include <boost/array.hpp>
int main()
{
try
{
namespace ip = boost::asio::ip;
boost::asio::io_service io_service;
ip::udp::socket socket(io_service,
ip::udp::endpoint(ip::udp::v4(), 1555));
socket.set_option(boost::asio::socket_base::broadcast(true));
ip::udp::endpoint broadcast_endpoint(ip::address_v4::broadcast(), 1555);
boost::array<char, 4> buffer1;
socket.send_to(boost::asio::buffer(buffer1), broadcast_endpoint);
ip::udp::endpoint sender_endpoint;
boost::array<char, 4> buffer2;
std::size_t bytes_transferred =
socket.receive_from(boost::asio::buffer(buffer2), sender_endpoint);
std::cout << "got " << bytes_transferred << " bytes." << std::endl;
}
catch (std::exception &e)
{
std::cerr << e.what();
}
system("PAUSE");
return 0;
}
Tested on Ubuntu 20.04.3 LTS and Boost.Asio 1.71.
Usually this kind of task is accomplished by using multicast. Broadcast creates too much load on a network.
Basing on the sender and receiver examples while combining both of them, you should open your socket on a multicast address, which represents a "chat room" and at the same time subscribe to that multicast group to receive the messages sent from other chat participants.
#include <iostream>
#include <string>
#include <boost/asio.hpp>
constexpr std::uint16_t multicast_port = 30001;
class Peer {
public:
Peer(boost::asio::io_context& io_context,
const boost::asio::ip::address& chat_room,
const std::string& nickname)
: socket_(io_context)
, multicast_endpoint_(chat_room, multicast_port)
, nickname_(nickname)
{
boost::asio::ip::udp::endpoint listen_endpoint(chat_room, multicast_port);
socket_.open(listen_endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(listen_endpoint);
Note that we using reuse_address option, so you could test this example locally.
If you want to receive messages sent to multicast group, you have to subscribe to that multicast group:
socket_.set_option(boost::asio::ip::multicast::join_group(chat_room));
And as you asked if you want to learn about new connections (though UDP is a connectionless protocol), you can send multicast welcome message:
auto welcome_message = std::string(nickname_ + " connected to the chat\n");
socket_.async_send_to(boost::asio::buffer(welcome_message), multicast_endpoint_,
[this](const boost::system::error_code& error_code, std::size_t bytes_sent){
if (!error_code.failed()){
std::cout << "Entered chat room successfully" << std::endl;
}
});
So, for now we have to establish two loops: first one will expect local user's input, send it to the multicast group and then waits for another user input, while the other one will listen for incoming UDP datagrams on a socket, print datagram's content on every datagram received and then return back to socket listening:
void do_receive(){
socket_.async_receive_from(boost::asio::buffer(receiving_buffer_), remote_endpoint_,
[this](const boost::system::error_code& error_code, std::size_t bytes_received){
if (!error_code.failed() && bytes_received > 0){
auto received_message_string = std::string(receiving_buffer_.begin(), receiving_buffer_.begin() + bytes_received);
// We don't want to receive the messages we produce
if (received_message_string.find(name_) != 0){
std::cout.write(receiving_buffer_.data(), bytes_received);
std::cout << std::flush;
}
do_receive();
}
});
}
void do_send(){
std::string nickname = nickname_;
std::string message;
std::getline(std::cin, message);
std::string buffer = name.append(": " + message);
socket_.async_send_to(boost::asio::buffer(buffer, maximum_message_size_), multicast_endpoint_,
[this, message](const boost::system::error_code& /*error_code*/, std::size_t bytes_sent){
std::cout << "You: " << message << std::endl;
do_send();
});
}
There we also invoke the same IO function in each completion handler to achieve the loop effect still looking like recursion.
For now, all we have to do is to publish each of the function call in the separate threads because of io_context.run() invocation blocking, otherwise one of our loops will block another one, so we call io_context.run() in each thread:
int main(int argc, char* argv[])
{
boost::asio::thread_pool thread_pool(2);
if(argc != 3){
std::cerr << "Usage: ./peer <your_nickname> <multicast_address>" << std::endl;
std::exit(1);
}
boost::asio::io_context io_context;
boost::asio::ip::address chat_room(boost::asio::ip::make_address(argv[2]));
Peer peer(io_context, chat_room, argv[1]);
boost::asio::post(thread_pool, [&]{
peer.do_receive();
io_context.run();
});
boost::asio::post(thread_pool, [&]{
peer.do_send();
io_context.run();
});
thread_pool.join();
return 0;
}
Full source code is available here.