我目前在Mac的Qt(C ++)中编写端口扫描程序。检查某个端口是否打开的过程完全正常。但是,如果用户要检查的端口范围太大,将检查每个端口,但仅在此过程之后才输出。
该程序实际上应该检查例如端口1并输出结果。之后,它应该检查下一个和输出,等等。
void MainWindow::checkPort(int portmin, int portmax, string ip) {
int dif = portmax - portmin;
if (dif <= 0)
return;
unsigned int open = 0;
unsigned int closed = 0;
int checked = 0;
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip.c_str());
for (int i = portmin; i <= portmax; i++) {
int s = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_port = htons(i);
int con = ::connect(s, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr));
if (con == 0){
ui->textEdit->setTextColor(Qt::green);
ui->textEdit->append("Port " + QString::number(i) + " open.");
open++;
}
if (con == -1) {
ui->textEdit->setTextColor(Qt::red);
ui->textEdit->append("Port " + QString::number(i) + " closed.");
closed++;
}
::close(con);
::close(s);
checked++;
}
您有什么建议可以在每次迭代后得到输出吗?
最佳答案
最简单的解决方案是使用线程池同时运行整个扫描作业。线程间通信通过信号插槽机制安全地完成:
// 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"