[ Java ] Lập trình truyền thông - Sử dụng Pipe - Ví dụ1

/*  Pipe - java
Ví dụ1: Viết chương trình mô phỏng Client-Server
- Client gửi cho Server 1 xâu ký tự (nhập từ bàn phím), chờ nhận xâu kết quả từ Server rồi in ra màn hình.
- Server nhận xâu ký tự từ Client, đổi xâu đó ra chữ hoa, gửi kết quả lại cho Client.
*/

// --------------------------------------------------------------------------------------------
// Class Sever

package pipedecho;
import java.io.*; 

public class PipedEchoServer extends Thread 
{
    PipedInputStream readPipe; 
    PipedOutputStream writePipe; 
    int ch;

    // khoi tao
public PipedEchoServer(PipedInputStream readPipe, PipedOutputStream writePipe)

    this.readPipe = readPipe; 
    this.writePipe = writePipe; 
    System.out.println("Server is starting . . .");  


// nhan du lieu tu client
public void Nhan_DuLieu_Tu_Client(){
        try {
            ch = readPipe.read();
        } catch (IOException ex) {
           System.out.print(" Co loi xay ra: "+ex.toString());
        }
}

//gui ket qua da xu ly cho client
public void Gui_QK_Cho_Client(){
        try {
            writePipe.write(ch);
        } catch (IOException ex) {
              System.out.print(" Co loi xay ra: "+ex.toString());
        }
}

// Doi chu thuong thanh chu hoa
public void DoiChu(){
    ch = Character.toUpperCase((char)ch);     
}
public void run()

    while(true) { 
        Nhan_DuLieu_Tu_Client();
        DoiChu();
        Gui_QK_Cho_Client();
    }
}
}




// --------------------------------------------------------------------------------------------
// Class Client

package pipedecho;
import java.io.*; 


public class PipedEchoClient extends Thread { 
    PipedInputStream readPipe; 
    PipedOutputStream writePipe; 
    int ch;
    
    public PipedEchoClient(PipedInputStream readPipe, PipedOutputStream writePipe){ 
        this.readPipe = readPipe; 
        this.writePipe = writePipe; 
        System.out.println("Client creation");         
    } 
  
    // doc 1 ky tu tu ban phim, gui ky tu do cho server
    public void Gui_Server(){   
        try {           
           ch=System.in.read(); 
           writePipe.write(ch);
        } catch (IOException ex) {
            System.out.print(" Co loi xay ra: "+ex.toString());
        }
        
    }
    
    // nhan 1 ky tu tu server
    public void Nhan_KQ_Tu_Server() {
        
        try {            
            ch = readPipe.read();
        } catch (IOException ex) {
           System.out.print(" Co loi xay ra: "+ex.toString());
        }
        
    }
    
    // In ket qua nhan duoc
    public void InKetQua(){
        System.out.print((char)ch); 
    }
    
    // thuc thi
    public void run()
    { 
        while(true) {          
                Gui_Server();
                Nhan_KQ_Tu_Server();
                InKetQua();               
        }
    }
}





// --------------------------------------------------------------------------------------------
// Class Main
package pipedecho;

import java.io.*; 

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); 
       
            server.start();
            client.start();
            
        } catch(IOException ie) { 
            System.out.println("Pipe Echo Error:"+ie); 
        }
    }

}

// Chú ý: Tạo 3 class trong cùng 1 project (PipedEcho)

Một số tài liệu và khoá học bổ ích dành cho bạn: 

# Tài liệu: Lập trình hướng đối tượng JAVA core dành cho người mới bắt đầu học lập trình [Click để xem]

# Khoá học online: Lập trình Java trong 4 tuần [Click để xem]

Tìm kiếm nội dung khác: