[ Lập trình mạng với Java ] Sử dụng PIPE mô phỏng Client - Server, tính tổng dãy số nhập từ bàn phím

/*
Sử dụng kỹ thuật Pipe mô phỏng Client - Server 

Yêu cầu:
- Client gửi cho Server dãy số nhập vào từ bàn phím, chờ kết quả nhận được từ Server hiện thị ra màn hình
- Server nhận dãy số từ Client, tính tổng các số, tổng lần lượt được gửi cho Client.

*/

// Class Server (PipedEchoServer)----------------------------------------------------------

package pipedecho;

import java.io.*;

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

    // 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();
            tong =tong+Integer.valueOf(String.valueOf((char)ch));          
        } catch (IOException ex) {
           System.out.print(" Co loi xay ra khi nhan du lieu tu client: "+ex.toString());
        }
}

//gui ket qua da xu ly cho client

public void GuiTong(){
    try{            
   writePipe.write(tong);      
  }catch (IOException ex) {
              System.out.print(" Co loi xay ra khi gui cho Client: "+ex.toString());
  }
}

public void run()
{
    while(true) {
        Nhan_DuLieu_Tu_Client();     
        GuiTong();      
       
    }
}
}


// Class Client (PipedEchoClient)----------------------------------------

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 khi gui cho Server: "+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 khi nhan ket qua tu Server: "+ex.toString());
        }
        
    }
    
    // In ket qua nhan duoc
    public void InKetQua(){
        int Kq=Integer.valueOf(String.valueOf(ch));
        System.out.print("\n Tong: "+Kq); 
    }
    
    // thuc thi
    public void run()
    { 
        while(true) {                         
                Gui_Server();               
                Nhan_KQ_Tu_Server();
                InKetQua();               
        }
    }
}


// Class main (PipedEcho)-------------------------------------------------


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); 
        }
    }

}


/*
Lưu ý: Viết 3 class trên cùng một project, có tên package: pipedecho. Khi chạy thực thi lớp PipedEcho
*/

 [Tải Code Chương trình tại đây (Lưu ý: Sau 5s, click Bỏ qua quảng cáo - Skin Ad)]

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