繁体   English   中英

QTcpSocket不发出信号

[英]QTcpSocket does not emit a signal

我在qt中为tcp编写了一个小型服务器,然后移到第二个线程(更容易为我处理)。 服务器的代码是:

#include "server.h"


Server::Server(QString ipAddr, quint32 port)
{
    Server::ipAddr = ipAddr;
    Server::port = port;
    firstTime = true;
    sessionOpened();
    qDebug() << "New server created (from Server)!";

    //connect(Server::tcpServer, SIGNAL(newConnection()), this, SLOT(createConnection()));
    qDebug() << "Connections created!";

}

void Server::showSignals()
{
    exit(0);
    qCritical() << m_pSignalSpy->wait(10000);
//    for(int index = 0; index<m_pSignalSpy->size(); index++)
//    {
//        QList<QVariant> listItem = m_pSignalSpy->value(index);

//        qDebug() << "Signal Arguments: ";
//        for(int index2 = 0;index2 < listItem.size();index2++)
//        {
//            qDebug() << listItem[index2].toString().toStdString()<<" ";
//        }
//    }
}

void Server::sessionOpened()
{
    Server::tcpServer = new QTcpServer(this);
    connect(tcpServer, &QTcpServer::newConnection, this, &Server:: gotAConnection);
    if(!Server::tcpServer->listen(QHostAddress::Any, (quint16)Server::port))
    {
        //QMessageBox::information(this, tr("Server"), tr("Unable to start the server: %1.").arg(Server::tcpServer->errorString()));
        Server::tcpServer->close();
        exit(0);
        return;
    }

    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    // use the first non-localhost IPv4 address
    for (int i = 0; i < ipAddressesList.size(); ++i) {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                ipAddressesList.at(i).toIPv4Address()) {
            Server::ipAddr = ipAddressesList.at(i).toString();
            break;
        }
    }
    // if we did not find one, use IPv4 localhost
    if (Server::ipAddr.isEmpty())
        Server::ipAddr = QHostAddress(QHostAddress::LocalHost).toString();

    qDebug() << Server::ipAddr;
    qDebug() << Server::tcpServer->hasPendingConnections();
//    connect(Server::tcpServer, SIGNAL(newConnection()), this, SLOT(gotAConnection()));
//    Server::m_pSignalSpy = new QSignalSpy(Server::tcpServer, SIGNAL(newConnection()));
//    connect(Server::tcpServer, SIGNAL(newConnection()), this, SLOT(showSignals()));
}

void Server::getInfo()
{
    sendData("Hello");
}

void Server::destroyConnection()
{
    Server::clientConnection->disconnectFromHost();
}

void Server::gotAConnection()
{
    qDebug() << "Got new Connection!";
    exit(0);
}

void Server::createConnection()
{
    qDebug() << "Got new connection, more info later!";
    Server::clientConnection = tcpServer->nextPendingConnection();
    qDebug() << "Got new connection from " << Server::clientConnection->peerAddress().toString();
    emit Server::gotNewConnection(Server::clientConnection->peerAddress().toString());
}

void Server::sendData(QVariant data)
{
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint16)0;
    out << data;// << '\n' << data.type();
    qCritical() << "Sending Data!";
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));
    connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
    clientConnection->write(block);
}

void Server::closeServer()
{
    destroyConnection();
    return;
}

Server.h:

#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
//#include <QtTest/QTest>
#include <QSignalSpy>
#include <QTcpSocket>
#include <QDebug>
//#include <QMessageBox>
#include <QNetworkInterface>
#include <typeinfo>
//#include <QSignalSpy>

class Server : public QObject
{
    Q_OBJECT
public slots:
    void sessionOpened();


    void getInfo();
    void closeServer();
    void createConnection();
    void gotAConnection(void);
    void destroyConnection();
    void sendData(QVariant data);
    void showSignals(void);
signals:
    void gotNewConnection(QString);
private:
    QTcpServer *tcpServer;
    QString ipAddr;
    quint32 port;
    QTcpSocket *clientConnection;
    quint32 BlockSize;
    bool firstTime;
    QSignalSpy * m_pSignalSpy;
//    template <typename t>
//    void getD
public:
    Server(QString ipAddr, quint32 port);
};

#endif // SERVER_H

我可以运行服务器,并连接到它(例如通过Telnet),但是一旦连接到tcpServer,它就不会发出信号。 为什么? 我做错什么了吗?
编辑:添加了更新的代码

connect(Server::tcpServer, SIGNAL(newConnection()), this, SLOT(gotAConnection()));

我相信这是问题所在,此处的连接失败。

为了进行连接,第一个参数应该是从QObject派生的对象实例的地址。 成员tcpServer尚未实例化,您应该连接到该实例,并在创建QTcpServer对象之后调用它。

创建QTcpServer实例后,删除构造函数中的连接并将其添加到SessionOpened函数中。

如果使用C ++ 11,则还可以使用新的连接语法,如果出现问题,它将在编译时警告您。

Server::tcpServer = new QTcpServer(this);
connect(tcpServer, &QTcpServer::newConnection, this, &Server:: gotAConnection);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM