Chủ đề: Client/ Server sử dụng PIPE line
-
14-04-2015, 05:23 PM #1Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Client/ Server sử dụng PIPE line
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);
}
}
}View more random threads:
- Login turn server
- vẽ 1 hình và xoay hình chuyển động quanh 1điểm
- Sử dụng lệnh sql vào JAVA, class chương trình chạy nhưng không có kết quả?
- Cách cài đặt và chạy Proximity như thế nào?
- Xử lý 1 string với nhiều ký tự đánh dấu
- Viết Game Purble Place chơi trong mạng Lan bằng Java như thế nào?
- Cách tạo một đối tượng và sử dụng con trỏ trong Java như C++?
- Lớp Ticker nằm trong package nào?
- Cách gọi actionPerformed (Mouse Click) từ KeyListener
- Làm sao để đóng form2 mà không tắt form1
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...