QTcpSocket不发出信号

时间:2014-11-17 14:39:57

标签: c++ qt tcp

我在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一旦连接到它就不会发出信号。为什么?我做错了什么吗? 编辑:更新了代码

1 个答案:

答案 0 :(得分:0)

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

我认为这是问题所在,这里的连接失败了。

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

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

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

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