Reputation: 2520
What happens when QTcpSocket::close()
is called?
I know it ultimately closes the connection, but what happens to the Tcp socket's internal buffer? Is it cleared?
Is this why QTcpSocket::size()
returns 0 when I call close()
?
From the client after receiving data from server:
qDebug() << tcpSocket.size(); //170
tcpSocket.close();
qDebug () << tcpSocket.size(); //0
Edit: According to this post, closing a socket automatically flushes any remaining data. Since I close the socket after the client has received data from the server, where does the data get flushed to? I haven't read any of the data yet.
Upvotes: 3
Views: 6443
Reputation: 2239
When close is called, Qt changes the QAbstractSocket
state to ClosingState
.
If there is data that has not been written to the socket, it is written now. Once that finishes, the socket is completely closed (I'm considering only the Qt "view" of the socket, not its implementation and the control states like TIME_WAIT).
Looking at the source of QAbstractSocket
class here, and following to the QNativeSocketEngine
class, looks like Qt do not store a read buffer for the socket, completely relying on the operating system for that.
So, once the socket is closed, the read buffer no longer exists and all non-read data is lost.
Conclusion: before closing the socket, be sure to read its data until bytesAvaiable
reaches 0
(or some imposed restriction).
Upvotes: 3