Qt TcpServer架构
问题是关于 Qt 服务器的架构。 例如,当套接字发出 readyRead()
信号时,会调用一个插槽 ReadyRead()
如果有多种请求,我们可以
void Server::ReadyRead()
{
QString msg = readFirstWordFromAvaiableData();
switch (msg){
case "PING":
case "GET": // and so on
}
}
我想知道是否还有其他方法可以做到这一点。我想这不具有可扩展性和舒适性。
如果我希望服务器与用 Java、C、Perl 等编写的客户端一起工作,我应该只使用 QByteArray,对吧?在 QtDemo 的所有示例中,客户端在消息之前发送消息的大小。有必要吗?
如果您建议我对一些用 Qt 编写的服务器进行采样(在 github、bitbucket 等上),那就太好了。我想看看生产服务器是如何安排的。
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 = readFirstWordFromAvaiableData();
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的每个命令都有固定长度的有效负载,则可能不需要发送消息长度。
QByteArray 是 unsigned char 数组的一个方便的包装器,并且有一些有用的实用方法,所以它是一个很好用的东西。如果您的协议是基于文本的,您也可以使用 QString,但请注意,默认情况下 QString 是 Unicode,因此一个字符需要 2 个字节而不是 1 个字节。您可以使用适当的转换函数(QString::fromUtf8、QString::fromAscii 等)。
我首先建议您查看Qt 文档中的示例,如果您还没有这样做。但没有任何其他例子可以推荐。
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.