QTcpSocket信号bytesWriteln和readyRead不起作用

时间:2019-03-18 17:22:11

标签: c++ qt qtcpsocket

我是QT的新手,很抱歉提出这个愚蠢的问题。

#include "simpletcp.h"

#include <QDebug>

SimpleTCP::SimpleTCP(const QString &srv, int port)
{
    sock = new QTcpSocket(this);

    QObject::connect(sock, &QTcpSocket::bytesWritten, this, &SimpleTCP::writeln);
    QObject::connect(sock, &QTcpSocket::connected, this, &SimpleTCP::connected);
    QObject::connect(sock, &QTcpSocket::readyRead, this, &SimpleTCP::ready);
    sock->connectToHost(srv, port);
    if (!sock->waitForConnected(300)) {
        qDebug() << "Failing to connect to srv";
    }
}

void SimpleTCP::connected()
{
    const char f[] = "HELO localhost.loc";
    qDebug() << sock->write(f, sizeof (f));
}

void SimpleTCP::writeln(qint64 num)
{
    qDebug() << "From writeln slot: " << num;
}

void SimpleTCP::ready()
{
    qDebug() << "From ready slot: ";
    qDebug() << sock->readAll();
}

simpletcp.h文件

#ifndef SIMPLETCP_H
#define SIMPLETCP_H

#include <QObject>
#include <QTcpSocket>

class SimpleTCP : public QObject
{
    Q_OBJECT;
    QTcpSocket *sock = nullptr;
public:
    SimpleTCP(const QString& srv, int port);

public slots:
    void connected();
    void writeln(qint64 num);
    void ready();
};

#endif // SIMPLETCP_H

main.cpp文件:

#include "simpletcp.h"

int main()
{
    SimpleTCP *tcp = new SimpleTCP("127.0.0.1", 25);
    delete tcp;
    return 0;
}

我连接到本地SMTP服务器(后缀),为我输出了一些greating-string(我使用telnet测试了它)。 当我运行代码时,它的输出为“ 19”,而没有其他输出。为什么bytesWriteln和readyRead的插槽不起作用?我做错了什么?

0 个答案:

没有答案