-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver.cpp
More file actions
125 lines (110 loc) · 3.9 KB
/
receiver.cpp
File metadata and controls
125 lines (110 loc) · 3.9 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// receiver.cpp
// ~~~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "boost/bind.hpp"
#include <boost/asio.hpp>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
const short multicast_port = 30001;
class receiver
{
public:
receiver(boost::asio::io_service &io_service,
const boost::asio::ip::address &listen_address,
const boost::asio::ip::address &multicast_address)
: socket_(io_service)
{
// Create the socket so that multiple may be bound to the same address.
boost::asio::ip::udp::endpoint listen_endpoint(
listen_address, multicast_port);
socket_.open(listen_endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(listen_endpoint);
// Join the multicast group.
socket_.set_option(
boost::asio::ip::multicast::join_group(multicast_address));
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&receiver::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_receive_from(const boost::system::error_code &error,
size_t bytes_recvd)
{
if (!error)
{
std::string msg_recv(data_, bytes_recvd);
// std::cout.write(data_, bytes_recvd);
// std::cout << std::endl;
std::string ip_str = sender_endpoint_.address().to_string();
std::cout << msg_recv << " " << ip_str << std::endl;
// std::ostringstream os;
// os << "echo " << msg_recv << " " << ip_str;
// std::string message_ = os.str();
users[ip_str] = msg_recv;
std::cout << "map is" << to_string(users) << std::endl;
socket_.async_receive_from(
boost::asio::buffer(data_, max_length), sender_endpoint_,
boost::bind(&receiver::handle_receive_from, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
socket_.send_to(boost::asio::buffer(to_string(users)), sender_endpoint_);
}
}
private:
boost::asio::ip::udp::socket socket_;
boost::asio::ip::udp::endpoint sender_endpoint_;
std::map<std::string, std::string> users;
enum
{
max_length = 1024
};
char data_[max_length];
std::string to_string(std::map<std::string, std::string> m)
{
std::string result;
std::string obj;
for (auto it = m.cbegin(); it != m.cend(); ++it)
{
obj = "[" + it->first + ":" + it->second + "],";
// std::cout << obj << std::endl;
result += obj;
}
// std::cout << result << std::endl;
return result;
}
};
int main(int argc, char *argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: receiver <listen_address> <multicast_address>\n";
std::cerr << " For IPv4, try:\n";
std::cerr << " receiver 0.0.0.0 239.255.0.1\n";
std::cerr << " For IPv6, try:\n";
std::cerr << " receiver 0::0 ff31::8000:1234\n";
return 1;
}
boost::asio::io_service io_service;
receiver r(io_service,
boost::asio::ip::address::from_string(argv[1]),
boost::asio::ip::address::from_string(argv[2]));
io_service.run();
}
catch (std::exception &e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}