我无法从MacOS上的Boost ASIO库运行多播示例。如果我按照代码本身的建议运行IPv6示例,则会收到以下错误:
./receiver 0::0 ff31::8000:1234
Exception: set_option: Can't assign requested address
按照代码中的建议使用IPv4地址时,可以正常使用。
我已经在Windows和Linux上使用过类似的代码,并且效果很好。现在,我正在MacOS上编译代码,它的破坏方式与官方示例代码相同。引发异常的行是这一行:
socket_.set_option(boost::asio::ip::multicast::join_group(multicast_address));
我无法理解失败的原因。
简化的示例代码(original source from boost.org)如下:
#include <array>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
class receiver
{
public:
receiver(boost::asio::io_context &io_context,
const boost::asio::ip::address &listen_address,
const boost::asio::ip::address &multicast_address)
: socket_(io_context)
{
boost::asio::ip::udp::endpoint listen_endpoint(listen_address, 30001);
socket_.open(listen_endpoint.protocol());
socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
socket_.bind(listen_endpoint);
// === THE FOLLOWING LINE FAILS ===
socket_.set_option(boost::asio::ip::multicast::join_group(multicast_address));
}
private:
boost::asio::ip::udp::socket socket_;
boost::asio::ip::udp::endpoint sender_endpoint_;
};
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_context io_context;
receiver r(io_context,
boost::asio::ip::make_address(argv[1]),
boost::asio::ip::make_address(argv[2]));
io_context.run();
}
catch (std::exception &e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
我的机器有很多网络接口(我猜是从安装VirtualBox来的),我想知道它是否与它有关(替换了MAC地址):
ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
inet 127.0.0.1 netmask 0xff000000
inet6 ::1 prefixlen 128
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
XHC1: flags=0<> mtu 0
XHC0: flags=0<> mtu 0
XHC20: flags=0<> mtu 0
VHC128: flags=0<> mtu 0
en5: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether XX:XX:XX:XX:XX:XX
inet6 fe80::aede:48ff:fe00:1122%en5 prefixlen 64 scopeid 0x8
nd6 options=201<PERFORMNUD,DAD>
media: autoselect (100baseTX <full-duplex>)
status: active
ap1: flags=8802<BROADCAST,SIMPLEX,MULTICAST> mtu 1500
ether XX:XX:XX:XX:XX:XX
media: autoselect
status: inactive
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether XX:XX:XX:XX:XX:XX
inet6 fe80::18ae:f381:4c4c:62e6%en0 prefixlen 64 secured scopeid 0xa
inet 192.168.43.242 netmask 0xffffff00 broadcast 192.168.43.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
ether XX:XX:XX:XX:XX:XX
media: autoselect
status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
ether XX:XX:XX:XX:XX:XX
inet6 fe80::2483:b4ff:fec9:2590%awdl0 prefixlen 64 scopeid 0xc
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether XX:XX:XX:XX:XX:XX
media: autoselect <full-duplex>
status: inactive
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether XX:XX:XX:XX:XX:XX
media: autoselect <full-duplex>
status: inactive
en3: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether XX:XX:XX:XX:XX:XX
media: autoselect <full-duplex>
status: inactive
en4: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=60<TSO4,TSO6>
ether XX:XX:XX:XX:XX:XX
media: autoselect <full-duplex>
status: inactive
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether XX:XX:XX:XX:XX:XX
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en1 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 13 priority 0 path cost 0
member: en2 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 14 priority 0 path cost 0
member: en3 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 15 priority 0 path cost 0
member: en4 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 16 priority 0 path cost 0
nd6 options=201<PERFORMNUD,DAD>
media: <unknown type>
status: inactive
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
inet6 fe80::2f34:55e7:4eee:9260%utun0 prefixlen 64 scopeid 0x12
nd6 options=201<PERFORMNUD,DAD>