简体   繁体   English

Boost UDP 多播发送方未使用正确的端口

[英]Boost UDP multicast sender not using correct port

I am following Boost UDP multicast sender tutorial here .我下面加速UDP多播发送器教程在这里 I modify it to as follow:我将其修改为如下:

#define _CRT_SECURE_NO_WARNINGS
#include <ctime>
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
using boost::asio::ip::udp;
using std::cout;
using std::cin;
using std::endl;

class sender
{
private:
    boost::asio::ip::udp::endpoint endpoint_;
    boost::asio::ip::udp::socket socket_;
    boost::asio::steady_timer timer_;
    int message_count_;
    std::string message_;
    short multicast_port = 13000;
    int max_message_count = 10;

public:
    sender(boost::asio::io_context& io_context, const boost::asio::ip::address& multicast_address)
        : endpoint_(multicast_address, multicast_port),
        socket_(io_context, endpoint_.protocol()),
        timer_(io_context),
        message_count_(0)
    {
        send_periodic();
    }

private:
    void send_periodic()
    {
        static int i = 0;
        message_ = some_string();

        socket_.async_send_to(boost::asio::buffer(message_), endpoint_, [this](boost::system::error_code ec, std::size_t /*length*/)
        {
            //cout << i << endl;    // show count
            cout << i << " - " << message_; // show  count
            ++i;
        });

        timer_.expires_after(std::chrono::seconds(1));
        timer_.async_wait([this](boost::system::error_code ec)
        {
            send_periodic();
        });
    }

    std::string make_daytime_string()
    {
        using namespace std; // For time_t, time and ctime;
        time_t now = time(0);
        return ctime(&now);
    }

    std::string some_string()
    {
        std::string result;
        // ====================================================
        //result = "abcd";
        // ====================================================
        //os << "Message " << message_count_++;
        //result = os.str();
        // ====================================================
        //std::stringstream ss;
        //ss << i;
        //result = ss.str();
        // ====================================================
        result = make_daytime_string();

        return result;
    }
};

int main(int argc, char* argv[])
{
    // ============================================================================================================
    try
    {
        boost::asio::io_context io_context;
        //sender s(io_context, boost::asio::ip::make_address("127.0.0.1"));     // doesn't work, why?
        sender s(io_context, boost::asio::ip::make_address("239.255.0.1"));
        io_context.run();
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}    

The port is set to 13000. I check it with Wireshark.端口设置为13000。我用Wireshark检查过。 It shows me that the packages are sent with source port of 62910, and destination port 0. Also, the packages are not seen from another computer on the network.它显示包是用源端口 62910 和目标端口 0 发送的。此外,从网络上的另一台计算机看不到包。

I have previously tested the unicast UDP time server and client Boost provided sample codes.我之前测试过单播 UDP 时间服务器客户端Boost 提供的示例代码。 They work, and the port specified is correctly observed with Wireshark.它们工作正常,并且使用 Wireshark 正确观察到指定的端口。 Could someone tell me what went wrong?有人能告诉我出了什么问题吗?

成员endpoint在成员multicast_port之前初始化,因为它们被声明的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM