forked from lpcvoid/cpp-net-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaytime_client.cpp
More file actions
54 lines (43 loc) · 1.43 KB
/
Copy pathdaytime_client.cpp
File metadata and controls
54 lines (43 loc) · 1.43 KB
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
54
#include "../src/netlib.hpp"
#include <csignal>
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
void exit_handler(int s){
std::cout << "Goodbye!" << std::endl;
exit(EXIT_SUCCESS);
}
int main(int argc, char** argv) {
signal(SIGINT, exit_handler);
uint16_t port = 13;
std::string time_host = "time.nist.gov"; //https://tf.nist.gov/tf-cgi/servers.cgi#
if (argc == 3) {
time_host = argv[1];
port = std::atol(argv[2]);
}
std::cout << "Connecting to " << time_host << " on port " << port << std::endl;
netlib::client client;
std::error_condition client_create_res =
client.connect(time_host, port, netlib::AddressFamily::IPv4,
netlib::AddressProtocol::TCP, 1000ms);
if (client_create_res) {
std::cerr << "Error connecting to host, error : " << client_create_res.message() << std::endl;
exit(EXIT_FAILURE);
}
uint32_t error_count = 0;
while (true) {
auto daytime_res = client.recv(1024, 1000ms);
if (daytime_res.second) {
if (error_count++ == 3) {
std::cerr << "Attempted to read three times, all failed, exiting. Error: " <<
daytime_res.second.message() << std::endl;
exit(EXIT_FAILURE);
}
std::this_thread::sleep_for(1000ms);
} else {
std::string daytime_string(daytime_res.first.begin(), daytime_res.first.end());
std::cout << daytime_string << std::endl;
exit(EXIT_SUCCESS);
}
}
}