-
Notifications
You must be signed in to change notification settings - Fork 1
/
UdpServer.cpp
58 lines (39 loc) · 1.18 KB
/
UdpServer.cpp
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
#include "UdpServer.hpp"
UdpServer::UdpServer (int portno)
{
using boost::asio::ip::udp;
port = portno;
//instantiate socket
socket = new udp::socket(io_service, udp::endpoint(udp::v4(), port));
}
UdpServer::~UdpServer ()
{
delete [] socket;
}
std::string UdpServer::fetch(){
using boost::asio::ip::udp;
boost::array<char, BUFFER_SIZE> buf;
int received = socket->receive_from(boost::asio::buffer(buf), remote_endpoint, 0, error);
std::string message(buf.begin(), buf.end()-(BUFFER_SIZE-received));
return message;
}
UdpClient::UdpClient(int portno){
using boost::asio::ip::udp;
port = portno;
udp::resolver resolver(io_service);
std::string portstr = std::to_string(port);
std::cout<<"port: "<<portstr<<std::endl;
udp::resolver::query query(udp::v4(), "localhost", portstr);
remote_endpoint = *resolver.resolve(query);
socket = new udp::socket(io_service);
socket->open(udp::v4());
}
UdpClient::~UdpClient(){
socket->close();
delete [] socket;
}
void UdpClient::send(std::string message){
boost::array<char, 90 > sendbuf;
std::copy(message.begin(), message.begin()+message.size(), sendbuf.begin());
socket->send_to(boost::asio::buffer(sendbuf), remote_endpoint);
}