Mọi người cho mình hỏi với chương trình sử dụng PIPE line trao đổi thông tin theo mô hình Client/Server sau đây, có thể tách hàm main riêng ra đưa vào trong Client và trong Sever để chạy lần lượt main Server -> main Client được không ạ:
//PipeEchoServer
public class PipedEchoServer extends Thread {
PipedInputStream readPipe;
PipedOutputStream writePipe;
PipedEchoServer(PipedInputStream readPipe, PipedOutputStream writePipe){
this.readPipe = readPipe;
this.writePipe = writePipe;
System.out.println("Server is starting . . .");
start();
}
public void run(){
while(true) {
try{
int ch = readPipe.read();
ch = Character.toUpperCase((char)ch);
writePipe.write(ch);
}
catch (IOException ie) { System.out.println("Echo Server Error: "+ie ); }
}
}
}

//PipeEchoClient
public class PipedEchoClient extends Thread {
PipedInputStream readPipe;
PipedOutputStream writePipe;
PipedEchoClient(PipedInputStream readPipe, PipedOutputStream writePipe){
this.readPipe = readPipe;
this.writePipe = writePipe;
System.out.println("Client creation");
start();
}
public void run(){
while(true) {
try {
int ch=System.in.read();
writePipe.write(ch);
ch = readPipe.read();
System.out.print((char)ch);
}
catch(IOException ie){
System.out.println("Echo Client Error: "+ie );
}
}
}
}

//hàm Main chính
public class PipedEcho {
public static void main(String argv[]){
try{
PipedOutputStream cwPipe = new PipedOutputStream();
PipedInputStream crPipe = new PipedInputStream();
PipedOutputStream swPipe = new PipedOutputStream(crPipe);
PipedInputStream srPipe = new PipedInputStream(cwPipe);
PipedEchoServer server = new PipedEchoServer(srPipe,swPipe);
PipedEchoClient client = new PipedEchoClient(crPipe,cwPipe);
} catch(IOException ie) {
System.out.println("Pipe Echo Error:"+ie);
}
}
}