一个java访问远程主机的程序

本文出自:http://blog.csdn.net/wopopo/archive/2008/03/11/2169991.aspx

Server端/主机:

package  p11;

import  java.io. * ;
import  java.net.ServerSocket;
import  java.net.Socket;

public   class  ServerMain {
    
public   static   int  TCP_PORT  =   8888 ;
    ServerSocket ss 
=   null ;
    String cmd 
=   null ;


    
public   void  startServer() {
        
try  {
            ss 
=   new  ServerSocket(TCP_PORT);
        } 
catch  (IOException e) {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        }

        
while  ( true ) {
            
try  {
                Socket sct 
=  ss.accept();
                Thread td 
=   new  Thread( new  ServerListenerThread(sct));
                td.start() ;
            } 
catch  (IOException e) {
                
//  TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }



    
public   static   void  main(String[] args) {
        
if  (args.length  >   0 ){
            
try {
                
int  xx  =  Integer.parseInt(args[ 0 ]) ;
                ServerMain.TCP_PORT 
=  xx ;
            }
catch (Exception e){
                
            }
        }
        
try  {
            
// 在主机上创建一个bat文件可以执行你得一切命令
            
// 因为dos的一些命令例如dir  tree 等命令为内部命令 ,java远程不能直接执行
            
// 运行方式举例:
            
// c:\\11.bat dir c:
            
// c:\\11.bat del d:\\11.data
            
            
            
            
// 普通的ping  netstat  ipconfig等命令是可以直接运行的
            FileOutputStream fs  =   new  FileOutputStream( " c:/11.bat " ) ;
            PrintWriter fw 
=   new  PrintWriter( new  OutputStreamWriter(fs)) ;
            fw.println(
" %1  %2 %3 %4 %5 %6 %7 %8 %9 " ) ;
            fw.close() ;
            fs.close() ;
        } 
catch  (FileNotFoundException e) {
            
//  TODO Auto-generated catch block
            e.printStackTrace();
        }
catch (IOException e){
            e.printStackTrace() ;
        }
        
        
        
        
new  ServerMain().startServer();
    }

}

 

package  p11;

import  java.io.BufferedReader;
import  java.io.DataInputStream;
import  java.io.DataOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStream;
import  java.net.Socket;

public   class  ServerListenerThread  implements  Runnable  ... {
    
private Socket sct = null;

    
public ServerListenerThread(Socket s) ...{
        
this.sct = s;
    }


    
public void run() ...{

        
try ...{
            DataInputStream ir 
= new DataInputStream(sct.getInputStream());
            String cmd 
= ir.readUTF();
            
while (true...{

                
try ...{
                    OutputStream os 
= sct.getOutputStream();
                    System.out.println(cmd);
                    
if (cmd.trim().equalsIgnoreCase("over")) ...{
                        
break;
                    }

                    Process pc 
= null ;
                    
try ...{
                            
                    pc 
= Runtime.getRuntime().exec(cmd);
                    }
 catch (IOException e1) ...{
                        
// TODO Auto-generated catch block
                        System.err.println("错误的命令 :" + cmd);
                        DataOutputStream ds 
= new DataOutputStream(os);
                        ds.writeUTF(
"错误的命令 :" + cmd) ;
                        cmd 
= ir.readUTF();
                        
                        
continue;
                    }

                    
                        InputStream sin 
= pc.getInputStream();
                        InputStream sin2 
= pc.getErrorStream();
                        Thread tIn 
= new Thread(new DoProccess(sin, os));
                        Thread tErr 
= new Thread(new DoProccess(sin2, os));
                        tIn.start();
                        tErr.start();
                        
int result = 0;
                        
try ...{
                            result 
= pc.waitFor();
                            tIn.join();
                            tErr.join();
                        }
 catch (InterruptedException e) ...{
                            
// TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        
/**//*
                        sin.close();
                        sin2.close();
                        os.close();
                        
*/
                        
if (result == 0...{
                            System.out.println(
"  *SUCCESS*  ");
                        }
 else ...{
                            System.out.println(
"  *FAILED*  ");
                        }

                        cmd 
= ir.readUTF();


                }
 catch (IOException e1) ...{
                    
// TODO Auto-generated catch block
                    e1.printStackTrace();
                }
 
            }


        }
 catch (IOException e2) ...{
            
// TODO Auto-generated catch block
            e2.printStackTrace();
        }
 finally ...{
            System.out.println(
"-- will ---End . ---------");
            System.out.println(
"-- is ---End . ---------");
            
            
try ...{
                sct.close();
            }
 catch (IOException e) ...{
                
// TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }

}

 

 

package  p11;

import  java.io. * ;
import  java.io.DataInputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStream;

public   class  DoProccess  implements  Runnable  ... {
    
private InputStream in;
    
private OutputStream ot;
    
public DoProccess(InputStream pc,OutputStream ot) ...{

        
this.in = pc;
        
this.ot = ot ;
    }


    
public void run() ...{
        
// TODO Auto-generated method stub

        BufferedReader inputBufferedReader 
= new BufferedReader(
                
new InputStreamReader(in));
        DataOutputStream ds 
= new DataOutputStream(ot);

        String line 
= null;
        
try ...{
            
while ((line = inputBufferedReader.readLine()) != null...{
                System.out.println(
"--:    "+line);
                ds.writeUTF(line) ;
                
            }

        }
 catch (IOException e1) ...{
            
// TODO Auto-generated catch block
            e1.printStackTrace();
        }


    }


}

客户端/控制端

 

package  p12;

import  java.io.BufferedReader;
import  java.io.DataInputStream;
import  java.io.DataOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.net.Socket;
import  java.net.UnknownHostException;

import  javax.swing.JOptionPane;

import  p11.ServerMain;

public   class  ClientMain {
    
public   static   int  TCP_PORT  =   8888 ;
    
public   static  String ip  =   " 127.0.0.1 " ;

    
public   static   void  main(String[] args) {
        
if  (args.length  >   1  ){
            ip 
=  args[ 0 ] ;
            
try {
                
int  xx  =  Integer.parseInt(args[ 1 ]) ;
                ClientMain.TCP_PORT 
=  xx ;
            }
catch (Exception e){
                
            }
        }
        
new  ClientMain().connect();
    }

    
public   void  connect() {
        
try  {
            Socket sct 
=   new  Socket(ip, TCP_PORT);
            System.out.println(
" 连接成功 " );
            InputStream is 
=  sct.getInputStream() ;
            
new &nbs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值