本文整理汇总了C++中QNetworkInterface::addressEntries方法的典型用法代码示例。如果您正苦于以下问题:C++ QNetworkInterface::addressEntries方法的具体用法?C++ QNetworkInterface::addressEntries怎么用?C++ QNetworkInterface::addressEntries使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QNetworkInterface
的用法示例。
在下文中一共展示了QNetworkInterface::addressEntries方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: accessPointChanged
// Go through all interfaces and add all IPs for interfaces that can
// broadcast and are not local.
foreach (QNetworkInterface networkInterface,
QNetworkInterface::allInterfaces())
{
if (!networkInterface.addressEntries().isEmpty()
&& networkInterface.isValid())
{
QNetworkInterface::InterfaceFlags interfaceFlags =
networkInterface.flags();
if (interfaceFlags & QNetworkInterface::IsUp
&& interfaceFlags & QNetworkInterface::IsRunning
&& interfaceFlags & QNetworkInterface::CanBroadcast
&& !(interfaceFlags & QNetworkInterface::IsLoopBack))
{
qDebug() << "ConnectionManager::handleNetworkSessionOpened():"
<< "The connection is valid!";
if (mNetworkSession) {
mAccessPoint = mNetworkSession->configuration().name();
emit accessPointChanged(mAccessPoint);
}
mMyIp.setAddress(networkInterface.addressEntries().at(0).ip().toString());
emit myIpChanged(mMyIp.toString());
setState(Connected);
return;
}
}
}
示例2: QObject
OceanSocket::OceanSocket(QObject *parent) :
QObject(parent)
{
// setup the heartbeat timer
heartbeat = new QTimer(this);
connect(heartbeat, SIGNAL(timeout()), this, SLOT(heartbeatTimer()));
// setup our ocean sockets
groupAddress = QHostAddress(MULTICAST_ADDRESS);
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
for (int i = 0; i < ifaces.count(); i++) {
QNetworkInterface iface = ifaces.at(i);
if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) && iface.flags().testFlag(QNetworkInterface::CanMulticast)) {
for (int j = 0; j < iface.addressEntries().count(); j++) {
if (iface.addressEntries().at(j).ip().protocol() != QAbstractSocket::IPv4Protocol) continue;
qDebug() << "Ocean bind to iface: " << iface.name() << endl << "IP: " << iface.addressEntries().at(j).ip().toString() << endl;
QUdpSocket* socket = new QUdpSocket(this);
socket->bind(QHostAddress::AnyIPv4, 8047, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
socket->joinMulticastGroup(groupAddress, iface);
connect(socket, SIGNAL(readyRead()), this, SLOT(processIncomingWaves()));
boundAddresses.append(iface.addressEntries().at(j).ip());
multicastSockets.append(socket);
}
}
}
sendSocket = new QUdpSocket(this);
sendSocket->bind();
}
示例3: hasInternetConnection
/*
* Checks if we have internet access!
*/
bool UnixCommand::hasInternetConnection()
{
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
bool result = false;
for (int i = 0; i < ifaces.count(); i++){
QNetworkInterface iface = ifaces.at(i);
if ( iface.flags().testFlag(QNetworkInterface::IsUp)
&& !iface.flags().testFlag(QNetworkInterface::IsLoopBack) ){
for (int j=0; j<iface.addressEntries().count(); j++){
/*
We have an interface that is up, and has an ip address
therefore the link is present.
We will only enable this check on first positive,
all later results are incorrect
*/
if (result == false)
result = true;
}
}
}
//It seems to be alright, but let's make a ping to see the result
/*if (result == true)
{
result = UnixCommand::doInternetPingTest();
}*/
return result;
}
示例4: getDeviceIP
QString DeviceManager::getDeviceIP()
{
QNetworkInterface *qni;
qni = new QNetworkInterface();
*qni = qni->interfaceFromName(QString("%1").arg("wlan0"));
return qni->addressEntries().at(0).ip().toString();
}
示例5: getNetworkAddressEntry
/**
* @brief wavrNetwork::getNetworkAddressEntry
* @param pAddressEntry Gets the address entries from network interface and fills in this variable.
* @return Returns true opon success.
* Gets the address entries from network interface whether IPV4 or IPV6 and fills the data in pAddressEntry.
*/
bool wavrNetwork::getNetworkAddressEntry(QNetworkAddressEntry* pAddressEntry) {
// get the first active network interface
QNetworkInterface networkInterface;
if(getNetworkInterface(&networkInterface)) {
wavrTrace::write("Querying IP address from network interface...");
// get a list of all associated ip addresses of the interface
QList<QNetworkAddressEntry> addressEntries = networkInterface.addressEntries();
// return the first address which is an ipv4 address
for(int index = 0; index < addressEntries.count(); index++) {
if(addressEntries[index].ip().protocol() == QAbstractSocket::IPv4Protocol) {
*pAddressEntry = addressEntries[index];
this->networkInterface = networkInterface;
this->interfaceName = networkInterface.name();
wavrTrace::write("IPv4 address found for network interface.");
return true;
}
}
// if ipv4 address is not present, check for ipv6 address
for(int index = 0; index < addressEntries.count(); index++) {
if(addressEntries[index].ip().protocol() == QAbstractSocket::IPv6Protocol) {
*pAddressEntry = addressEntries[index];
this->networkInterface = networkInterface;
this->interfaceName = networkInterface.name();
wavrTrace::write("IPv6 address found for network interface.");
return true;
}
}
wavrTrace::write("Warning: No IP address found for network interface.");
}
return false;
}
示例6: settings
bool
Utilities::isOnline(QNetworkAccessManager *manager)
{
bool ret = false;
QSettings settings("Entomologist");
if (settings.value("work-offline", false).toBool())
return(false);
#if QT_VERSION >= 0x040700
if (manager->networkAccessible() != QNetworkAccessManager::NotAccessible)
ret = true;
else
ret = false;
#else
QNetworkInterface iface;
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
for (int i = 0; i < ifaces.count(); ++i)
{
iface = ifaces.at(i);
if (iface.flags().testFlag(QNetworkInterface::IsUp)
&& !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
{
if (iface.addressEntries().count() >= 1)
{
ret = true;
break;
}
}
}
#endif
return(ret);
}
示例7: isConnectedToNetwork2
bool isConnectedToNetwork2()
{
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
bool result = false;
for (int i = 0; i < ifaces.count(); i++)
{
QNetworkInterface iface = ifaces.at(i);
if ( iface.flags().testFlag(QNetworkInterface::IsUp)
&& !iface.flags().testFlag(QNetworkInterface::IsLoopBack) )
{
// this loop is important
for (int j=0; j<iface.addressEntries().count(); j++)
{
// we have an interface that is up, and has an ip address
// therefore the link is present
// we will only enable this check on first positive,
// all later results are incorrect
if (result == false)
result = true;
}
}
}
return result;
}
示例8: interfaceBoxIndexChanged
//someone selected another interface
void ApplicationWindow::interfaceBoxIndexChanged(int boxIndex)
{
//get the right index data
int interfaceIndex=interfaceBox->itemData(boxIndex).toInt();
QNetworkInterface tmpInterface = QNetworkInterface::interfaceFromIndex (interfaceIndex);
macLine->setText(tmpInterface.hardwareAddress());
// indexLine->setText(QString("%1").arg(interfaceIndex));
QList<QNetworkAddressEntry> interfaceAdresses;
interfaceAdresses=tmpInterface.addressEntries();
if (interfaceAdresses.size()>0)
{
ipLine->setText(interfaceAdresses[0].ip().toString());
maskLine->setText(interfaceAdresses[0].netmask().toString());
}
else
{
ipLine->setText("");
maskLine->setText("");
}
writeLog(createHeaderText());
}
示例9: nativeSetMulticastInterface
bool QNativeSocketEnginePrivate::nativeSetMulticastInterface(const QNetworkInterface &iface)
{
#ifndef QT_NO_IPV6
if (socketProtocol == QAbstractSocket::IPv6Protocol) {
uint v = iface.isValid() ? iface.index() : 0;
return (::setsockopt(socketDescriptor, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *) &v, sizeof(v)) != -1);
}
#endif
struct in_addr v;
if (iface.isValid()) {
QList<QNetworkAddressEntry> entries = iface.addressEntries();
for (int i = 0; i < entries.count(); ++i) {
const QNetworkAddressEntry &entry = entries.at(i);
const QHostAddress &ip = entry.ip();
if (ip.protocol() == QAbstractSocket::IPv4Protocol) {
v.s_addr = htonl(ip.toIPv4Address());
int r = ::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, (char *) &v, sizeof(v));
if (r != -1)
return true;
}
}
return false;
}
v.s_addr = INADDR_ANY;
return (::setsockopt(socketDescriptor, IPPROTO_IP, IP_MULTICAST_IF, (char *) &v, sizeof(v)) != -1);
}
示例10: getHostAddress
// ----------------------------------------------------------------------------
// getHostAddress (static)
// ----------------------------------------------------------------------------
QHostAddress NetworkTools::getHostAddress(const QNetworkInterface &in_interface)
{
if (in_interface.isValid() == false) return QHostAddress();
QList<QNetworkAddressEntry> addr_entry_list = in_interface.addressEntries();
if (addr_entry_list.isEmpty()) return QHostAddress();
return addr_entry_list.first().ip();
}
示例11: sendMessage
bool OceanSocket::sendMessage(const char message_type, QString data)
{
QByteArray pulse = QString(message_type+data).toUtf8();
QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
for (int i = 0; i < ifaces.count(); i++) {
QNetworkInterface iface = ifaces.at(i);
if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack) && iface.flags().testFlag(QNetworkInterface::CanMulticast)) {
bool isIp4 = false;
for (int j = 0; j < iface.addressEntries().count(); j++) {
if (iface.addressEntries().at(j).ip().scopeId() == "")
isIp4 = true;
}
if (isIp4) {
sendSocket->setMulticastInterface(iface);
sendSocket->writeDatagram(pulse.data(), pulse.size(), groupAddress, 8047);
}
}
}
return true;
}
示例12: item_text
// Set up bind address combobox
foreach (const QNetworkInterface &netif, QNetworkInterface::allInterfaces())
{
const QString &hname = netif.name();
foreach (const QNetworkAddressEntry &netaddr, netif.addressEntries())
{
const QHostAddress &addr = netaddr.ip();
if (addr.protocol() == QAbstractSocket::IPv4Protocol)
{
QString item_text(addr.toString() + " (" + hname + ")");
settings_dialog.bind_address->addItem(item_text, addr.toString());
}
}
}
示例13: scan
void PJobRunnerNetworkScanner::scan(const QNetworkInterface& interface){
foreach(const QNetworkAddressEntry& address_entry, interface.addressEntries()){
if(m_want_stop) break;
quint32 netmask = address_entry.netmask().toIPv4Address();
quint32 local_ip = address_entry.ip().toIPv4Address();
quint32 address_to_try = (local_ip & netmask) + 1;
//quint32 inv_netmask = ~netmask;
if(interface.humanReadableName() == "lo"){
std::cout << "Skipping interface \"lo\"." << std::endl;
continue;
}
if(!interface.isValid()){
std::cout << "Skipping interface \"" << interface.humanReadableName().toStdString() << "\"." << std::endl;
continue;
}
if(address_entry.ip().protocol() != QAbstractSocket::IPv4Protocol){
std::cout << "Skipping non-IPv4 interface." << std::endl;
continue;
}
if(address_entry.ip() == QHostAddress::LocalHost){
std::cout << "Skipping loopback interface." << std::endl;
continue;
}
if(address_entry.ip().isNull()){
continue;
}
std::cout << "Probing interface " << interface.humanReadableName().toStdString() << std::endl;
std::cout << "Local address is " << interface.hardwareAddress().toStdString() << std::endl;
std::cout << "Netmask: " << address_entry.netmask().toString().toStdString() << std::endl;
std::cout << "Searching local network..." << std::endl;
while((address_to_try & netmask) == (local_ip & netmask)){
if(m_want_stop) break;
std::cout << "\r" << QHostAddress(address_to_try).toString().toStdString();
std::cout.flush();
emit probing_host(QHostAddress(address_to_try));
PJobRunnerSessionWrapper* session = new PJobRunnerSessionWrapper(QHostAddress(address_to_try), 50);
if(session->is_valid()) found(session);
else delete session;
address_to_try++;
}
}
emit finished_scanning();
m_want_stop = false;
}
示例14: joinMulticastGroup
bool MulticastSocket::joinMulticastGroup(const QHostAddress& groupAddress, const QNetworkInterface& iface)
{
bool success = false;
foreach (const QNetworkAddressEntry& addressEntry, iface.addressEntries()) {
const QHostAddress interfaceAddress = addressEntry.ip();
if (interfaceAddress.protocol() == IPv4Protocol) {
ip_mreq mreq;
mreq.imr_multiaddr.s_addr = htonl(groupAddress.toIPv4Address());
mreq.imr_interface.s_addr = htonl(interfaceAddress.toIPv4Address());
const bool error = setsockopt(socketDescriptor(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*) &mreq, sizeof(mreq)) == -1;
if (error)
setErrorString(strerror(errno));
else
success = true;
}
}
return success;
}
示例15: updateInterfaceAddressCombo
void AdvancedSettings::updateInterfaceAddressCombo()
{
// Try to get the currently selected interface name
const QString ifaceName = comboBoxInterface.itemData(comboBoxInterface.currentIndex()).toString(); // Empty string for the first element
const QString currentAddress = BitTorrent::Session::instance()->networkInterfaceAddress();
// Clear all items and reinsert them, default to all
comboBoxInterfaceAddress.clear();
comboBoxInterfaceAddress.addItem(tr("All addresses"));
comboBoxInterfaceAddress.setCurrentIndex(0);
auto populateCombo = [this, ¤tAddress](const QString &ip, const QAbstractSocket::NetworkLayerProtocol &protocol)
{
Q_ASSERT((protocol == QAbstractSocket::IPv4Protocol) || (protocol == QAbstractSocket::IPv6Protocol));
// Only take ipv4 for now?
if ((protocol != QAbstractSocket::IPv4Protocol) && (protocol != QAbstractSocket::IPv6Protocol))
return;
comboBoxInterfaceAddress.addItem(ip);
//Try to select the last added one
if (ip == currentAddress)
comboBoxInterfaceAddress.setCurrentIndex(comboBoxInterfaceAddress.count() - 1);
};
if (ifaceName.isEmpty()) {
for (const QHostAddress &ip : asConst(QNetworkInterface::allAddresses()))
populateCombo(ip.toString(), ip.protocol());
}
else {
const QNetworkInterface iface = QNetworkInterface::interfaceFromName(ifaceName);
const QList<QNetworkAddressEntry> addresses = iface.addressEntries();
for (const QNetworkAddressEntry &entry : addresses) {
const QHostAddress ip = entry.ip();
populateCombo(ip.toString(), ip.protocol());
}
}
}