#include #include #include #include using boost::asio::ip::tcp; using namespace std; char* cmdname; void usage() { cerr << "Usage: " << cmdname << " port" << endl; exit(1); } int main(int argc, char* argv[]) { // process command line arguments cmdname = *argv++; --argc; if (argc != 1) usage(); unsigned int port(atoi(argv[0])); boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port)); acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); try { for(;;) { tcp::socket socket(io_service); acceptor.accept(socket); try { char timebuf[32]; time_t clock; time(&clock); ctime_r(&clock, timebuf, sizeof timebuf); std::string msg(timebuf); boost::asio::write(socket, boost::asio::buffer(msg), boost::asio::transfer_all()); } catch (exception& e) { cerr << cmdname << ": " << e.what() << endl; } } } catch (exception& e) { cerr << cmdname << ": " << e.what() << endl; } }