QTcpSocket with static local port: extremely quick SocketTimeoutError



  • Hi

    I need to connect to some host several times, using the same local port. I use bind() to make static port for tcp socket.
    First time it binds and connects to host successfully, but all subsequent connects finished with extremely quick (several ms) SocketTimeoutError, although I use waitForConnected() method.

    What I'm doing wrong? Thanks in advance.

    Test::Test(QObject *parent) : QObject(parent)
    {
        QTcpSocket *sock = new QTcpSocket ();
        connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)));
        connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
    
        for (int i=0; i<3; i++)
        {
            qDebug() << "before abort" << sock->localPort();
            sock->abort();
            sock->close();
            qDebug() << "after abort" << sock->localPort();
            qDebug() << "bind 3310" << sock->bind(3310);
    
            qDebug() << i << QTime::currentTime().toString("HH:mm:ss:zzz") << "CONNECT TO HOST" << sock->localPort();
            sock->connectToHost(QString("192.168.1.6"), 90);
    
            if (sock->waitForConnected(10000))
                qDebug() << QTime::currentTime().toString("HH:mm:ss:zzz") << i << "CONNECTED" << sock->localPort();
            else
                qDebug() << QTime::currentTime().toString("HH:mm:ss:zzz") << i << "NOT connected";
        }
    
    }
    
    
    void Test::stateChanged(QAbstractSocket::SocketState state)
    {
        QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
        qDebug() << QTime::currentTime().toString("HH:mm:ss:zzz") << "stateChanged" << state << socket->localPort();
    }
    
    
    void Test::error(QAbstractSocket::SocketError error)
    {
        QTcpSocket *socket = static_cast<QTcpSocket*>(sender());
        qDebug() << QTime::currentTime().toString("HH:mm:ss:zzz") << "error" << error << socket->localPort();
    }
    
    

    Application output:

    before abort 0
    after abort 0
    "19:47:41:990" stateChanged QAbstractSocket::BoundState 3310
    bind 3310 true
    0 "19:47:41:997" CONNECT TO HOST 3310
    "19:47:41:998" stateChanged QAbstractSocket::HostLookupState 3310
    "19:47:42:001" stateChanged QAbstractSocket::ConnectingState 3310
    "19:47:42:006" stateChanged QAbstractSocket::ConnectedState 3310
    "19:47:42:006" 0 CONNECTED 3310
    before abort 3310
    "19:47:42:510" stateChanged QAbstractSocket::UnconnectedState 3310
    after abort 0
    "19:47:42:518" stateChanged QAbstractSocket::BoundState 3310
    bind 3310 true
    1 "19:47:42:523" CONNECT TO HOST 3310
    "19:47:42:525" stateChanged QAbstractSocket::HostLookupState 3310
    "19:47:42:529" stateChanged QAbstractSocket::ConnectingState 3310
    "19:47:42:534" stateChanged QAbstractSocket::UnconnectedState 3310
    "19:47:42:535" error QAbstractSocket::SocketTimeoutError 3310
    "19:47:42:540" stateChanged QAbstractSocket::UnconnectedState 3310
    "19:47:42:545" 1 NOT connected
    before abort 3310
    after abort 0
    "19:47:43:051" stateChanged QAbstractSocket::BoundState 3310
    bind 3310 true
    2 "19:47:43:059" CONNECT TO HOST 3310
    "19:47:43:065" stateChanged QAbstractSocket::HostLookupState 3310
    "19:47:43:067" stateChanged QAbstractSocket::ConnectingState 3310
    "19:47:43:072" stateChanged QAbstractSocket::UnconnectedState 3310
    "19:47:43:073" error QAbstractSocket::SocketTimeoutError 3310
    "19:47:43:080" stateChanged QAbstractSocket::UnconnectedState 3310
    "19:47:43:082" 2 NOT connected


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.