Qt5.2中使用ping命令实现Ip扫描功能
2024-10-20 00:43:11
在实现类似于Free IP Scanner 2.1的Ip扫描器软件中,会用到ping命令。如果使用Qt编程实现,主要会用QThread、QProcess这两个类。关于这两个类的具体用法可以查阅Qt助手或者QT官网。
在QT中为了扫描大量的Ip,通常需要将扫描Ip的任务放在一个单独的线程来完成,这样需要子类化QThread,重写其run()方法。从QThread类派生出一个ShellProcess的子类,该类的头文件代码如下:
#ifndef SHELLPROCESS_H
#define SHELLPROCESS_H
#include <QThread>
#include <QtCore/QStringList>
// Ping IP的线程子类
class ShellProcess : public QThread
{
Q_OBJECT
public:
explicit ShellProcess(QObject *parent = 0);
QStringList getIpRangle(); //获取需要扫描的IP列表
void setIpRange(QStringList ipRange); //设置需要扫描的IP列表
protected:
void run();
signals:
void commandSuccessed(QString ip);
void commandFailed(QString ip);
public slots:
private:
QStringList ipRange; //需要扫描的IP列表
};
#endif // SHELLPROCESS_H
ShellProcess类的实现代码如下:
#include "shellprocess.h"
#include <QtCore/QtDebug>
#include <QtCore/QProcess>
ShellProcess::ShellProcess(QObject *parent) :
QThread(parent)
{
}
QStringList ShellProcess::getIpRangle()
{
return this->ipRange;
}
void ShellProcess::setIpRange(QStringList ipRange)
{
this->ipRange = ipRange;
}
void ShellProcess::run()
{
QString ip;
//依次扫描Ip列表中的各个IP
foreach( ip, ipRange )
{
int exitCode;
//对每个Ip执行ping命令检测其是否在线
qDebug() << "ping " + ip << endl;
#ifdef Q_OS_WIN
QString strArg = "ping " + ip + " -n 1 -i 2";
exitCode = QProcess::execute(strArg);
#else
//其他平台(Linux或Mac)
exitCode = QProcess::execute("ping", QStringList() << "-c 1" << "-t 2" << ip));
#endif
if(0 == exitCode)
{
//it's alive
qDebug() << "shell ping " + ip + " sucessed!";
//发射该IP在线的信号
emit commandSuccessed(ip);
} else {
qDebug() << "shell ping " + ip + " failed!";
//发射IP离线的信号
emit commandFailed(ip);
}
}
}
在实际编程中可以这样使用ShellProcess类:
#define THREAD_SIZE 10 //线程数
ShellProcess *shellProcess;
for(int i=0;i<THREAD_SIZE;i++){
shellProcess= new ShellProcess(this);
connect(shellProcess, SIGNAL(commandSuccessed(QString)), this, SLOT(slot_onCommandSuccessed(QString)));
connect(shellProcess, SIGNAL(commandFailed(QString)), this, SLOT(slot_onCommandFailed(QString)));
connect(shellProcess, SIGNAL(finished()), shellProcess, SLOT(deleteLater()));
//设置每个线程的Ip扫描范围,一共10个线程
shellProcess->setIpRange(ip_in_thread[i]);
//启动线程
shellProcess->start();
}
具体的项目代码可以参看我github上使用Qt5.2的一个Ip扫描器:ip_scan,目前只完成了一部分功能。
另外,需要注意的是ping命令在Windows下和Linux(Mac)下的命令有所不同,可以参考Linux和Windows下ping命令详解1和Linux和Windows下ping命令详解2
最新文章
- SSIS 属性:ExecValueVariable
- jsp页面路径问题
- 【M20】协助完成“返回值优化(RVO)”
- Web前端新人笔记之CSS值和单位
- javascript 写农场迭代
- GNU 网络程序
- php怎样求一个数组中最长的
- SQL 复习二(数据查询语言)
- Linux的五种I/O模式
- 我的第一个python web开发框架(12)——工具函数包说明(三)
- spring mvc 复杂参数注入
- Python入门经典 以解决计算问题为导向的Python编程 待完好
- DPDK virtio-user
- vue项目报错webpackJsonp is not defined
- Java单例模式之最优解分析【为何说是最优解】
- 解决[Errno 10048]报错
- Spring Boot学习记录03_一些属性配置文件
- Matlab adaptive quadrature
- 关于promise的几个认知
- [Windows Azure] Virtual Machine and Cloud Service Sizes for Windows Azure
热门文章
- (转)SQL注入原理
- 设置xampp开机自动启动
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
- SQL数据库—<;2>;数据库基本操作(CRUD)
- linux100day(day7)--用户管理和权限管理简单介绍
- Linux Shell脚本在service模式下的环境变量
- 使用intellij的idea集成开发工具中的git插件(转)
- 六、SpringBoot配置@ConfigurationProperties与@Value区别
- java23种设计模式(一)-- 工厂模式、抽象工厂模式和单例模式
- 【leetcode】sudokuSolver数独解题