Qt 4.8 QTcpServer strange behaviour.

  • Hi,
    I have a following code:

    QTcpSocket client;
    QTcpServer server;
    qDebug() << server.listen( QHostAddress::Any  //true
                             , 36235);
    usleep( 1000000);
    client.connectToHost( QHostAddress::LocalHost
                                           , 36235);
    qDebug() << client.error(); //QAbstractSocket::UnknownSocketError
    usleep( 1000000);
    QTcpSocket *t = server.nextPendingConnection();
    client.waitForConnected(-1); //wait forever!
    qDebug() << "client.state()" << client.state();
    qDebug() << client.write( QByteArray( "12345"));
    qDebug() << "t.state(): " << t->state();
    qDebug() << t->bytesAvailable();
    qDebug() << t->read( 5);

    So, it gives me:

    client.state() QAbstractSocket::ConnectedState

    And after that I got segfault because t is 0x00.

    I don't want to use Qt Signals/Slots system, so all the connect magic is not for me.

    āžœ ss -at
    State       Recv-Q Send-Q          Local Address:Port                           Peer Address:Port                
    LISTEN      0      0                           *:36235                                     *:*                    
    LISTEN      0      0                           *:ssh                                       *:*                    
    ESTAB       0      0                                    
    ESTAB       0      0                                   
    CLOSE-WAIT  38     0                                   
    CLOSE-WAIT  147    0                                       
    telnet gave me:
    āžœ  telnet localhost 36235
    Trying ::1...
    Connection failed: Connection refused
    Connected to localhost.
    Escape character is '^]'.

    What i'm doing wrong?
    What this UnknownSocketError means?
    What should I do?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Then you should use waitForConnected to ensure the connection was established.

    Out of curiosity, why not use Qt's signals and slots mechanism ?

  • @SGaist Hi. Thanks for answer!

    Using of waitForNewConnection makes no difference.

    I have quite a complicated code which I don't want to complicate more with async logic.

  • Lifetime Qt Champion

    Can you explain what you are trying to achieve ? Currently it looks like you are trying to setup a server in your application and connect to the said server from the same application.

Log in to reply

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