TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
multicast_sender_example.cpp
Go to the documentation of this file.
1 /**
2  * @file multicast_sender_example.cpp
3  *
4  * @author Lucas Haug <lucas.haug@thuneratz.org>
5  * @author Lucas Schneider <lucas.schneider@thuneratz.org>
6  *
7  * @brief Example on how to use the MulticastSender
8  *
9  * @date 04/2021
10  *
11  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
12  */
13 
14 #include <boost/asio.hpp>
15 #include <iostream>
16 #include <string>
17 
19 
20 /*****************************************
21  * Main Function
22  *****************************************/
23 
24 int main() {
25  const std::string multicast_address_str = "224.0.0.1";
26  const short multicast_port = 10002;
27  const int max_message_count = 10;
28 
29  try {
30  boost::asio::io_context io_context;
31  boost::asio::steady_timer my_timer(io_context);
32  travesim::udp::MulticastSender my_sender(multicast_address_str, multicast_port);
33 
34  for (int i = 0; i < max_message_count; i++) {
35  std::string msg = "Menssagem " + std::to_string(i);
36  size_t bytes_sent = my_sender.send(msg.c_str(), msg.length());
37 
38  std::cout << msg << "\nBytes sent: " << bytes_sent << "\n\n";
39 
40  my_timer.expires_after(std::chrono::milliseconds(200));
41  my_timer.wait();
42  }
43  } catch (std::exception& e) {
44  std::cerr << "Exception: " << e.what() << "\n";
45  }
46 
47  return 0;
48 }
Sender class using UDP in multicast mode.
size_t send(const char *buffer, const size_t buffer_size)
Send data using UDP.
Definition: sender.cpp:46
Send data using UDP in multicast mode.