1.server.cpp

#include <iostream>
#include <string>
#include "boost/asio.hpp"
using namespace std;
using namespace boost::asio;
static const size_t BUF_SIZE = 1024;
int main() {
io_service ios;
ip::udp::socket udp_server(ios);
ip::udp::endpoint local_addr(ip::address::from_string("127.0.0.1"), 9001);
udp_server.open(local_addr.protocol());
udp_server.bind(local_addr);
char buf[BUF_SIZE] = "";
ip::udp::endpoint send_point;
int recv_len = 0;
while (true) {
recv_len = udp_server.receive_from(buffer(buf, BUF_SIZE), send_point);
cout << "server recv size = " << recv_len << endl;
cout << "server recv message = " << buf << endl;
cout << "server send back size = " << udp_server.send_to(buffer(buf, recv_len), send_point) << endl;
}

return 0;
}

2.client.cpp

#include <iostream>
#include "boost/asio.hpp"
using namespace std;
using namespace boost::asio;
static const size_t BUF_SIZE = 1024;
int main() {
io_service ios;
ip::udp::socket sock(ios);
ip::udp::endpoint end_point(ip::address::from_string("127.0.0.1"), 9001);
sock.open(end_point.protocol());
char buf[BUF_SIZE] = "";
string str;
while (true) {
cout << "input str = ";
cin >> str;
try {
cout << "client send size = " << sock.send_to(buffer(str.c_str(), str.size()), end_point) << endl;
cout << "client recv size = " << sock.receive_from(buffer(buf, BUF_SIZE), end_point) << endl;
cout << "client recv message = " << buf << endl;
}
catch (boost::system::system_error &e) {
cerr << e.what() << endl;
}
}


return 0;
}

3.make.sh

g++ -std=c++17 -g -o ServerTest server.cpp -I /opt/boost_1_69_0 -pthread
g++ -std=c++17 -g -o ClientTest client.cpp -I /opt/boost_1_69_0 -pthread