Commit 7472b5a49d681088d4ebc01db2b567f6d5e3787f

Authored by Grzegorz Jabłoński
1 parent 91c7c002

More examples

05-daytime1.cpp 0 → 100644
  1 +//
  2 +// client.cpp
  3 +// ~~~~~~~~~~
  4 +//
  5 +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6 +//
  7 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9 +//
  10 +
  11 +#include <iostream>
  12 +#include <boost/array.hpp>
  13 +#include <asio.hpp>
  14 +
  15 +using asio::ip::tcp;
  16 +
  17 +int main(int argc, char* argv[])
  18 +{
  19 + try
  20 + {
  21 + if (argc != 2)
  22 + {
  23 + std::cerr << "Usage: client <host>" << std::endl;
  24 + return 1;
  25 + }
  26 +
  27 + asio::io_context io_context;
  28 +
  29 + tcp::resolver resolver(io_context);
  30 + tcp::resolver::results_type endpoints =
  31 + resolver.resolve(argv[1], "daytime");
  32 +
  33 + tcp::socket socket(io_context);
  34 + asio::connect(socket, endpoints);
  35 +
  36 + for (;;)
  37 + {
  38 + boost::array<char, 128> buf;
  39 + asio::error_code error;
  40 +
  41 + size_t len = socket.read_some(asio::buffer(buf), error);
  42 +
  43 + if (error == asio::error::eof)
  44 + break; // Connection closed cleanly by peer.
  45 + else if (error)
  46 + throw asio::system_error(error); // Some other error.
  47 +
  48 + std::cout.write(buf.data(), len);
  49 + }
  50 + }
  51 + catch (std::exception& e)
  52 + {
  53 + std::cerr << e.what() << std::endl;
  54 + }
  55 +
  56 + return 0;
  57 +}
  58 +
... ...
06-daytime2.cpp 0 → 100644
  1 +//
  2 +// server.cpp
  3 +// ~~~~~~~~~~
  4 +//
  5 +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6 +//
  7 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9 +//
  10 +
  11 +#include <ctime>
  12 +#include <iostream>
  13 +#include <string>
  14 +#include <asio.hpp>
  15 +
  16 +using asio::ip::tcp;
  17 +
  18 +std::string make_daytime_string()
  19 +{
  20 + using namespace std; // For time_t, time and ctime;
  21 + time_t now = time(0);
  22 + return ctime(&now);
  23 +}
  24 +
  25 +int main()
  26 +{
  27 + try
  28 + {
  29 + asio::io_context io_context;
  30 +
  31 + tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 13));
  32 +
  33 + for (;;)
  34 + {
  35 + tcp::socket socket(io_context);
  36 + acceptor.accept(socket);
  37 +
  38 + std::string message = make_daytime_string();
  39 +
  40 + asio::error_code ignored_error;
  41 + asio::write(socket, asio::buffer(message), ignored_error);
  42 + }
  43 + }
  44 + catch (std::exception& e)
  45 + {
  46 + std::cerr << e.what() << std::endl;
  47 + }
  48 +
  49 + return 0;
  50 +}
  51 +
