一个Socket服务的例子! 请高手指点无法捕捉的输出如何修改?

1/5/2008来源:Java教程人气:5981


  一个Socket服务的例子!
请高手指点无法捕捉的输出如何修改?
//服务端
package org.dyol.net;

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

public class Console {

public static void main(String args[]){
if (args.length!=1)
throw new IllegalArgumentException("unlawful number of args: USER: Console ");
new Console(Integer.parseInt(args[0]));
}

public Console(int port){
this.port = port;
try {
ServerSocket socket = new ServerSocket(port);
System.out.PRintln("Start service on "+port);
while(true){
new Transmit(socket.accept()).start();
}
}catch (IOException e){}
}

private class Transmit extends Thread {
private String sys = System.getProperty("os.name")+" "+System.getProperty("os.arch")+" "+System.getProperty("os.version");
private Socket sock;
public Transmit(Socket sock){
this.sock = sock;
try {
input = new BufferedReader(new InputStreamReader(sock.getInputStream(),"gb2312"));
output = new PrintStream(sock.getOutputStream());
output.println(sys);
}catch(Exception e){}
}
public void run(){
System.out.println("while(true){
try {
synchronized(output){
cmd = input.readLine();
if( cmd == null ) break;
System.out.println("Start Command");
System.out.println("[ "+new Date().toString()+" Receive COMMAND From : "+sock.getInetAddress()+" ]: "+cmd);
proc = Runtime.getRuntime().exec(cmd);
dis = new BufferedReader(new InputStreamReader(proc.getInputStream(),"gb2312"));
}
synchronized(output){
while((msg = dis.readLine())!=null){
if(msg.equals("EXIT".toLowerCase()))break;
output.println(msg);
}
}
System.out.println("End Command");
}catch (IOException e){
break;
}
}
System.out.println("End Thread ---------------------->");
}
}

private int port = 520;
private BufferedReader input;
private PrintStream output;
private String cmd;
private Process proc;
private String msg;
private BufferedReader dis ;

}

//客户端
import java.io.*;
import java.net.*;

public class Client {
public static void main(String[] args){
if (args.length != 2)
throw new IllegalArgumentException("Wrong number of args");
String host = args[0];
int port = Integer.parseInt(args[1]);
new Client(host, port);
}
public Client(String host,int port){
try {
Socket s = new Socket(host, port);
final BufferedReader echo=new BufferedReader(new InputStreamReader(s.getInputStream(),"gb2312"));
PrintWriter ps = new PrintWriter(s.getOutputStream());
BufferedReader cmd = new BufferedReader(new InputStreamReader(System.in,"gb2312"));
final PrintWriter pis = new PrintWriter(System.out, true);
pis.println("Connected to "+s.getInetAddress()+":" + s.getPort());
Thread t = new Thread() {
public void run() {
char[] buffer = new char[1024];
int ch;
try {
while((ch = echo.read(buffer)) != -1) {
for(int i = 0; i < ch; i++) {
if (buffer[i] == ' ') pis.println();
else pis.print(buffer[i]);
}
pis.flush();
}
}catch (IOException e){
pis.println(e);
}
pis.println("Connection closed by server.");
System.exit(0);
}
};
t.setPriority(Thread.currentThread().getPriority() + 1);
t.start();
String line;
while((line = cmd.readLine()) != null) {
ps.print(line + " ");
ps.flush();
}
s.close();
pis.println("Connection closed by client.");
System.exit(0);
}catch (Exception e) {}
}
}