skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


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

    udp::socket socket(io_context, {{}, 8080});
    

    For the rest I very much expect it to work. Adding some more interaction, demonstrating on Linux (sorry no windows available):

    Live On Coliru

    #include <array>
    #include <boost/asio.hpp>
    #include <ctime>
    #include <iostream>
    #include <string>
    
    namespace asio = boost::asio;
    using asio::ip::udp;
    
    int main() {
        try {
            asio::io_context io_context;
            udp::socket socket(io_context, {{}, 8080});
    
            char str[26] = {};
            std::array<char, 1024> recv_buf;
    
            for (;;) {
                udp::endpoint sender;
                auto          n = socket.receive_from(asio::buffer(recv_buf), sender);
    
                std::string_view msg(recv_buf.data(), n);
                if (size_t pos; -1ull != (pos = msg.find_last_not_of(" nrtf")))
                    msg = msg.substr(0, pos + 1);
    
                std::cout << "Received " << quoted(msg) << " from " << sender << std::endl;
    
                time_t now = time(NULL);
                ctime_r(&now, str);
    
                boost::system::error_code ignored_error;
                socket.send_to(asio::buffer(std::string_view(str)), sender, 0, ignored_error);
            }
        } catch (std::exception const& e) {
            std::cerr << e.what() << std::endl;
        }
    }
    

    Demo locally with 20 concurrent clients each sending 5 messages endlessly:

    function stream() { while sleep .1; do echo "message $RANDOM"; done; }
    function clients() { split -ul 5 --filter='nc -Nuq0 localhost 8080';     
    for a in {1..20}; do (stream | clients)& done; sleep 10; killall nc
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search