本文整理汇总了C++中NetworkInterface::broadcastAddress方法的典型用法代码示例。如果您正苦于以下问题:C++ NetworkInterface::broadcastAddress方法的具体用法?C++ NetworkInterface::broadcastAddress怎么用?C++ NetworkInterface::broadcastAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface::broadcastAddress方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testBroadcast
void DatagramSocketTest::testBroadcast()
{
UDPEchoServer echoServer;
DatagramSocket ss(IPAddress::IPv4);
#if defined(POCO_NET_HAS_INTERFACE) && (POCO_OS == POCO_OS_FREE_BSD)
NetworkInterface ni = NetworkInterface::forName("em0");
SocketAddress sa(ni.broadcastAddress(1), echoServer.port());
#else
SocketAddress sa("255.255.255.255", echoServer.port());
#endif
// not all socket implementations fail if broadcast option is not set
/*
try
{
int n = ss.sendTo("hello", 5, sa);
fail ("broadcast option not set - must throw");
}
catch (IOException&)
{
}
*/
ss.setBroadcast(true);
#if (POCO_OS == POCO_OS_FREE_BSD)
int opt = 1;
poco_socklen_t len = sizeof(opt);
ss.impl()->setRawOption(IPPROTO_IP, IP_ONESBCAST, (const char*) &opt, len);
ss.impl()->getRawOption(IPPROTO_IP, IP_ONESBCAST, &opt, len);
assert (opt == 1);
#endif
int n = ss.sendTo("hello", 5, sa);
assert (n == 5);
char buffer[256] = { 0 };
n = ss.receiveBytes(buffer, 5);
assert (n == 5);
assert (std::string(buffer, n) == "hello");
ss.close();
}