A socket connection hangs in ConnectingState on Windows 7
Two Qt environments connected with a TCP-socket to each others, the server side running on Linux and the client running on Windows 7.
The client establishes a socket connection to the server and starts sending messages (commands, queries, etc.) to it. Uses QAbstractSocket->connectToHost() functionality. The server is at that point created as QTcpServer(), the actual server code waits in a slot defined for signal newConnection. When Qt signals on the server side a new connection a TCP-socket is constructed with new QTcpSocket() API and the new connection is set by calling QTcpServer->nextPendingConnection().
On the server side (Linux) the socket state is immediately (by polling or catching the signal in a slot) in ConnectedState. But on the client side (WIN 7) the state stays ConnectingState and does not turn to ConnectedState at all if we do not write something to the socket.
Writing to the socket that is in ConnectingState seems to follow a pattern:
- the first writing - message disappears and the polled client state shows ConnectingState
- the second - fift writings - messages go thru to server but the polled client state still shows ConnectingState
- A signal connected() on the client side comes and triggers the slot connected for this signal
- the sixth - following writings - messages go thru to server and the polled client state shows ConnectedState
(polling done by QTcpSocket->state())
If we put delay between the first writing and the next ones the socket enters to ConnectedState sometime between the first writing and the following ones and shows ConnectedState both by polling & catching the corresponding signal in a slot.
But if we do not send anything the socket seems to hang in ConnectingState forever - at least as long we have patience to wait...
The Qt docs say that one should write to the socket first when it is in ConnectedState and also that prior to that state the operation may be somehow undefined. On the other hand we do not want to miss any messages or pad our code with dummy ones just as a quick and dirty patch.
Any idea why a socket hangs in ConnectingState on WIN 7?