大家好!我是新手,提一个问题!如何实现控制台的多线程端口扫描器。
我已经完成了单线程扫描器。
看了众多实现多线程的文章,有
1重写run的。
2 有继承QObject的。
但目前还是毫无头绪,经常出现无法解决问题,希望给与明晰的回答!
谢谢各位大神了
//头文件 scan.h
#ifndef SCAN_H
#define SCAN_H
#include <QString>
#include <QList>
#include <QDebug>
#include <QTcpSocket>
void scan( QString ad,int sp,int ep)
{
//连接并验证端口是否打开
QTcpSocket connct;
int temp=sp;
QList<int> list;//保存扫描结果
for(;temp<=ep;temp++)
{
connct.connectToHost(ad,temp);
if(connct.waitForConnected(100))
{
list.append(temp);
qDebug()<<"address--"<<ad<<":"<<temp<<"open!"<<endl;
}else{
qDebug()<<"address--"<<ad<<":"<<temp<<"close!"<<endl;
}
connct.abort();//断开连接
}
qDebug()<<"address--"<<ad<<":"<<"Finish!"<<endl;
qDebug()<<"open-port:"<<list<<endl;
}
#endif // SCAN_H
//主程序 main.cpp
#include <QCoreApplication>
#include <QDebug>
#include <QTcpSocket>
#include <iostream>
#include<QTextStream>
#include <QHostInfo>
#include "scan.h"
int main(int argc,char* argv[ ])
{
QCoreApplication a(argc,argv);
//为了QT使用c++标准输入输出
QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
//地址和端口号
int sport,eport,num;
QString address,name;
QHostInfo adsname;
cout<<"#################################################"<<endl;
cout<<"#################################################"<<endl;
cout<<"######PortScanner###Ver 1.0######################"<<endl;
cout<<"########################Power by JudeMoses#######"<<endl;
cout<<"#################################################"<<endl;
cout<<"#################################################"<<endl;
while(1){
cout<<"#please input a number (1:IP 2:website name)#####"<<endl;
cin>>num;
switch (num)
{
case 1:
cout<<"IP?"<<endl;
cin>>address;
cout<<"IP--"<<address<<endl;
cout<<"Start Port?"<<endl;
cin>>sport;
cout<<"End Port?"<<endl;
cin>>eport;
cout<<"Start-port--"<<sport<<endl;
cout<<"End---port--"<<eport<<endl;
scan (address,sport,eport);
break;
case 2:
cout<<"website name?"<<endl;
cin>>name;
adsname = QHostInfo::fromName(name);
address=adsname.addresses().first().toString();
cout<<"IP--"<<address<<endl;
cout<<"Start Port?"<<endl;
cin>>sport;
cout<<"End Port?"<<endl;
cin>>eport;
cout<<"Start-port--"<<sport<<endl;
cout<<"End---port--"<<eport<<endl;
scan (address,sport,eport);
break;
default:
cout<<"#######WARNING###################################"<<endl;
cout<<"#######WARNING###################################"<<endl;
cout<<"#######WARNING###################################"<<endl;
break;
}
}
a.exec();
}