如何让QTcpSocket等待接收数据 [英] How to make QTcpSocket wait for receiving data

查看:1610
本文介绍了如何让QTcpSocket等待接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个小项目中使用Qt

我的程序是一个客户端,连接到服务器。

我使用QTcpSocket。

I am working in a small project using Qt
My program is a client, connected to server.
I use QTcpSocket.

connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(handleData()));



客户端在两种情况下从服务器接收数据:

+)案例1:服务器始终发送其状态

客户端收到并仅显示它们。

+)案例2:当客户想要获取某些信息时,客户端将发送命令。

服务器将解析命令并返回正确的信息。

我希望客户端必须等待在TIMEOUT秒内接收数据。



因为我使用SIGNAL readyRead(),所以数据将在另一个线程中接收。 br />
我不能等待案件2中从服务器接收数据。



有人能建议我一些理想吗?

谢谢!


Client receives data from server in 2 cases :
+) Case 1: Server always send its status
Client receives and only show them.
+) Case 2: When client wants to get some information, client will send a command.
Server will parse the command and return proper informations.
I want that client must wait for receiving data in TIMEOUT seconds.

Because I am using SIGNAL readyRead(), data will be received in another thread.
I can't wait for receiving data from server in case 2.

Can anyone suggest me some ideals ?
Thanks !

推荐答案

你只需保存客户发送的最后一个请求(案例1或2),以及何时数据从服务器返回,处理它并将其发送到下一级。
you will just have to save what was the last request the client sent (case 1 or 2), and when the data comes back from the server, process it and signal it to the next level.


当读取和连接字节时,您可以发出自定义信号。
You can emit custom signal when bytes are read and is being connected.

这篇关于如何让QTcpSocket等待接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