[Solved] TcpSocket timeout HELP
Hope this is the correct place to post ?
Can anyone please give me any pointers as to why i keep getting time out errors from a tcpsocket ?
According to a packet sniffer the data is being sent well within the timeslot but my code times out about 90% of the time !
Function is pasted below
The command gets sent ok
The data gets sent ok
The reply packet length gets received ok
The reply packet times out 90% of the time !
Even though according to network sniffing the data HAS BEEN sent from the server ??
This has been bugging me for days.
Any help much appreciated.
@// Send a complex command with data attached
void CtcpClient::slotCommand(const QString commandStr,const QString dataStr )
// Output stream
// Send command
if (commandStr.length ()<4)
emit sigError (TCPERR_INVALID_COMMAND,"Invalid command");
outText << commandStr.left (4);
// Send data length
if (dataStr.length ()<1)
emit sigError (TCPERR_INVALID_DATA,"Invalid data");
QString dataLen = QString::number (dataStr.size ()).rightJustified (4,'0');
outText << dataLen;
// Send data
outText << dataStr;
// Wait untill we have 4 bytes of data ( packet length )
while (socket->bytesAvailable ()< 4)
emit sigError(socket->error(), socket->errorString());
// Then read packet length
QString packetLen = inLength.read (4);
// Wait untill we have packetlength of data
while (socket->bytesAvailable ()< packetLen.toInt ())
emit sigError(socket->error(), socket->errorString()); // +++++++ KEEPS EXITING HERE +++++++++
// Then read data
QString data = inData.read (packetLen.toInt ());
// Then emit sigReply(packet)
emit sigReply (data);
You need to return to the event loop or call waitForBytesWritten on the socket.
Yes. If possible at all, I would recommend you use the first approach peppe suggests: return to the eventloop.
That means, that you split up your code in sections, and use QTcpSocket's signals and slots to trigger actions. Avoid the waitFor* functions if at all possible.
Many thanks peppe & Andre.
Between the two of you my problem is solved !
Had to do a fairly major rewrite of both server and client so replies could be associated with the relevant sent command, and as suggested moved receiving on the client to a separate slot connected to TcpSocket.ReadyRead() signal.
This solved all time-out issues.
Again many thanks for the help it was much appreciated ( and needed ).
Thank you for reporting back with your solution!