Skip to content

Instantly share code, notes, and snippets.

@edouarda
Created February 13, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edouarda/e6efb392ee340b7f90bb4500e2ab67d4 to your computer and use it in GitHub Desktop.
Save edouarda/e6efb392ee340b7f90bb4500e2ab67d4 to your computer and use it in GitHub Desktop.
#include <boost/asio.hpp>
#include <iostream>
int main(int, char **)
{
boost::asio::io_context context;
auto ip = boost::asio::ip::address::from_string("127.0.0.1");
auto ep = boost::asio::ip::tcp::endpoint{ip, 8080};
boost::asio::ip::tcp::acceptor acc{context, ep};
while (true)
{
boost::system::error_code ec;
auto s = acc.accept(ec);
if (ec)
{
std::cerr << ec.value() << ec.message() << std::endl;
break;
}
auto t = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now());
std::array<char, 1024> time_buffer;
auto err = ::ctime_s(time_buffer.data(), time_buffer.size(), &t);
auto len = ::strnlen_s(time_buffer.data(), time_buffer.size());
if (!err)
{
boost::asio::write(
s, boost::asio::const_buffer{time_buffer.data(), len + 1}, ec);
}
s.shutdown(boost::asio::socket_base::shutdown_both, ec);
s.close(ec);
}
acc.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment