C++ Boost asio set_选项错误

C++ Boost asio set_选项错误,c++,boost-asio,C++,Boost Asio,我试图在redhat中使用参数0.0.0.0 127.0.0.1运行以下程序,但在socket.set_选项(boost::asio::ip::multicast::join_group(multicast_address))处出现“Exception:set_option:Invalid argument”错误 它还尝试在窗口中运行它,并且在vc++中运行良好。我快没主意了。请帮帮我 // // receiver.cpp // ~~~~~~~~~~~~ // // Copyright (c)

我试图在redhat中使用参数0.0.0.0 127.0.0.1运行以下程序,但在socket.set_选项(boost::asio::ip::multicast::join_group(multicast_address))处出现“Exception:set_option:Invalid argument”错误

它还尝试在窗口中运行它,并且在vc++中运行良好。我快没主意了。请帮帮我

//
// receiver.cpp
// ~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include "boost/bind.hpp"

const short multicast_port = 30001;

class receiver
{
public:
  receiver(boost::asio::io_service& io_service,
      const boost::asio::ip::address& listen_address,
      const boost::asio::ip::address& multicast_address)
    : socket_(io_service)
  {
    // Create the socket so that multiple may be bound to the same address.
    boost::asio::ip::udp::endpoint listen_endpoint(
        listen_address, multicast_port);
    socket_.open(listen_endpoint.protocol());
    socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    socket_.bind(listen_endpoint);

    **// Join the multicast group.
    socket_.set_option(
        boost::asio::ip::multicast::join_group(multicast_address)); <-- Error**

    socket_.async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&receiver::handle_receive_from, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

  void handle_receive_from(const boost::system::error_code& error,
      size_t bytes_recvd)
  {
    if (!error)
    {
      std::cout.write(data_, bytes_recvd);
      std::cout << std::endl;

      socket_.async_receive_from(
          boost::asio::buffer(data_, max_length), sender_endpoint_,
          boost::bind(&receiver::handle_receive_from, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
  }

private:
  boost::asio::ip::udp::socket socket_;
  boost::asio::ip::udp::endpoint sender_endpoint_;
  enum { max_length = 1024 };
  char data_[max_length];
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 3)
    {
      std::cerr << "Usage: receiver <listen_address> <multicast_address>\n";
      std::cerr << "  For IPv4, try:\n";
      std::cerr << "    receiver 0.0.0.0 239.255.0.1\n";
      std::cerr << "  For IPv6, try:\n";
      std::cerr << "    receiver 0::0 ff31::8000:1234\n";
      return 1;
    }

    boost::asio::io_service io_service;
    receiver r(io_service,
        boost::asio::ip::address::from_string(argv[1]),
        boost::asio::ip::address::from_string(argv[2]));
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}
//
//receiver.cpp
// ~~~~~~~~~~~~
//
//版权所有(c)2003-2008克里斯托弗·M·科尔霍夫(克里斯在科尔霍夫网站)
//
//根据Boost软件许可证1.0版发布。(见附页)
//文件LICENSE_1_0.txt或复制到http://www.boost.org/LICENSE_1_0.txt)
//
#包括
#包括
#包括
#包括“boost/bind.hpp”
const short multicast_port=30001;
类接收器
{
公众:
接收器(boost::asio::io_服务和io_服务,
常量boost::asio::ip::地址和侦听地址,
const boost::asio::ip::地址和多播地址)
:socket_(io_服务)
{
//创建套接字,以便将多个套接字绑定到同一地址。
boost::asio::ip::udp::端点侦听\U端点(
侦听地址、多播端口);
socket_u.open(listen_endpoint.protocol());
套接字\设置\选项(boost::asio::ip::udp::socket::重用\地址(true));
套接字绑定(侦听端点);
**//加入多播组。
套接字设置选项(
boost::asio::ip::multicast::join_group(multicast_address));从,您需要

socket_.set_option(
    boost::asio::ip::multicast::join_group(multicast_address.to_v4()));
也就是说,在
ip::address
变量上添加
.to_v4()

From,您需要

socket_.set_option(
    boost::asio::ip::multicast::join_group(multicast_address.to_v4()));

也就是说,在您的
ip::address
变量上附加
.to_v4()

我不应该使用本地ip。它不是一个多播ip格式。它不是一个多播ip格式

谢谢。我还从google进行了一些搜索。这个解决方案无法解决我的错误。socket\uu.set\u选项(boost::asio::ip::multicast::join_group(multicast_address.to_v4());@BryanFok当您创建
listen_端点
时,是否传递
listen_address.to_v4()
?我注意到链接的问题使用了
udp::v4()
。刚刚尝试过。boost::asio::ip::udp::endpoint listent_端点(listent_address.to_v4(),multicast_端口).相同的错误谢谢。我也从谷歌上搜索了一些。这个解决方案无法解决我的错误。socket_u.set_选项(boost::asio::ip::multicast::join_group(multicast_address.to_v4());@BryanFok当你创建
listen_端点
时,你是否也将
listen_address.to_v4()
传递给了?我注意到链接的问题使用了
udp::v4()
。刚刚尝试过。boost::asio::ip::udp::endpoint listen\u endpoint(listen\u address.to\u v4(),multicast\u port);。同样的错误我不应该使用本地ip。它不是一种多播ip格式我不应该使用本地ip。它不是一种多播ip格式