suhuanxi 发表于 2013-1-15 03:11:23

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

本文出自:http://blog.csdn.net/wopopo/archive/2008/03/11/2169991.aspx
Server端/主机:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">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();
    }

}
页: [1]
查看完整版本: 一个java访问远程主机的程序