Question

When using Qt's QNetworkAccessManager to do HTTP GET from a service that's on an IPv4 port, but the same computer also has IPv6 functionality (which our service does not use), there is a 2+ second delay before the response is processed. It's as though Qt is attempting to use the IPv6 address first, then times out, then attempts IPv4, where it is successful.

I would like to 'inform' Qt to just use IPv4, to avoid this delay.

I am using Qt 4.8 and using Qt 5 isn't an option at the moment.

Is there a way to 'tell' Qt to just use IPv4? Or avoid this delay some other way?

bool float::doRequest(QString* response, const QString& serverfunc, HttpPostData& postdata, int timeout) {
    *response="";
    bool ret=false;

    QString url = "http://" + _host + ":" + QString::number(_port) + "/license.txt";

    postdata["page"] = serverfunc;
    postdata["seed"] = stripChars(QUuid::createUuid().toString());

    QNetworkAccessManager manager;
    QEventLoop q;
    QTimer tT;

    tT.setSingleShot(true);
    connect(&tT, SIGNAL(timeout()), &q, SLOT(quit()));
    connect(&manager, SIGNAL(finished(QNetworkReply*)), &q, SLOT(quit()));

    QString postdata_string = mapToPostData(postdata);
    QString testurl = url + "?" + postdata_string;
    QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(testurl)));

    printf("Starting request. ");

    tT.start(timeout * 1000);
    q.exec();

    printf("Done!"); // 2+ seconds pass between "Starting request." and here when IPv6 is available on server

    if (tT.isActive()) {
        QByteArray data = reply->readAll();
        QTextStream out(&data);
        *response = out.readAll();
        tT.stop();
        ret=true;
    } else {
        // timeout
    }
    reply->close();
    q.quit();

    printf("Done.\n");

    // Other method
    //QString errormsg;
    //ret = SimpleNetworkOp::sendSynchronousPost("Trying...",url,postdata,response,&errormsg);

    delete reply; // clean-up

    return ret;
}
Was it helpful?

Solution

I 'solved' the issue by switching to Chilkat's HTTP library for network requests. It lets you specify IPv4 vs. IPv6 preference very easily.

I realize this isn't a correct 'answer' to my own question, since I was asking about Qt, but it's the solution I found easiest, and it works great. This has been in use for over a year now and no problems at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top