我有一个从QTcpServer继承的Server类。我已连接slot和s信号,但QTcpServer :: nextPendingConnection()和Server :: nextPendingConnection()返回NULL。我该怎么办? 这是服务器的实现
Server::Server(QObject *parent) : QTcpServer(parent)
{
tcpSocket = new QTcpSocket();
start(QHostAddress::Any, quint16(port));
}
void Server::start(const QHostAddress &address, quint16 port)
{
connect(this, &Server::newConnection, this, &Server::slotNewConnection);
if (!this->listen(address, port))
{
....
}
else
{
....
}
}
void Server::slotNewConnection()
{
QTcpSocket *client = this->nextPendingConnection(); //RETURNS NULL
clients.insert(client);
client->write("Hello");
connect(client, &QTcpSocket::readyRead, this, &Server::slotServerRead);
connect(client, &QTcpSocket::disconnected, this, &Server::slotClientDisconnected);
}
Server.h文件:
class Server : public QTcpServer
{
Q_OBJECT
public:
explicit Server(QObject *parent = 0);
void start(const QHostAddress &address, quint16 port);
void sendMessageToGroup();
public slots:
void slotNewConnection();
void slotServerRead();
void slotClientDisconnected();
private:
QSet<QTcpSocket*> clients;
};