Qt TcpServer architecture
The question is about the architecture of a server with Qt.
For example, there is a slot ReadyRead()
being called when a socket signals readyRead()
If there are several kind of requests we can
void Server::ReadyRead()
{
QString msg = readFirstWordFr开发者_JAVA技巧omAvaiableData();
switch (msg){
case "PING":
case "GET": // and so on
}
}
I wonder is there other way to do this. I guess this isn't expandable and comfortable.
If i want server to work with client written on Java, C, Perl, etc I should use QByteArray only, right? In all samples from QtDemo client sends size of message before the message. Is it necessary?
It would be nice if you suggest me samples some servers written with Qt (on github, bitbucket, etc). I want to look how production servers are arranged.
If each of your commands have a fixed length of the payload, it may not be necessary to send message length.
QByteArray is a convenient wrapper around unsigned char array, and has some useful utility methods, so it's a good thing to use. You can also use QString if your protocol is text-based, though be aware that by default QString is Unicode, so a character takes 2 bytes instead of one. You can use appropriate conversion functions (QString::fromUtf8, QString::fromAscii, etc.).
I would first suggest taking a look at the examples in Qt documentation if you haven't done so. Don't have any other examples to recommend though.
精彩评论