Giả lập dịch vụ phản hồi thông tin bằng pipe

3 114 0
Giả lập dịch vụ phản hồi thông tin bằng pipe

Đang tải... (xem toàn văn)

Thông tin tài liệu

Giả lập dịch vụ phản hồi thông tin Pipe Giả lập dịch vụ phản hồi thông tin Pipe Bởi: unknown Giả lập dịch vụ phản hồi thông tin Pipe Phần ta xây dựng ứng dụng có tên PipeEcho mô dịch vụ phản hồi thông tin để minh họa cách sử dụng Pipe làm phương tiện giao tiếp Thread ứng dụng Mô hình ứng dụng PipeEcho Trong ứng dụng Client Server hai Thread thuộc lớp PipedEchoClient PipedEchoServer Việc trao đổi thông tin client server thực thông qua Pipe (cwPipe-srPipe) (swPipe-crPipe) PipedEchoClient nhận ký tự từ bàn phím, gởi chúng sang PipedEchoServer chờ nhận ký tự gởi từ PipedEchoServer để in hình PipedEchoServer chờ nhận ký tự từ PipedEchoClient, đổi ký tự nhận thành ký tự hoa gởi ngược PipedEchoClient 1/3 Giả lập dịch vụ phản hồi thông tin Pipe Lớp PipedEchoServer Hãy lưu chương trình sau vào tập tin PipedEchoServer.java import java.io.*; 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 ); } } }} Lớp PipedEchoClient Hãy lưu chương trình sau vào tập tin PipedEchoClient.java import java.io.*; 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 ); } } }} Lớp PipedEcho Hãy lưu chương trình sau vào tập tin PipedEcho.java 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); } catch(IOException ie) { System.out.println("Pipe Echo Error:"+ie); } }} Biên dịch thực thi chương trình Biên dịch thực thi chương trình theo cách sau: 2/3 Giả lập dịch vụ phản hồi thông tin Pipe Kết biên dịch chương tình PipedEcho.java Nhập vào bàn phím chuỗi ký tự mà bạn muốn nhấp phím Enter Bạn thấy chuỗi ký tự in hoa chuỗi vừa nhập xuất hình 3/3 .. .Giả lập dịch vụ phản hồi thông tin Pipe Lớp PipedEchoServer Hãy lưu chương trình sau vào tập tin PipedEchoServer.java import java.io.*; public class PipedEchoServer extends Thread { PipedInputStream... PipedInputStream(); PipedOutputStream swPipe = new PipedOutputStream(crPipe); PipedInputStream srPipe = new PipedInputStream(cwPipe); PipedEchoServer server = new PipedEchoServer(srPipe,swPipe); PipedEchoClient... PipedInputStream readPipe; PipedOutputStream writePipe; PipedEchoServer(PipedInputStream readPipe, PipedOutputStream writePipe){ this.readPipe = readPipe; this.writePipe = writePipe; System.out.println("Server

Ngày đăng: 30/12/2015, 16:42

Từ khóa liên quan

Mục lục

  • Giả lập dịch vụ phản hồi thông tin bằng Pipe

  • Giả lập dịch vụ phản hồi thông tin bằng Pipe

    • Lớp PipedEchoServer

    • Lớp PipedEchoClient

    • Lớp PipedEcho

    • Biên dịch và thực thi chương trình 

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan