-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
53 lines (43 loc) · 1.54 KB
/
main.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <ctime>
#include <iostream>
#include <string>
#include <memory>
#include <map>
#include <mutex>
#include <asio.hpp>
using asio::ip::tcp;
std::atomic_bool g_running {true};
//asio::ip::tcp::endpoint server_addr;
//asio::ip::tcp::acceptor acceptor;
std::string make_daytime_string() {
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
int main() {
// SIGINT handler
signal(SIGINT, [](int sig) {
g_running = false;
std::cout << "ctrl+c is pressed .. exit...." <<std::endl;
});
try {
asio::io_context io_context;
tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 8899));
std::cout << "loccal addr info: \n addr: " << acceptor.local_endpoint().address() << std::endl;
std::cout << "port: " << acceptor.local_endpoint().port() << std::endl;
while (g_running) {
tcp::socket client(io_context);
acceptor.accept(client);
std::string message = make_daytime_string();
std::cout << "msg : " << message <<std::endl;
std::cout << "peer client addr = " << client.remote_endpoint().address() << std::endl;
asio::error_code ignored_error;
asio::write(client, asio::buffer(message), ignored_error);
//socket.read_some(asio::buffer(message)); //sync
}
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
std::cout << "main process exit.... " <<std::endl;
return 0;
}