-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdpServer.cpp
More file actions
33 lines (26 loc) · 863 Bytes
/
UdpServer.cpp
File metadata and controls
33 lines (26 loc) · 863 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
#include "UdpServer.h"
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
UdpServer::UdpServer(boost::asio::io_service& ioService, uint16_t port)
: socket(ioService, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port))
, buffer{}
{
std::cout << "Listening on UDP port " << port << std::endl;
startReceive();
}
void UdpServer::startReceive()
{
socket.async_receive_from(boost::asio::buffer(buffer), remoteEndpoint, boost::bind(&UdpServer::handleReceiveFrom, this, _1, _2));
}
void UdpServer::handleReceiveFrom(const boost::system::error_code& error, size_t bytesReceived)
{
if (error) {
std::cerr << "Error: " << error.message() << std::endl;
}
if (bytesReceived > 0) {
std::cout << bytesReceived << " bytes received" << std::endl;
dataReceived("source", buffer.data(), bytesReceived);
}
startReceive();
}