... ...
07-daytime3.cpp 0 → 100644
  1 +//
  2 +// server.cpp
  3 +// ~~~~~~~~~~
  4 +//
  5 +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6 +//
  7 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9 +//
  10 +
  11 +#include <ctime>
  12 +#include <iostream>
  13 +#include <string>
  14 +#include <boost/bind/bind.hpp>
  15 +#include <boost/shared_ptr.hpp>
  16 +#include <boost/enable_shared_from_this.hpp>
  17 +#include <asio.hpp>
  18 +
  19 +using asio::ip::tcp;
  20 +
  21 +std::string make_daytime_string()
  22 +{
  23 + using namespace std; // For time_t, time and ctime;
  24 + time_t now = time(0);
  25 + return ctime(&now);
  26 +}
  27 +
  28 +class tcp_connection
  29 + : public boost::enable_shared_from_this<tcp_connection>
  30 +{
  31 +public:
  32 + typedef boost::shared_ptr<tcp_connection> pointer;
  33 +
  34 + static pointer create(asio::io_context& io_context)
  35 + {
  36 + return pointer(new tcp_connection(io_context));
  37 + }
  38 +
  39 + tcp::socket& socket()
  40 + {
  41 + return socket_;
  42 + }
  43 +
  44 + void start()
  45 + {
  46 + message_ = make_daytime_string();
  47 +
  48 + asio::async_write(socket_, asio::buffer(message_),
  49 + std::bind(&tcp_connection::handle_write, shared_from_this(),
  50 + std::placeholders::_1,
  51 + std::placeholders::_2));
  52 + }
  53 +
  54 +private:
  55 + tcp_connection(asio::io_context& io_context)
  56 + : socket_(io_context)
  57 + {
  58 + }
  59 +
  60 + void handle_write(const asio::error_code& /*error*/,
  61 + size_t /*bytes_transferred*/)
  62 + {
  63 + }
  64 +
  65 + tcp::socket socket_;
  66 + std::string message_;
  67 +};
  68 +
  69 +class tcp_server
  70 +{
  71 +public:
  72 + tcp_server(asio::io_context& io_context)
  73 + : io_context_(io_context),
  74 + acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
  75 + {
  76 + start_accept();
  77 + }
  78 +
  79 +private:
  80 + void start_accept()
  81 + {
  82 + tcp_connection::pointer new_connection =
  83 + tcp_connection::create(io_context_);
  84 +
  85 + acceptor_.async_accept(new_connection->socket(),
  86 + std::bind(&tcp_server::handle_accept, this, new_connection,
  87 + std::placeholders::_1));
  88 + }
  89 +
  90 + void handle_accept(tcp_connection::pointer new_connection,
  91 + const asio::error_code& error)
  92 + {
  93 + if (!error)
  94 + {
  95 + new_connection->start();
  96 + }
  97 +
  98 + start_accept();
  99 + }
  100 +
  101 + asio::io_context& io_context_;
  102 + tcp::acceptor acceptor_;
  103 +};
  104 +
  105 +int main()
  106 +{
  107 + try
  108 + {
  109 + asio::io_context io_context;
  110 + tcp_server server(io_context);
  111 + io_context.run();
  112 + }
  113 + catch (std::exception& e)
  114 + {
  115 + std::cerr << e.what() << std::endl;
  116 + }
  117 +
  118 + return 0;
  119 +}
  120 +
... ...
08-daytime4.cpp 0 → 100644
  1 +//
  2 +// client.cpp
  3 +// ~~~~~~~~~~
  4 +//
  5 +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6 +//
  7 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9 +//
  10 +
  11 +#include <iostream>
  12 +#include <boost/array.hpp>
  13 +#include <asio.hpp>
  14 +
  15 +using asio::ip::udp;
  16 +
  17 +int main(int argc, char* argv[])
  18 +{
  19 + try
  20 + {
  21 + if (argc != 2)
  22 + {
  23 + std::cerr << "Usage: client <host>" << std::endl;
  24 + return 1;
  25 + }
  26 +
  27 + asio::io_context io_context;
  28 +
  29 + udp::resolver resolver(io_context);
  30 + udp::endpoint receiver_endpoint =
  31 + *resolver.resolve(udp::v4(), argv[1], "daytime").begin();
  32 +
  33 + udp::socket socket(io_context);
  34 + socket.open(udp::v4());
  35 +
  36 + boost::array<char, 1> send_buf = {{ 0 }};
  37 + socket.send_to(asio::buffer(send_buf), receiver_endpoint);
  38 +
  39 + boost::array<char, 128> recv_buf;
  40 + udp::endpoint sender_endpoint;
  41 + size_t len = socket.receive_from(
  42 + asio::buffer(recv_buf), sender_endpoint);
  43 +
  44 + std::cout.write(recv_buf.data(), len);
  45 + }
  46 + catch (std::exception& e)
  47 + {
  48 + std::cerr << e.what() << std::endl;
  49 + }
  50 +
  51 + return 0;
  52 +}
  53 +
... ...
09-daytime5.cpp 0 → 100644
  1 +//
  2 +// server.cpp
  3 +// ~~~~~~~~~~
  4 +//
  5 +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6 +//
  7 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9 +//
  10 +
  11 +#include <ctime>
  12 +#include <iostream>
  13 +#include <string>
  14 +#include <boost/array.hpp>
  15 +#include <asio.hpp>
  16 +
  17 +using asio::ip::udp;
  18 +
  19 +std::string make_daytime_string()
  20 +{
  21 + using namespace std; // For time_t, time and ctime;
  22 + time_t now = time(0);
  23 + return ctime(&now);
  24 +}
  25 +
  26 +int main()
  27 +{
  28 + try
  29 + {
  30 + asio::io_context io_context;
  31 +
  32 + udp::socket socket(io_context, udp::endpoint(udp::v4(), 13));
  33 +
  34 + for (;;)
  35 + {
  36 + boost::array<char, 1> recv_buf;
  37 + udp::endpoint remote_endpoint;
  38 + socket.receive_from(asio::buffer(recv_buf), remote_endpoint);
  39 +
  40 + std::string message = make_daytime_string();
  41 +
  42 + asio::error_code ignored_error;
  43 + socket.send_to(asio::buffer(message),
  44 + remote_endpoint, 0, ignored_error);
  45 + }
  46 + }
  47 + catch (std::exception& e)
  48 + {
  49 + std::cerr << e.what() << std::endl;
  50 + }
  51 +
  52 + return 0;
  53 +}
  54 +
