Qt 4.8 QTcpServer strange behaviour.
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 184.108.40.206:http ESTAB 0 0 192.168.10.96:45112 220.127.116.11:https CLOSE-WAIT 38 0 192.168.10.96:60620 18.104.22.168:https CLOSE-WAIT 147 0 192.168.10.96:35470 22.214.171.124: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?
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.
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.