-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
executable file
·34 lines (31 loc) · 897 Bytes
/
server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/write.hpp>
#include <ctime>
#include <exception>
#include <iostream>
#include <string>
using boost::asio::ip::tcp;
std::string make_daytime_string() {
auto now = std::time(nullptr);
return std::ctime(&now);
}
int main() {
try {
boost::asio::io_service io;
tcp::acceptor acceptor(io, tcp::endpoint(tcp::v4(), 13));
std::cout << "start service\n";
while (1) {
tcp::socket socket(io);
acceptor.accept(socket);
std::string message = make_daytime_string();
boost::system::error_code ignored_error;
boost::asio::write(socket, boost::asio::buffer(message), ignored_error);
}
}
__catch(std::exception & e) { std::cerr << e.what() << std::endl; }
std::cout << "Bye ....\n";
return 0;
}