TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
unicast_sender_example.cpp File Reference

Example on how to use the UnicastSender. More...

#include <boost/asio.hpp>
#include <iostream>
#include <string>
#include "travesim_adapters/udp/unicast_sender.hpp"
Include dependency graph for unicast_sender_example.cpp:

Go to the source code of this file.

Functions

int main ()
 

Detailed Description

Example on how to use the UnicastSender.

Author
Lucas Haug lucas.nosp@m..hau.nosp@m.g@thu.nosp@m.nera.nosp@m.tz.or.nosp@m.g
Lucas Schneider lucas.nosp@m..sch.nosp@m.neide.nosp@m.r@th.nosp@m.unera.nosp@m.tz.o.nosp@m.rg
Date
04/2021

Definition in file unicast_sender_example.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 24 of file unicast_sender_example.cpp.

24  {
25  const short unicast_port = 30001;
26  const std::string unicast_address_str = "127.0.0.1";
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::UnicastSender my_sender(unicast_address_str, unicast_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 unicast mode.

References travesim::udp::Sender::send().

Here is the call graph for this function: