k4info
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

[Java]Bài tập về nhà từ điển Anh-Việt trao đổi thông tin giữa 2 luồng trong 1 chường trình bằng ống dẫn Pipe

Go down

Cool [Java]Bài tập về nhà từ điển Anh-Việt trao đổi thông tin giữa 2 luồng trong 1 chường trình bằng ống dẫn Pipe

Bài gửi by Admin Sun Oct 07, 2012 7:53 pm

Lưu ý chương trình này chỉ là trao đổi truyền thông tin giửa 2 luồng trong 1 chương trình. Anh em chỉ Class Main.

Class luồng Client:

Code:

package Dictionary;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Client extends Thread {

   /**
    * @param args
    */
   
   PipedInputStream inputC;
   PipedOutputStream outputC;
   Scanner read;
   PrintWriter write;
   String s1,s2;
   Client()
   {}
   public Client( PipedInputStream input, PipedOutputStream output)
   {
      this.inputC=input;
      this.outputC=output;
      write = new PrintWriter(System.out);
      write.println("Client đã khởi động...");
   }
   public void inputString()
   {
      read = new Scanner(System.in);
      write = new PrintWriter(System.out);
      write.print("Nhập từ cần tra>>");
      write.flush();
      s1 = read.nextLine();
   }
   public void Send()
   {
      write = new PrintWriter(outputC);
      write.println(s1);
      write.flush();
   }
   public void Reviced()
   {
      read = new Scanner(inputC);
      s2 = read.nextLine();
   }
   public void Proscing()
   {
      write = new PrintWriter(System.out);
      write.println("Ngữ nghĩa: "+s2);
      write.flush();
   }
   public void run()
   {
      while(true)
      {
         inputString();
         Send();
         Reviced();
         Proscing();
      }
   }
   
}


Class luồng Server:
Code:

package Dictionary;

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Server extends Thread {

   /**
    * @param args
    */
   String tv[] = {"dictionary","hello","cat","pen","phone","rose","table","pig","duck","eat","drink"};
   String tv2[]= {"từ điển","xin chào","con mèo","cây viết","điện thoại","hoa hồng","cái bàn","con lợn","con vịt","ăn","uống"};
   PipedInputStream inputS;
   PipedOutputStream outputS;
   int i;
   String s1,s2="Xin lỗi từ này không có trong thư viện";
   Scanner read;
   PrintWriter write;
   Server()
   {}
   public Server( PipedInputStream input, PipedOutputStream output)
   {
      this.inputS = input;
      this.outputS = output;
      write = new PrintWriter(System.out);
      write.println("Server đã khởi động...!");
      write.flush();
   }
   public void Reviced()
   {
      read = new Scanner(inputS);
      s1 = read.nextLine();
   }
   public void Procsing()
   {
      for(int j =0; j<tv.length; j++)
      {
         if(s1.equals(tv[j]))  { i=j; s2=tv2[i]; }
      }
   }
   public void Send()
   {
      write = new PrintWriter(outputS);
      write.println(s2);
      write.flush();
      s2="Xin lỗi từ này không có trong thư viện";
   }
   public void run()
   {
      while(true)
      {
         Reviced();
         Procsing();
         Send();
      }
   }

}

Lưu ý để cập nhật thêm từ vựng cho từ điển Anh-Việt ta cập nhật ở class Server( chương trình hiện tại chỉ có 1 số từ đơn gian) ngay dòng này bên Class Server:
Code:

String tv[] = {"dictionary","hello","cat","pen","phone","rose","table","pig","duck","eat","drink"};
   String tv2[]= {"từ điển","xin chào","con mèo","cây viết","điện thoại","hoa hồng","cái bàn","con lợn","con vịt","ăn","uống"};

Class Main dùng để run chương trình:

Code:

package Dictionary;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class Main {

   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      PipedInputStream inputC = new PipedInputStream();
      PipedOutputStream outputC = new PipedOutputStream();
      PipedInputStream inputS = new PipedInputStream();
      PipedOutputStream outputS = new PipedOutputStream();
      System.out.println("Chương trình từ Từ điển Anh-Việt!");
         try {
            
            inputS.connect(outputC);
            outputS.connect(inputC);
            Server S = new Server(inputS, outputS);
            Client C = new Client(inputC, outputC);
            S.start(); C.start();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
   }

}

Admin
Admin

Posts : 1013
Thanked : 47
Gia Nhập 25/08/2010

Tài Sản
Thú nuôi:

https://k4info.forumvi.com

Về Đầu Trang Go down

Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết