-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUdpSender.h
More file actions
37 lines (31 loc) · 858 Bytes
/
UdpSender.h
File metadata and controls
37 lines (31 loc) · 858 Bytes
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
#pragma once
#include <boost/asio.hpp>
#include <iostream>
#include <string>
using namespace std;
class UdpSender
{
public:
UdpSender(const string& host, unsigned short port)
: io_context_(),
socket_(io_context_),
endpoint_(boost::asio::ip::make_address(host), port)
{
socket_.open(boost::asio::ip::udp::v4());
}
~UdpSender()
{
socket_.close();
}
void SendString(const string& message)
{
boost::system::error_code ec;
socket_.send_to(boost::asio::buffer(message), endpoint_, 0, ec);
if (ec)
std::cerr << "UDP sending error: " << ec.message() << std::endl;
}
private:
boost::asio::io_context io_context_;
boost::asio::ip::udp::socket socket_;
boost::asio::ip::udp::endpoint endpoint_;
};