LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tcp_echo_client.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <libwire/tcp/socket.hpp>
3 #include <libwire/dns.hpp>
4 
17 int main(int argc, char** argv) {
18  using namespace libwire;
19  using namespace std::literals::string_view_literals;
20 
21  if (argc != 2 && argc != 3) {
22  std::cerr << "Usage: echo-client <ip> [port]\n";
23  return 1;
24  }
25 
26  uint16_t port = 7;
27  if (argc == 3) {
28  port = uint16_t(std::stoi(argv[2]));
29  }
30 
31  tcp::socket socket;
32  socket.connect({dns::resolve(ip::v4, argv[1]).at(0), port});
33 
34  std::string buffer;
35 
36  while (std::cout << "> ", std::getline(std::cin, buffer)) {
37  socket.write(buffer);
38  socket.write("\n"sv);
39  socket.read_until('\n', buffer);
40 
41  std::cout << "< " << buffer << '\n';
42  }
43 }
This file defines free functions for interaction with system resolver.
This file defines tcp::socket type, base class for outgoing TCP connections.
std::vector< address > resolve(ip protocol, const std::string_view &domain, std::error_code &ec) noexcept
Resolve domain name to one or more IP addresses of 'protocol' version.
Descriptor wrapper for TCP socket.
Definition: socket.hpp:59
int main(int argc, char **argv)
void connect(endpoint target, std::error_code &ec) noexcept
Initialize underlying socket and connect to remote endpoint.