Overhead is not relevant here. If you handle a connection one second later, it won't compromise anything. Not reacting to bogus or unexpected data will.
If you create a QDataStream in a slot connected to readyRead() then I can immediately tell you your code is incorrect without even reading it.Not getting data in is also on the list since the solution will have a mobile component to it and those devices can go off-grid any time. I figured that something along the lines of
Qt Code:
void Server::onReadyRead() { if(!client) return; int blockSize = 0 ; QString incomingData; if (blockSize == 0) { if (client->bytesAvailable() < (int)sizeof(quint16)) return; in >> blockSize; } if (client->bytesAvailable() < blockSize) return; in >> incomingData; doStuff(incomingData); }To copy to clipboard, switch view to plain text mode
should do the job. On top of that is a timer that let's each connection time out after a set period of time. (The client pings the server to confirm its status)
Thanks for the advice, it's really helpful to get a different perspective on things.






Reply With Quote
. The above code in post #6 is what's connected to the readyRead() signal. Only 2 differences are a) The QDataStream now gets created once per socket when I'm working through the TcpServer::hasPendingConnections() list and b) I limit the data being received since I know all the strings and their length that are permitted to be submitted (socket->bytesAvailable()). I just had for testing purposes a waitForReadyRead() in there to see how it behaves with large or no data coming in.

Bookmarks