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

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

#include <iostream>
#include "travesim_adapters/udp/unicast_receiver.hpp"
Include dependency graph for unicast_receiver_example.cpp:

Go to the source code of this file.

Macros

#define BUFFER_SIZE   1024U
 

Functions

int main (int argc, char *argv[])
 

Detailed Description

Example on how to use the UnicastReceiver.

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_receiver_example.cpp.

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   1024U

Definition at line 22 of file unicast_receiver_example.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 28 of file unicast_receiver_example.cpp.

28  {
29  const std::string unicast_address_str = "127.0.0.1";
30  const short unicast_port = 30001;
31 
32  char data_buff[BUFFER_SIZE];
33 
34  try {
35  boost::asio::io_context io_context;
36  boost::asio::steady_timer my_timer(io_context);
37  travesim::udp::UnicastReceiver my_receiver(unicast_address_str, unicast_port);
38 
39  my_receiver.force_specific_source(true);
40 
41  size_t data_size = 0;
42 
43  for (long int i = 0;; i++) {
44  data_size = my_receiver.receive(data_buff, BUFFER_SIZE);
45 
46  if (data_size > 0) {
47  std::string received_msg(data_buff, data_size);
48  std::cout << received_msg << std::endl;
49  }
50 
51  my_timer.expires_after(std::chrono::milliseconds(200));
52  my_timer.wait();
53  }
54  } catch (std::exception& e) {
55  std::cerr << "Exception: " << e.what() << "\n";
56  }
57 
58  return 0;
59 }
Receiver class using UDP in unicast mode.
#define BUFFER_SIZE

References BUFFER_SIZE, travesim::udp::Receiver::force_specific_source(), and travesim::udp::Receiver::receive().

Here is the call graph for this function: