Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt 4.8 QTcpServer strange behaviour.

Qt 4.8 QTcpServer strange behaviour.

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    flomop
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply
      1
      • SGaistS SGaist

        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 ?

        F Offline
        F Offline
        flomop
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved