QTcpSocket和QSocketNotifier

ljq1000 2017-05-10 08:48:26
QTcpSocket和QSocketNotifier配合使用出现问题,表现为能接收到QSocketNotifier::activated(int)信号,但是从QTcpSocket中读数据时始终返回0。
代码如下:
// class TcpServer : public QTcpServer
void TcpServer::incomingConnection(qintptr socketDescriptor)
{
QString connName = ...
TcpConnectionPtr conn(new TcpConnection(connName, socketDescriptor));
......
}


TcpConnection::TcpConnection(const QString &name, qintptr sockfd)
: _name(name),
_socket(new QTcpSocket(this)),
_readNotifier(new QSocketNotifier(sockfd, QSocketNotifier::Read, this)),
_writeNotifier(new QSocketNotifier(sockfd, QSocketNotifier::Write, this)),
...
{
_socket->setSocketDescriptor(sockfd);
_socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
_readNotifier->setEnabled(true);
_writeNotifier->setEnabled(false);

connect(_readNotifier, &QSocketNotifier::activated,
this, &TcpConnection::handleRead,
Qt::DirectConnection);
connect(_writeNotifier, &QSocketNotifier::activated,
this, &TcpConnection::handleWrite,
Qt::DirectConnection);
...
}

毫无头绪,请大家帮忙!
...全文
538 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
erdoor 2019-07-05
  • 打赏
  • 举报
回复
@ljq1000 关于海图有问题请教,能留个联系方式吗?我的Q-Q 122-635-224
弓人水 2019-07-05
  • 打赏
  • 举报
回复
看下Qt帮助文档(QSocketNotifier ):
Finally, you can use the socket() function to retrieve the socket identifier. Although the class is called QSocketNotifier, it is normally used for other types of devices than sockets. QTcpSocket and QUdpSocket provide notification through signals, so there is normally no need to use a QSocketNotifier on them.

QTcpSocket 和 QUdpSocket 自身就有readyRead()和bytesWritten(qint64 bytes)信号。
ljq1000 2017-05-11
  • 打赏
  • 举报
回复
去掉_readNotifier,连接TCPSocket::readReady信号就可以了

16,215

社区成员

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

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