Created
August 15, 2021 23:22
-
-
Save werto87/27bb68ed92864d1211a650346a174ea9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/asio.hpp> | |
#include <iostream> | |
#include <memory> | |
#include <optional> | |
class session : public std::enable_shared_from_this<session> | |
{ | |
public: | |
session (boost::asio::ip::tcp::socket &&socket) : socket (std::move (socket)) {} | |
void | |
start () | |
{ | |
boost::asio::async_read_until (socket, streambuf, '\n', [self = shared_from_this ()] (boost::system::error_code error, std::size_t bytes_transferred) { std::cout << std::istream (&self->streambuf).rdbuf (); }); | |
} | |
private: | |
boost::asio::ip::tcp::socket socket; | |
boost::asio::streambuf streambuf; | |
}; | |
class server | |
{ | |
public: | |
server (boost::asio::io_context &io_context, std::uint16_t port) : io_context (io_context), acceptor (io_context, boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4 (), port)) {} | |
void | |
async_accept () | |
{ | |
auto socket = std::make_shared<boost::asio::ip::tcp::socket> (io_context); | |
acceptor.async_accept (*socket, [&, socket] (boost::system::error_code error) { | |
std::make_shared<session> (std::move (*socket))->start (); | |
async_accept (); | |
}); | |
} | |
private: | |
boost::asio::io_context &io_context; | |
boost::asio::ip::tcp::acceptor acceptor; | |
}; | |
int | |
main () | |
{ | |
boost::asio::io_context io_context; | |
server srv (io_context, 15001); | |
srv.async_accept (); | |
io_context.run (); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment