I am trying out the example async and sync UDP server example in the boost examples.
The boost version is 1.84.0, Visual Studio 2019.
The code compiles and netstat shows the server is listening on the correct port. Wireshark shows a UDP client is sending data to the UDP port BUT any break point after the function socket.receive_from() or the call back for the async example is not showing any success on receiving data.
Any thoughts anyone?
#include <array>
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
int main()
{
try
{
boost::asio::io_context io_context;
udp::socket socket(io_context, udp::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 8080));
time_t now = time(NULL);
//char *str = ctime(&now);
char str[26] = {};
ctime_s(str, 26, &now);
for (;;)
{
std::array<char, 1> recv_buf;
udp::endpoint remote_endpoint;
socket.receive_from(boost::asio::buffer(recv_buf), remote_endpoint);
std::string message = std::string(str);
boost::system::error_code ignored_error;
socket.send_to(boost::asio::buffer(message),
remote_endpoint, 0, ignored_error);
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
2
Answers
The issue was with the udp sender app from the microsoft store. It seems win10 does not allow apps to use loopback by default. Strange that wireshark was able to see the UDP packet being sent.
Started using PacketSender https://packetsender.com/download and its now working.
Thanks.
Check firewalls, different target ip addresses or (virtual) network adaptors.
To rule out the latter you could start with not binding a specific address (127.0.0.1) in the first place:
For the rest I very much expect it to work. Adding some more interaction, demonstrating on Linux (sorry no windows available):
Live On Coliru
Demo locally with 20 concurrent clients each sending 5 messages endlessly: