每次迭代后更新帧

时间:2016-09-13 11:42:55

标签: c++ qt sockets port port-scanning

我目前正在使用Qt(C ++)for Mac编写端口扫描程序。检查某个端口是否打开的过程完全正常。 如果用户想要检查的端口范围太大,将检查每个端口,但输出仅在此过程之后发生。 该程序实际上应检查端口1的示例并输出结果。之后它应该检查下一个和输出等等......

{{1}}

您是否有任何建议如何在每次迭代后获得输出

2 个答案:

答案 0 :(得分:1)

也许是这样的:

//...
bool tooManyPorts = dif > 10000; // Set flag to true if port range is too big (for example more than 10 000 ports
// 
QString msgs = "";
for (int i = portmin; i <= portmax; i++) {
    int s = socket(AF_INET, SOCK_STREAM, 0);
    addr.sin_port = htons(i);

    if (con == 0){
        if (tooManyPorts) {
            QString("<font color='green'>Port " + QString::number(i) + " open.</font><br/>");
        }
        else {
            ui->textEdit->setTextColor(Qt::green);
            ui->textEdit->append("Port " + QString::number(i) + " open.");
        }
        open++;
    }

    if (con == -1) {
        if (tooManyPorts) {
            msgs += QString("<font color='red'>Port " + QString::number(i) + " closed.</font><br/>");
        }
        else {
            ui->textEdit->setTextColor(Qt::red);
            ui->textEdit->append("Port " + QString::number(i) + " closed.");
        }
        closed++;
    }
    // ...
}
if(tooManyPorts) {
    ui->textEdit->append(msgs); // Add all iteration messages to text edit
}

请注意HTML格式部分的用法。

这会在循环后将所有输出添加到您的字段中。为了让它适用于每次迭代,只需在循环中设置msgs = ...而不是msgs += ...,然后在if(tooManyPorts) ...的末尾移动for,而不是在外部。坦率地说,我很难理解你是否不想要第一个版本(循环之后),因为现在你 在每个迭代步骤中将输出添加到文本字段。

答案 1 :(得分:0)

最简单的解决方案是使用线程池同时运行整个扫描作业。线程间通信通过信号槽机制安全地完成:

// https://github.com/KubaO/stackoverflown/tree/master/questions/async-portscan-39469180
#include <QtWidgets>
#include <QtConcurrent>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <unistd.h>

class Scanner : public QObject {
    Q_OBJECT
    bool running = false, stop = false;
    int open = 0, closed = 0, total = 0;
    void scan() {
        running = true;
        sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr("127.0.0.1");
        for (int i = 1; i < 65536 && !stop; ++i) {
            auto s = socket(AF_INET, SOCK_STREAM, 0);
            addr.sin_port = htons(i);
            auto con = ::connect(s, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr));
            emit hasResult(i, con == 0);
            con == 0 ? ++open : ++closed;
            ++total;
            ::close(s);
        }
        emit done();
        running = false;
    }
public:
    ~Scanner() {
        stop = true;
        while (running);
    }
    Q_SIGNAL void hasResult(int port, bool open);
    Q_SIGNAL void done();
    Q_SLOT void start() {
        QtConcurrent::run(this, &Scanner::scan);
    }
};

int main(int argc, char ** argv) {
    using Q = QObject;
    QApplication app{argc, argv};
    QWidget ui;
    QVBoxLayout layout{&ui};
    QTextBrowser log;
    QProgressBar bar;
    QPushButton scan{"Scan localhost"};
    layout.addWidget(&log);
    layout.addWidget(&bar);
    layout.addWidget(&scan);
    bar.setRange(1, 65535);
    ui.show();

    Scanner scanner;
    Q::connect(&scan, &QPushButton::clicked, &scanner, [&]{
        scan.setEnabled(false);
        scanner.start();
    });
    Q::connect(&scanner, &Scanner::hasResult, &log, [&](int port, bool isOpen){
        bar.setValue(port);
        if (!isOpen) return;
        auto color = isOpen ? QStringLiteral("green") : QStringLiteral("red");
        auto state = isOpen ? QStringLiteral("open") : QStringLiteral("closed");
        log.append(QStringLiteral("<font color=\"%1\">Port %2 is %3.</font><br/>").
                   arg(color).arg(port).arg(state));
    });
    Q::connect(&scanner, &Scanner::done, &scan, [&]{
        bar.reset();
        scan.setEnabled(true);
    });
    return app.exec();
}
#include "main.moc"