Server
There is the easiest client and server. The server accepts the packet, sends another packet, and waits for another one, meaning that the client has successfully received the packet.
#include <array> #include <iostream> #include <string> #include <boost/asio.hpp> int main() try{ using namespace std; using namespace boost::asio; using namespace ip; io_service service; udp::socket socket{ service, { ip::udp::v4(), 9999u } }; udp::endpoint client_ep; array<char, 100> recive_buffer; //First receive cout << "start recive\n"; auto recived_length = socket.receive_from(buffer(recive_buffer), client_ep); cout << "message recived from: " + client_ep.address().to_string() + ':' + to_string(client_ep.port()) + "\nrecived message: \""; cout.write(recive_buffer.data(), recived_length); cout << "\""; //Send cout << "\n\nsend answer\n"; socket.send_to(buffer("ANSWER"s), client_ep); cout << "answer sent\n\n"; //Second recive cout << "start recive 2\n"; recived_length = socket.receive(buffer(recive_buffer)); cout << "message recived\nrecived message: \""; cout.write(recive_buffer.data(), recived_length); cout << "\""; } catch (const std::exception& err) { std::cerr << err.what(); }I did the forwarding of my router to my computer:
From what it all worked on my computer.
Customer
The client sends the packet to the server and then receives another packet from the server. If the reception is successful, the client sends another packet.
#include <array> #include <iostream> #include <string> #include <boost/asio.hpp> #include <thread> int main() try{ using namespace std; using namespace boost::asio; using namespace ip; io_service service; udp::socket socket{ service, udp::endpoint{udp::v4(), 9997 } }; this_thread::sleep_for(5s); array<char, 64> recive_buffer; udp::endpoint server_ep{ address::from_string("100.71.188.14"s), 9999u }; socket.send_to(buffer( "local address: "s + socket.local_endpoint().address().to_string() + ':' + std::to_string(socket.local_endpoint().port())) ,server_ep); socket.receive(buffer(recive_buffer)); socket.send_to(buffer("OK"s), server_ep); } catch (const std::exception& err) { std::cerr << err.what(); }Job
The server is running on my computer. The following is the output of server code for clients running on different computers:
The client is running on my computer (in the same place as the server):
C:\Users\effol\Desktop>test-server start receive message recived from: 100.71.188.14:63806 recived message: "local address: 0.0.0.0:63806" send answer answer sent start recive 2 message recived recived message: "OK" C:\Users\effol\Desktop>The client is running on friend's computer 1:
C:\Users\effol\Desktop>test-server start recive message recived from: 188.163.106.39:56120 recived message: "local address: 0.0.0.0:55404" send answer answer sent start recive 2The client is running on friend's computer 2:
C:\Users\effol\Desktop>test-server start recive
Now questions
Why does everything work on my computer? Why on the friend's computer 1 client does not accept messages from the server? Why doesn’t the server’s friend 2 even see his message?
Maybe it's because of the firewall? Whereas all other applications work, such as Skype or torrent Illy batl. Neta. Tell the gentlemen advanced, in which direction to dig, why does this happen, and why does this not happen with other applications?
Issue update
(client code is fixed)
Firewall! When I used to open a socket like this:
udp::socket socket{ service, udp::endpoint{udp::v4(), 0} }; The firewall did not cause a proposal to allow everything that the firewall prohibits for this program. But when I transmit a specific port in the constructor, for example:
udp::socket socket{ service, udp::endpoint{udp::v4(), 9997 } }; Immediately after this command, the firewall is called, and if you tick the boxes there, then everything works for the other1. Why isn't the firewall being called with a random port? How to control it?

socket.connect(server_ep)- where, in this case, does the client send data? And on the server, a call tosocket.receive()should not work, it’s only for sockets with a connection established. - Roman