qt4 qtcpsocket read 阻塞不返回

zhenimei100 2017-09-21 10:30:28
这是我的qtcpsocket的read过程:
if(this->qTcpSocket->state() == QAbstractSocket::ConnectedState&&(this->qTcpSocket->isReadable())&&(this->qTcpSocket->bytesAvailable()>5))
{
readlen=qTcpSocket->read(buf,2048);
if(readlen<0)
{
qDebug()<<"err:readlen="<<readlen;
return;
}
}
然后监视到,有时候程序卡在 readlen=qTcpSocket->read(buf,2048);这里,不返回了。
其他线程还在跑。。。。。。
...全文
1314 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangzai6378 2017-11-25
  • 打赏
  • 举报
回复
不可能阻塞,QIODevice类的read函数没有数据时返回0;QTcpSocket是QIODevice的子类。请看官方文档 qint64 QIODevice::read(char *data, qint64 maxSize) Reads at most maxSize bytes from the device into data, and returns the number of bytes read. If an error occurs, such as when attempting to read from a device opened in WriteOnly mode, this function returns -1. 0 is returned when no more data is available for reading. However, reading past the end of the stream is considered an error, so this function returns -1 in those cases (that is, reading on a closed socket or after a process has died).

16,173

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