Pregunta

I am using the QNetworkAccessManager to do HTTP requests. We have found that the network connection we are using can go offline occasionally, and I want to actively detect when the link goes down.

I have connected a slot to the QNetworkAccessManager::networkAccessibleChanged() signal, but am not seeing any output from my slot.

In searching for a solution, the closest I come to an answer is the following discussion: http://www.qtcentre.org/threads/37514-use-of-QNetworkAccessManager-networkAccessible

However, the solutions suggested didn't resolve my problem.

Any suggestions on what I may be doing wrong?

¿Fue útil?

Solución

Ok, after some more experimenting, I have found the answer...

Turns out my problem is that I have several Ethernet interfaces on my system.

In the discussion linked from my question, adding the following code was suggested:

QNetworkAccessManager* mNetworkAccessManager = new QNetworkAccessManager();
QNetworkConfigurationManager manager;
mNetworkAccessManager->setConfiguration(manager.defaultConfiguration());

The documentation for QNetworkAccessManager::setConfiguration() indicates the the default configuration is used automatically; so this is unnecessary, but it set me on the right track.

My problem is that the default configuration attaches to a different interface than the one my connection is going through; so I did the following:

QString ifName = "eth2";
QNetworkAccessManager* pNetworkAccessManager = new QNetworkAccessManager();
QNetworkConfigurationManager manager;
foreach(QNetworkConfiguration cfg, manager.allConfigurations()) {
  if (cfg.name() == ifName) {
    pNetworkAccessManager->setConfiguration(cfg);
    break;
  }
}

Now, my slot gets called.

I wish there were an easier way to get the desired configuration. Now, I have to figure out how to get the configuration starting with an IP address, instead of the interface name.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top