QThreadPool::globalInstance()->setMaxThreadCount(600);
bool CommonFunctions::isPortListen(int port, QString ipAddress, int msecs){ QTcpSocket sock; sock.connectToHost(QHostAddress(ipAddress),port); if(sock.waitForConnected(msecs)) { sock.close(); return true; } return false;}
#ifndef LISTENPORT_H#define LISTENPORT_H#include "commonfunctions.h"#include <QObject>#include <QRunnable>class ListenPort : public QObject, public QRunnable{ Q_OBJECTpublic: explicit ListenPort(QString ipAddress, int port, int msecs, QObject *parent = 0);private: QString _ipAddress; int _port; int _msecs; void run();signals: void portIsListen(QString ipAddress, int port); void stopped();public slots:};#endif // LISTENPORT_H
#include "listenport.h"ListenPort::ListenPort(QString ipAddress, int port, int msecs, QObject *parent) : QObject(parent), _ipAddress(ipAddress), _port(port), _msecs(msecs){ }void ListenPort::run(){ if(CommonFunctions::isPortListen(_port,_ipAddress,_msecs)) { emit portIsListen(_ipAddress,_port); } if(_port == 65535) { emit stopped(); }}
void IpStandardItem::startPortScan(){ QThreadPool *pool = new QThreadPool(); for(int i=0;i<=65535;i++) { ListenPort *lisP = new ListenPort(_ipInformation.IpAddress,i,10); connect(lisP,&ListenPort::portIsListen,[=](QString, int port){ emit canInsertPort(port); }); pool->start(lisP); } }
void MainWindow::recursivePortScan(QString ipAddress, int port){ qDebug() << port; if(ports_count > 65535) { qDebug() << "scanned all"; return; } tek_ports_count++; QTcpSocket *sock = new QTcpSocket(); connect(sock,&QTcpSocket::stateChanged,[=](QAbstractSocket::SocketState test){ if(test == QAbstractSocket::ConnectedState) { qDebug() << port; sock->disconnectFromHost(); } else if(test == QAbstractSocket::UnconnectedState) { sock->close(); } }); connect(sock,&QTcpSocket::disconnected,[=](){ sock->close(); }); connect(sock,&QTcpSocket::aboutToClose,[=](){ sock->blockSignals(true); sock->deleteLater(); ports_count++; tek_ports_count--; if(tek_ports_count <= 5000) { recursivePortScan(ipAddress,ports_count + 5001);//вызываем рекурсивно, освободилось место для сканирования } }); sock->connectToHost(QHostAddress(ipAddress),port);}
tek_ports_count = 0; ports_count = 0; for(int i=0;i<=65535/*5000*/;i++) { recursivePortScan("192.168.56.254", i); }
QEventDispatcherWin32::registerTimer: Failed to create a timer (Текущий процесс использовал все системные разрешения по управлению объектами диспетчера окон.)