... ...
10-daytime6.cpp 0 → 100644
  1 +//
  2 +// server.cpp
  3 +// ~~~~~~~~~~
  4 +//
  5 +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6 +//
  7 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9 +//
  10 +
  11 +#include <ctime>
  12 +#include <iostream>
  13 +#include <string>
  14 +#include <boost/array.hpp>
  15 +#include <boost/bind/bind.hpp>
  16 +#include <boost/shared_ptr.hpp>
  17 +#include <asio.hpp>
  18 +
  19 +using asio::ip::udp;
  20 +
  21 +std::string make_daytime_string()
  22 +{
  23 + using namespace std; // For time_t, time and ctime;
  24 + time_t now = time(0);
  25 + return ctime(&now);
  26 +}
  27 +
  28 +class udp_server
  29 +{
  30 +public:
  31 + udp_server(asio::io_context& io_context)
  32 + : socket_(io_context, udp::endpoint(udp::v4(), 13))
  33 + {
  34 + start_receive();
  35 + }
  36 +
  37 +private:
  38 + void start_receive()
  39 + {
  40 + socket_.async_receive_from(
  41 + asio::buffer(recv_buffer_), remote_endpoint_,
  42 + std::bind(&udp_server::handle_receive, this,
  43 + std::placeholders::_1,
  44 + std::placeholders::_2));
  45 + }
  46 +
  47 + void handle_receive(const asio::error_code& error,
  48 + std::size_t /*bytes_transferred*/)
  49 + {
  50 + if (!error)
  51 + {
  52 + boost::shared_ptr<std::string> message(
  53 + new std::string(make_daytime_string()));
  54 +
  55 + socket_.async_send_to(asio::buffer(*message), remote_endpoint_,
  56 + std::bind(&udp_server::handle_send, this, message,
  57 + std::placeholders::_1,
  58 + std::placeholders::_2));
  59 +
  60 + start_receive();
  61 + }
  62 + }
  63 +
  64 + void handle_send(boost::shared_ptr<std::string> /*message*/,
  65 + const asio::error_code& /*error*/,
  66 + std::size_t /*bytes_transferred*/)
  67 + {
  68 + }
  69 +
  70 + udp::socket socket_;
  71 + udp::endpoint remote_endpoint_;
  72 + boost::array<char, 1> recv_buffer_;
  73 +};
  74 +
  75 +int main()
  76 +{
  77 + try
  78 + {
  79 + asio::io_context io_context;
  80 + udp_server server(io_context);
  81 + io_context.run();
  82 + }
  83 + catch (std::exception& e)
  84 + {
  85 + std::cerr << e.what() << std::endl;
  86 + }
  87 +
  88 + return 0;
  89 +}
  90 +
... ...
11-daytime7.cpp 0 → 100644
  1 +//
  2 +// server.cpp
  3 +// ~~~~~~~~~~
  4 +//
  5 +// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6 +//
  7 +// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8 +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9 +//
  10 +
  11 +#include <ctime>
  12 +#include <iostream>
  13 +#include <string>
  14 +#include <boost/array.hpp>
  15 +#include <boost/bind/bind.hpp>
  16 +#include <boost/shared_ptr.hpp>
  17 +#include <boost/enable_shared_from_this.hpp>
  18 +#include <asio.hpp>
  19 +
  20 +using asio::ip::tcp;
  21 +using asio::ip::udp;
  22 +
  23 +std::string make_daytime_string()
  24 +{
  25 + using namespace std; // For time_t, time and ctime;
  26 + time_t now = time(0);
  27 + return ctime(&now);
  28 +}
  29 +
  30 +class tcp_connection
  31 + : public boost::enable_shared_from_this<tcp_connection>
  32 +{
  33 +public:
  34 + typedef boost::shared_ptr<tcp_connection> pointer;
  35 +
  36 + static pointer create(asio::io_context& io_context)
  37 + {
  38 + return pointer(new tcp_connection(io_context));
  39 + }
  40 +
  41 + tcp::socket& socket()
  42 + {
  43 + return socket_;
  44 + }
  45 +
  46 + void start()
  47 + {
  48 + message_ = make_daytime_string();
  49 +
  50 + asio::async_write(socket_, asio::buffer(message_),
  51 + boost::bind(&tcp_connection::handle_write, shared_from_this()));
  52 + }
  53 +
  54 +private:
  55 + tcp_connection(asio::io_context& io_context)
  56 + : socket_(io_context)
  57 + {
  58 + }
  59 +
  60 + void handle_write()
  61 + {
  62 + }
  63 +
  64 + tcp::socket socket_;
  65 + std::string message_;
  66 +};
  67 +
  68 +class tcp_server
  69 +{
  70 +public:
  71 + tcp_server(asio::io_context& io_context)
  72 + : io_context_(io_context),
  73 + acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
  74 + {
  75 + start_accept();
  76 + }
  77 +
  78 +private:
  79 + void start_accept()
  80 + {
  81 + tcp_connection::pointer new_connection =
  82 + tcp_connection::create(io_context_);
  83 +
  84 + acceptor_.async_accept(new_connection->socket(),
  85 + std::bind(&tcp_server::handle_accept, this, new_connection,
  86 + std::placeholders::_1));
  87 + }
  88 +
  89 + void handle_accept(tcp_connection::pointer new_connection,
  90 + const asio::error_code& error)
  91 + {
  92 + if (!error)
  93 + {
  94 + new_connection->start();
  95 + }
  96 +
  97 + start_accept();
  98 + }
  99 +
  100 + asio::io_context& io_context_;
  101 + tcp::acceptor acceptor_;
  102 +};
  103 +
  104 +class udp_server
  105 +{
  106 +public:
  107 + udp_server(asio::io_context& io_context)
  108 + : socket_(io_context, udp::endpoint(udp::v4(), 13))
  109 + {
  110 + start_receive();
  111 + }
  112 +
  113 +private:
  114 + void start_receive()
  115 + {
  116 + socket_.async_receive_from(
  117 + asio::buffer(recv_buffer_), remote_endpoint_,
  118 + std::bind(&udp_server::handle_receive, this,
  119 + std::placeholders::_1));
  120 + }
  121 +
  122 + void handle_receive(const asio::error_code& error)
  123 + {
  124 + if (!error)
  125 + {
  126 + boost::shared_ptr<std::string> message(
  127 + new std::string(make_daytime_string()));
  128 +
  129 + socket_.async_send_to(asio::buffer(*message), remote_endpoint_,
  130 + boost::bind(&udp_server::handle_send, this, message));
  131 +
  132 + start_receive();
  133 + }
  134 + }
  135 +
  136 + void handle_send(boost::shared_ptr<std::string> /*message*/)
  137 + {
  138 + }
  139 +
  140 + udp::socket socket_;
  141 + udp::endpoint remote_endpoint_;
  142 + boost::array<char, 1> recv_buffer_;
  143 +};
  144 +
  145 +int main()
  146 +{
  147 + try
  148 + {
  149 + asio::io_context io_context;
  150 + tcp_server server1(io_context);
  151 + udp_server server2(io_context);
  152 + io_context.run();
  153 + }
  154 + catch (std::exception& e)
  155 + {
  156 + std::cerr << e.what() << std::endl;
  157 + }
  158 +
  159 + return 0;
  160 +}
  161 +
... ...
makefile
... ... @@ -4,7 +4,7 @@ CFLAGS=-ggdb -Wall -pedantic -D_REENTRANT -DASIO_STANDALONE -DASIO_HAS_CO_AWAIT
4 4 %: %.cpp
5 5 g++-10 -std=c++20 -fcoroutines $(CFLAGS) $< -o $@ -lpthread
6 6  
7   -EXECS = 00-timer1 01-timer2 02-timer3 03-timer4 04-timer5
  7 +EXECS = 00-timer1 01-timer2 02-timer3 03-timer4 04-timer5 05-daytime1 06-daytime2 07-daytime3 08-daytime4 09-daytime5 10-daytime6 11-daytime7
8 8  
9 9 all: $(EXECS)
10 10  
... ...