当前位置: 首页>>代码示例>>C++>>正文


C++ QUdpSocket::setMulticastInterface方法代码示例

本文整理汇总了C++中QUdpSocket::setMulticastInterface方法的典型用法代码示例。如果您正苦于以下问题:C++ QUdpSocket::setMulticastInterface方法的具体用法?C++ QUdpSocket::setMulticastInterface怎么用?C++ QUdpSocket::setMulticastInterface使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QUdpSocket的用法示例。


在下文中一共展示了QUdpSocket::setMulticastInterface方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: announceRecords

void Discover::announceRecords ()
{
	// Reset timer
	timer->start(announcePeriodMsec);

	if (not running) return;

	// Servers don't announce periodically or send departure messages
	if (mServerMode) return;

	// Start building a list of records to send in each scope
	QList<Record> recordsToSend;

	///////////////////////////////////////////////////////////////////////
	// Global scope first
	///////////////////////////////////////////////////////////////////////
	for (Record record : ownedRecords)
	if (record["Scope"] == "Global")
	{
		recordsToSend.push_back(record);
	}
	if (not recordsToSend.empty() or defaultScope=="Global")
	{
		// This is a discovery request which should be responded to
		QByteArray datagram("DSDR");
		if (departure) datagram = "DSDD";
		datagram += makeDatagram(recordsToSend, false);

		// Send to each Global server
		for (QPair<QHostAddress,quint16> globalServer : globalServers)
		{
			globalSocket->writeDatagram(datagram, globalServer.first, globalServer.second);
		}
	}

	///////////////////////////////////////////////////////////////////////
	// Local scope next
	///////////////////////////////////////////////////////////////////////
	for (Record record : ownedRecords)
	if (record["Scope"] == "Local")
	{
		recordsToSend.push_back(record);
	}
	if (not recordsToSend.empty() or defaultScope=="Global" or defaultScope=="Local")
	{
		QByteArray datagram("DSDA");
		if (announceNeedsResponse) datagram = "DSDR";
		if (departure)             datagram = "DSDD";
		datagram += makeDatagram(recordsToSend, true);

		// For each interface
		for (auto iface : QNetworkInterface::allInterfaces())
		{
			// For each IPv4 address on this interface
			for (auto entry : iface.addressEntries())
			{
				if (entry.ip().protocol() != QAbstractSocket::IPv4Protocol) continue;

				// Add it to addressEntryCache if necessary for local scope testing
				if (not addressEntryCache.contains(entry)) addressEntryCache.push_back(entry);

				// Multicast
				if (iface.flags().testFlag(QNetworkInterface::CanMulticast) and !entry.ip().isNull() and !entry.ip().isLoopback())
				{
					// Create the socket if it doesn't exit yet
					if (not multiSocket.contains(entry.ip().toString()))
					{
						logDebug(QString("New multicast socket: %1 %2").arg(entry.ip().toString(), entry.netmask().toString()));

						// Add it, create and bind the socket
						QUdpSocket* socket = new QUdpSocket(this);
						connect(socket, SIGNAL(readyRead()), this, SLOT(readDatagrams()));
						socket->setSocketOption(QAbstractSocket::MulticastTtlOption, 1);
						if (not socket->bind(QHostAddress::AnyIPv4, port, QAbstractSocket::ShareAddress | QAbstractSocket::ReuseAddressHint))
						{
							logWarning(QString("Error binding to iface %1: %2").arg(entry.ip().toString(), socket->errorString()));
						}
						socket->setMulticastInterface(iface);
						socket->joinMulticastGroup(groupAddress, iface);
						multiSocket.insert(entry.ip().toString(), socket);
					}

					// Send datagram
					multiSocket[entry.ip().toString()]->writeDatagram(datagram, groupAddress, port);
				}
			}
		}
	}

	///////////////////////////////////////////////////////////////////////
	// Loopback scope last
	///////////////////////////////////////////////////////////////////////
	for (Record record : ownedRecords)
	if (record["Scope"] == "Loopback")
	{
		recordsToSend.push_back(record);
	}
	if (not recordsToSend.empty() or true) // Any scope is above or equivalent to loopback
	{
		QByteArray datagram("DSDA");
//.........这里部分代码省略.........
开发者ID:dustyco,项目名称:Drone,代码行数:101,代码来源:Discover.cpp


注:本文中的QUdpSocket::setMulticastInterface方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。