簡體   English   中英

Boost UDP 多播發送方未使用正確的端口

[英]Boost UDP multicast sender not using correct port

我下面加速UDP多播發送器教程在這里 我將其修改為如下:

#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;
}    

端口設置為13000。我用Wireshark檢查過。 它顯示包是用源端口 62910 和目標端口 0 發送的。此外,從網絡上的另一台計算機看不到包。

我之前測試過單播 UDP 時間服務器客戶端Boost 提供的示例代碼。 它們工作正常,並且使用 Wireshark 正確觀察到指定的端口。 有人能告訴我出了什么問題嗎?

成員endpoint在成員multicast_port之前初始化,因為它們被聲明的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM