I'm moving my application from Qt 4.7 to Qt 6.3. In Qt 4.7 all works fine. In Qt 6.3 I have some issues when tcp server closes connection, I establish again connection, and I try to write data.
This is the function I use to write to socket:
void NetworkFacility::Write(QTcpSocket& oTCPSocket, const QVariant& oV)
{
//Controls Socket is valid
if (oTCPSocket.state() == QAbstractSocket::ConnectedState)
{
QByteArray block; //ByteArray to serialiaze object
MyPacket oPacket; //Packet to send
//Set Size of QVariant object
oPacket.setPacketSize(getQVariantSize(oV));
//Set QVariant
oPacket.setTransport(oV);
//Create stream to write on ByteArray
QDataStream out(&block, QIODevice::WriteOnly);
//Sets version
out.setVersion(QDataStream::Qt_4_7);
//Serializes
out << oPacket;
//TX to socket
oTCPSocket.write(block);
}
}
I manage disconnection this way:
void MyClient::remoteNodeDisconnected()
{
m_pTCPSocket->flush();
m_pTCPSocket->close();
}
void MyClient::ManagesTCPError(QAbstractSocket::SocketError socketError)
{
//Clears last packets
m_pTCPSocket->flush();
}
This is connection portion of code after disconnection:
m_pTCPSocket->connectToHost(m_sIpAddress, m_iIpPort);
//Waits connection
if (m_pTCPSocket->waitForConnected(MSEC_WAIT_FOR_CONNECTION))
{
//Print connected and exit from while loop
break;
}
Finally this is the way in which I manage the remote server connecte:
void MyClient::remoteNodeConnected()
{
//Improve Network latency on this connection
m_pTCPSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
}
The issue is that on the first connection all works fine. If the server disconnects (i.e. I umplugg the server cable in my LAN or I shutdown and restarts the server application) and then connects again the call to:
oTCPSocket.write(block);
in Networkfacility::Write method generates a crash.
Why the write method generates a crash after reconnection?
I fixed the issue by removing completely th oTCPSocket instance.