ljq1000 |
2017-05-10 20:32 |
QTcpSocket和QSocketNotifier
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); ... }
void TcpConnection::handleRead(int /*socket*/) { char sbuf[1024]; qint64 n = _socket->read(sbuf, 1024); // n always zero, why??? ... } |
|