-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_sender.h
67 lines (53 loc) · 1.79 KB
/
udp_sender.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef UDP_SENDER_H
#define UDP_SENDER_H
#include "udp_config.h"
#include <boost/asio.hpp>
/*
// example use
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include "udp_sender.h"
int main()
{
// send message to 127.0.0.1 port 10253
udp_sender udp("127.0.0.1", 10253);
// just loop and send a message every second forever
size_t counter=0;
while(true) {
udp.send("udp_sender " + std::to_string(++counter));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
*/
// udp_sender sends single UDP messages to remote IP and port
class udp_sender {
public:
// udp_port = remote UDP port (on receiving end)
udp_sender(const std::string& ipaddress, int udp_port = 10253)
: m_ipaddress(ipaddress)
, m_udp_port(udp_port)
, m_io_service()
, m_socket(m_io_service, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0))
, m_receiver_endpoint(boost::asio::ip::address::from_string(ipaddress), udp_port)
{}
virtual ~udp_sender()
{
m_socket.close();
}
// send a single string as UDP message
void send(const std::string& msg)
{
m_socket.send_to(boost::asio::buffer(msg, msg.size()), m_receiver_endpoint);
}
std::string ipaddress() const { return m_ipaddress; }
int udp_port() const { return m_udp_port; }
private:
std::string m_ipaddress; // remote IP-address (on receiving end)
int m_udp_port; // remote UDP port (on receiving end)
boost::asio::io_service m_io_service;
boost::asio::ip::udp::socket m_socket; // socket used for communication
boost::asio::ip::udp::endpoint m_receiver_endpoint; // remote endpoint (on receiving end)
};
#endif // UDP_SENDER_H