| 1 | //
|
|---|
| 2 | // server.cpp
|
|---|
| 3 | // ~~~~~~~~~~
|
|---|
| 4 | //
|
|---|
| 5 | // Copyright (c) 2003-2017 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 | #define _CRT_SECURE_NO_WARNINGS
|
|---|
| 11 | #include <ctime>
|
|---|
| 12 | #include <iostream>
|
|---|
| 13 | #include <string>
|
|---|
| 14 | #include <boost/array.hpp>
|
|---|
| 15 | #include <boost/bind.hpp>
|
|---|
| 16 | #include <boost/shared_ptr.hpp>
|
|---|
| 17 | #include <boost/asio.hpp>
|
|---|
| 18 | #include <boost/lexical_cast.hpp>
|
|---|
| 19 | #include <boost/thread.hpp>
|
|---|
| 20 | using boost::asio::ip::udp;
|
|---|
| 21 | using std::cout;
|
|---|
| 22 | using std::cin;
|
|---|
| 23 | using std::endl;
|
|---|
| 24 |
|
|---|
| 25 | class sender
|
|---|
| 26 | {
|
|---|
| 27 | private:
|
|---|
| 28 | boost::asio::ip::udp::endpoint endpoint_;
|
|---|
| 29 | boost::asio::ip::udp::socket socket_;
|
|---|
| 30 | boost::asio::steady_timer timer_;
|
|---|
| 31 | int message_count_;
|
|---|
| 32 | std::string message_;
|
|---|
| 33 | short multicast_port = 13000; //30001
|
|---|
| 34 | int max_message_count = 10;
|
|---|
| 35 |
|
|---|
| 36 | public:
|
|---|
| 37 | sender(boost::asio::io_context& io_context, const boost::asio::ip::address& multicast_address)
|
|---|
| 38 | : endpoint_(multicast_address, multicast_port),
|
|---|
| 39 | socket_(io_context, endpoint_.protocol()),
|
|---|
| 40 | timer_(io_context),
|
|---|
| 41 | message_count_(0)
|
|---|
| 42 | {
|
|---|
| 43 | //message_ = "abcd";
|
|---|
| 44 | //do_send();
|
|---|
| 45 | send_periodic();
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | private:
|
|---|
| 49 | void do_send()
|
|---|
| 50 | {
|
|---|
| 51 | static int i = 0;
|
|---|
| 52 | cout << i << endl; // show count
|
|---|
| 53 | ++i;
|
|---|
| 54 |
|
|---|
| 55 | std::ostringstream os;
|
|---|
| 56 | os << "Message " << message_count_++;
|
|---|
| 57 | message_ = os.str();
|
|---|
| 58 |
|
|---|
| 59 | socket_.async_send_to(boost::asio::buffer(message_), endpoint_, [this](boost::system::error_code ec, std::size_t /*length*/)
|
|---|
| 60 | {
|
|---|
| 61 | //if (!ec && message_count_ < max_message_count)
|
|---|
| 62 | if (!ec)
|
|---|
| 63 | do_timeout();
|
|---|
| 64 | });
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void do_timeout()
|
|---|
| 68 | {
|
|---|
| 69 | timer_.expires_after(std::chrono::seconds(1));
|
|---|
| 70 | timer_.async_wait([this](boost::system::error_code ec)
|
|---|
| 71 | {
|
|---|
| 72 | if (!ec)
|
|---|
| 73 | do_send();
|
|---|
| 74 | });
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void send_periodic()
|
|---|
| 78 | {
|
|---|
| 79 | static int i = 0;
|
|---|
| 80 | message_ = some_string();
|
|---|
| 81 |
|
|---|
| 82 | socket_.async_send_to(boost::asio::buffer(message_), endpoint_, [this](boost::system::error_code ec, std::size_t /*length*/)
|
|---|
| 83 | {
|
|---|
| 84 | //cout << i << endl; // show count
|
|---|
| 85 | cout << i << " - " << message_; // show count
|
|---|
| 86 | ++i;
|
|---|
| 87 | });
|
|---|
| 88 |
|
|---|
| 89 | timer_.expires_after(std::chrono::seconds(1));
|
|---|
| 90 | timer_.async_wait([this](boost::system::error_code ec)
|
|---|
| 91 | {
|
|---|
| 92 | send_periodic();
|
|---|
| 93 | });
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | std::string make_daytime_string()
|
|---|
| 97 | {
|
|---|
| 98 | using namespace std; // For time_t, time and ctime;
|
|---|
| 99 | time_t now = time(0);
|
|---|
| 100 | return ctime(&now);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | std::string some_string()
|
|---|
| 104 | {
|
|---|
| 105 | std::string result;
|
|---|
| 106 | // ====================================================
|
|---|
| 107 | //result = "abcd";
|
|---|
| 108 | // ====================================================
|
|---|
| 109 | //os << "Message " << message_count_++;
|
|---|
| 110 | //result = os.str();
|
|---|
| 111 | // ====================================================
|
|---|
| 112 | //std::stringstream ss;
|
|---|
| 113 | //ss << i;
|
|---|
| 114 | //result = ss.str();
|
|---|
| 115 | // ====================================================
|
|---|
| 116 | result = make_daytime_string();
|
|---|
| 117 |
|
|---|
| 118 | return result;
|
|---|
| 119 | }
|
|---|
| 120 | };
|
|---|
| 121 |
|
|---|
| 122 | int main(int argc, char* argv[])
|
|---|
| 123 | {
|
|---|
| 124 | // ============================================================================================================
|
|---|
| 125 | try
|
|---|
| 126 | {
|
|---|
| 127 | //if (argc != 2)
|
|---|
| 128 | //{
|
|---|
| 129 | // std::cerr << "Usage: sender <multicast_address>\n";
|
|---|
| 130 | // std::cerr << " For IPv4, try:\n";
|
|---|
| 131 | // std::cerr << " sender 239.255.0.1\n";
|
|---|
| 132 | // std::cerr << " For IPv6, try:\n";
|
|---|
| 133 | // std::cerr << " sender ff31::8000:1234\n";
|
|---|
| 134 | // return 1;
|
|---|
| 135 | //}
|
|---|
| 136 |
|
|---|
| 137 | boost::asio::io_context io_context;
|
|---|
| 138 | //sender s(io_context, boost::asio::ip::make_address(argv[1]));
|
|---|
| 139 | //sender s(io_context, boost::asio::ip::make_address("127.0.0.1"));
|
|---|
| 140 | //sender s(io_context, boost::asio::ip::make_address("239.255.0.1"));
|
|---|
| 141 | sender s(io_context, boost::asio::ip::make_address("192.168.0.255"));
|
|---|
| 142 | io_context.run();
|
|---|
| 143 | }
|
|---|
| 144 | catch (std::exception& e)
|
|---|
| 145 | {
|
|---|
| 146 | std::cerr << "Exception: " << e.what() << "\n";
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | return 0;
|
|---|
| 150 | }
|
|---|