Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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:

    true
    QAbstractSocket::UnknownSocketError
    client.state() QAbstractSocket::ConnectedState
    5
    

    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               192.168.10.96:33360                        151.101.129.69:http                 
    ESTAB       0      0               192.168.10.96:45112                        74.125.232.230:https                
    CLOSE-WAIT  38     0               192.168.10.96:60620                        107.20.174.100:https                
    CLOSE-WAIT  147    0               192.168.10.96:35470                            192.0.73.2:https                
    
    telnet gave me:
    
    āžœ  telnet localhost 36235
    
    Trying ::1...
    Connection failed: Connection refused
    Trying 127.0.0.1...
    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