殺毒軟件:
殺毒軟件是每一臺電腦不可少的應用軟件之一,現在我來研究 一下殺毒軟件的整個工作流程吧。。。首先要明確殺毒軟件的目的是什么,怎么樣才能實現這一目的。。。
殺毒軟件是客戶在通過掃描自己的電腦里的每一個文件,然后與殺毒軟件服務器病毒庫里的病毒相比較,如果你電腦里有和服務器中文件相同的,殺毒軟件就視為是病毒,然后有用戶選擇是否要把掃描出來的文件刪除。。。。下面是我用Java語言來實現這個功能的。。。希望對大家有所感悟?,F在說說我的具體實現的步驟吧。
服務器代碼:
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
龍ぜ殘劍 閱讀(2107)
評論(2) 編輯 收藏