c++qtqtcpsocketqt5.9

QTcpSocket.connectToHost() takes too long time


This is my code for connection with custom TcpServer. In result connectiontoHost() is complitted in 13.5 seconds (some times in 2.5 seconds). Connection to localserver is very fast. The same code with the remote server, but on Qt5.7 works very quickly.

bool cuTcpSocketIOInterface::pInitialize()
{
    QTcpSocket* tSocket = new QTcpSocket(this);
    QElapsedTimer timer;
    timer.start();
    if (!isSocketReady()){
       qDebug()<<"connectToHost" <<address()<<port()<<", Time:"
               <<timer.elapsed();
       tSocket->connectToHost(address(), port());
    }
    qDebug()<<"Done:"<<timer.elapsed();
    tSocket->waitForConnected(100);
    return tSocket->state() == QAbstractSocket::ConnectedState;
}

And next is a output of this function in std::out with remote server:

connectToHost QHostAddress("192.168.255.193") 9876 , Time: 0
Done: 13581

Connection with custom localserver

connectToHost QHostAddress("127.0.0.1") 9876 , Time: 0
Done: 5

Ping to my custom remote server is very well (less than 1 ms). Connection with putty is very quickly.

Update So, I try work with Qt5.7 and Qt 5.9.1. And I obtain next results: My code:

#include <QCoreApplication>
#include <QDebug>
#include <QTcpSocket>
#include <QElapsedTimer>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug()<<"START TEST!!!";
    QElapsedTimer timer;
    timer.start();

    QTcpSocket socket;
    socket.connectToHost(TcpIpAddress,80);
    qDebug()<<"connectToHost Done:"<<timer.elapsed();

    return a.exec();
}

On Qt 5.7.1:

START TEST!!!
connectionToHost Done:7

On Qt 5.9:

START TEST!!!
connectionToHost Done:2572

Looks it like an excuse for BugReport?

Update: on Qt 5.10.1

START TEST!!!
connectionToHost Done:2574

Solution

  • Solution: Need to setting up proxy before connecting to server: socket.setProxy(QNetworkProxy::NoProxy)