本文出自: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();
}
}
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
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
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