杀毒软件:
杀毒软件是每一台电脑不可少的应用软件之一,现在我来研究 一下杀毒软件的整个工作流程吧。。。首先要明确杀毒软件的目的是什么,怎么样才能实现这一目的。。。
杀毒软件是客户在通过扫描自己的电脑里的每一个文件,然后与杀毒软件服务器病毒库里的病毒相比较,如果你电脑里有和服务器中文件相同的,杀毒软件就视为是病毒,然后有用户选择是否要把扫描出来的文件删除。。。。下面是我用Java语言来实现这个功能的。。。希望对大家有所感悟。现在说说我的具体实现的步骤吧。
服务器代码:
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import com.dr.bean.Virus;
public class Server {
public static List<Virus> virusList = new ArrayList<Virus>();
public static void main(String[] args) throws IOException {
ServerSocket server = null;
//输出肯定使用打印流
PrintStream out = null;
//服务器肯定也要接收数据
BufferedReader buf = null;
//实例化一个服务器的监听端
server = new ServerSocket(8888);
//可以用一种死循环的方式接收内容
System.out.println("---------服务器已经启动----------");
Socket client = null;
//初始化暗杀名单
//List<Virus> virusList = getVirusList();
while(true){
//不断接收内容
client = server.accept();
//准备好向客户端输入内容
out = new PrintStream(client.getOutputStream());
//而且客户端要有输入给服务器端
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
//接收客户端发送过来的内容
String str = buf.readLine();
System.out.println("server receive data is:"+str);
String virus = "";
if("getVirusList".equals(str)){//组成暗杀协议,返回客户端
for(Virus v :virusList){
virus += v.getName()+":";
}
out.println(virus);
}
//进行收尾工作
out.flush();
out.close();
buf.close();
client.close();
}
}
public static List<Virus> getVirusList(){
Virus virus = null;
virus = new Virus();
virus.setName("QQ.exe");
virusList.add(virus);
virus = new Virus();
virus.setName("Niu.exe");
virusList.add(virus);
virus = new Virus();
virus.setName("Baidu.exe");
virusList.add(virus);
virus = new Virus();
virus.setName("Jinshan.exe");
virusList.add(virus);
return virusList;
}
}
执行结果:
客户端代码
package com.dr.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.dr.bean.Virus;
public class Client {
private String str;
private List<Virus> virusList = null;
public Client(String str){
this.str = str;
virusList = new ArrayList<Virus>();
}
public List<Virus> send() throws UnknownHostException, IOException{
Socket client = null;
//接收服务器信息的输入流
BufferedReader buf = null;
//向服务器发送信息的输出流
PrintStream out = null;
//实例化一个套接字
client = new Socket("localhost",8888);
//从服务器接收信息
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
//向服务器打印信息
out = new PrintStream(client.getOutputStream());
//打印出去
out.println(this.str);
//接收进来QQ.exe:baidu.exe:niu.exe
String msg = buf.readLine();
System.out.println(msg);
String[] msgArray = msg.split(":");
for(int i=0;i<msgArray.length;i++){
Virus v = new Virus();
v.setName(msgArray[i]);
virusList.add(v);
System.out.println(msgArray[i]);
}
buf.close();
out.flush();
out.close();
client.close();
return virusList;
}
}
文件扫描过程代码类:::
package com.dr.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import com.dr.bean.Virus;
public class Client {
private String str;
private List<Virus> virusList = null;
public Client(String str){
this.str = str;
virusList = new ArrayList<Virus>();
}
public List<Virus> send() throws UnknownHostException, IOException{
Socket client = null;
//接收服务器信息的输入流
BufferedReader buf = null;
//向服务器发送信息的输出流
PrintStream out = null;
//实例化一个套接字
client = new Socket("localhost",8888);
//从服务器接收信息
buf = new BufferedReader(new InputStreamReader(client.getInputStream()));
//向服务器打印信息
out = new PrintStream(client.getOutputStream());
//打印出去
out.println(this.str);
//接收进来QQ.exe:baidu.exe:niu.exe
String msg = buf.readLine();
System.out.println(msg);
String[] msgArray = msg.split(":");
for(int i=0;i<msgArray.length;i++){
Virus v = new Virus();
v.setName(msgArray[i]);
virusList.add(v);
System.out.println(msgArray[i]);
}
buf.close();
out.flush();
out.close();
client.close();
return virusList;
}
}
KillVirusUI代码:
package com.dr.ui;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import com.dr.file.FileList;
public class KillVirusUI {
public static String filePath = "";
public static List<String> virusFilePath = new ArrayList<String>();
public static void main(String args[]) {
Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setBounds(2, 2, 1200, 600);
//shell.setMaximized(true);// 全屏显示
shell.setText("杀毒软件简单版");
/**
* 设置执行按钮
*/
final Button btnOk = new Button(shell, SWT.PUSH);
btnOk.setBounds(20, 25, 70, 25);
btnOk.setText("扫描杀毒");
final Button btnOk1 = new Button(shell, SWT.PUSH);
btnOk1.setBounds(120, 25, 70, 25);
btnOk1.setText("删除病毒");
Color color = new Color(Display.getCurrent(), 100, 180, 10);// 声明颜色对象
Color color1 = new Color(Display.getCurrent(), 100, 220, 240);// 声明颜色对象
final Text text = new Text(shell, SWT.MULTI | SWT.BORDER);
text.setBounds(10, 270, 1200, 400);
text.setBackground(color1);
final Text text1 = new Text(shell, SWT.MULTI | SWT.BORDER);
text1.setBounds(10, 150, 1200, 50);
text1.setBackground(color1);
final ProgressBar progressBar = new ProgressBar(shell, SWT.HORIZONTAL);
GridData data = new GridData();
data.horizontalSpan = 2;
data.grabExcessHorizontalSpace = true;
progressBar.setLayoutData(data);
progressBar.setMaximum(100);// 设置最大值
progressBar.setMinimum(0);// 设置最小值
/**
* 注册点击事件,循环显示数据
*/
Label labe=new Label(shell,SWT.NULL);
labe.setBounds(800,25, 120,75); // 设置按钮位置
labe.setFont(new Font(display,"宋体",20,SWT.BOLD));
labe.setBackground( color);
labe.setText(" "+"360"+"\n"+"网络保镖");
;
btnOk.addSelectionListener(new SelectionAdapter() {//Button监听事件
public void widgetSelected(SelectionEvent e) {
FileList f = new FileList();
DirectoryDialog dlg = new DirectoryDialog(shell);
dlg.setText("目录"); // 设置窗口标题
dlg.setMessage("请选择一个目录:"); // 设置提示文字
dlg.setFilterPath("/root"); // 设置初始目录
String dir = dlg.open(); // 打开对话框并返回一个包含所选目录路径的字符串
//File f=new File(dlg.open());
f.setStr(dir);
if (f != null)
System.out.println(f); // 比如选择“我的文档”,则会打印“D:\My Documents”
Thread t = new Thread(f);
t.setDaemon(true);
t.start();
t.yield();
for(int i=0;i<100;i++){
text.append(filePath+"\n");
progressBar.setBounds(10, 80, 1200, 20);
progressBar.setSelection(progressBar.getSelection()+1);//显示一条数据,滚动条进度加1
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
if(virusFilePath.size()!=0){
text.setText("");
for(String str : virusFilePath){
text1.append("很悲剧:你的电脑里发现病毒:"+str+"\n");
}
}
else{
text1.setText("恭喜你,没有发现病毒!");
}
t.interrupt();
}
});
btnOk1.addSelectionListener(new SelectionAdapter() {//Button监听事件
public void widgetSelected(SelectionEvent e) {
FileList q = new FileList();
Thread t = new Thread(q);
t.setDaemon(true);
t.start();
for(int i=0;i<100;i++){
text.append(filePath+"\n");
progressBar.setBounds(10, 105, 1200, 20);
progressBar.setSelection(progressBar.getSelection()+1);//显示一条数据,滚动条进度加1
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
if(virusFilePath.size()!=0){
text.setText("");
for(String str : virusFilePath){
//text1.append("发现病毒:"+str+"\n");
File f1=new File("f.filePath");
f1.delete();
text1.append("恭喜你已经成功清理了电脑里的病毒:"+str+"\n");
}
}
else{
text1.setText("祝贺你不用为电脑安危考虑了!");
}
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
执行结果:
首先要启动服务器,杀毒软件才能够正常的工作。。。。。。。
病毒类:
package com.dr.bean;
public class Virus{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
posted on 2010-11-23 13:43
龍ぜ殘剑 阅读(2100)
评论(2) 编辑 收藏