-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvr.cpp
52 lines (41 loc) · 1.59 KB
/
svr.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
#include <cstdlib>
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
enum { max_length = 1024 };
void server(boost::asio::io_service& io_service, unsigned short port)
{
udp::socket sock(io_service, udp::endpoint(udp::v4(), port));
char data[max_length];
udp::endpoint sender_endpoint1;
size_t length = sock.receive_from(
boost::asio::buffer(data, max_length), sender_endpoint1);
std::cout << "Received from [" << sender_endpoint1 << "]:"
<< std::string(data, length) << std::endl;
udp::endpoint sender_endpoint2;
length = sock.receive_from(
boost::asio::buffer(data, max_length), sender_endpoint2);
std::cout << "Received from [" << sender_endpoint2 << "]:"
<< std::string(data, length) << std::endl;
std::string ip_port1 = sender_endpoint1.address().to_string() + " " +
std::to_string(sender_endpoint1.port());
std::string ip_port2 = sender_endpoint2.address().to_string() + " " +
std::to_string(sender_endpoint2.port());
std::cout << "Sending clients 1 & 2 each other's ip and port.\n";
sock.send_to(boost::asio::buffer(ip_port2), sender_endpoint1);
sock.send_to(boost::asio::buffer(ip_port1), sender_endpoint2);
std::cout << "Clients 1 & 2 know each other now.\n";
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: svr <port>\n";
return 1;
}
try {
boost::asio::io_service io_service;
server(io_service, std::atoi(argv[1]));
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}