1. 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:

    TP-Link Configuration Screenshot

    From what it all worked on my computer.

  2. 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(); } 
  3. 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 2 
    • The 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?

  • ... because port forwarding is not done on it, how did you do it? Without knowledge of connectivity between machines over the network, this is a guessing game. - D-side
  • And how for example Skype automatically makes forvarding? We do not manually forward ports for each application that needs to communicate with the server - Ilya Polishchuk
  • Or I do not understand something in the documentation, or your code basically can not work. On the client, you are not doing socket.connect(server_ep) - where, in this case, does the client send data? And on the server, a call to socket.receive() should not work, it’s only for sockets with a connection established. - Roman
  • Aah, yes, I forgot to copy the code. (Soryan) There is no connection. and send_to calls. And now, I already have a slightly different problem !!! Dude, help me figure it out: When I flashed a binding to a specific port (in the socket constructor) in the client, a firewall was suddenly popped up by the Windows host. And when the friend1 put a tick there, and opened the program 2 times. everything worked !!!. So, why, if the designer passes the value of the port to zero, the firewall does not suggest ticking? - Ilya Polishchuk
  • Please write all these details in the question: edit . - Roman

0