[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
Trang 1 trong tổng số 1 trang
[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
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:
Class luồng Server:
Class Main dùng để run chương trình:
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();
}
}
}
- 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();
}
}
}
Similar topics
» [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài đọc số từ Client gởi lên Server
» [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2
» HÀNG LOẠT GAME JAVA PHIÊN BẢNG VIỆT ĐÂY
» Viết chương trình nhập mật khẩu (tối đa 127 ký tự)
» [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc
» [Java] trao đổi giữa 2 chương trình bằng Socket. Đề tài thực hiện phép toàn Operant1 OP Operant2
» HÀNG LOẠT GAME JAVA PHIÊN BẢNG VIỆT ĐÂY
» Viết chương trình nhập mật khẩu (tối đa 127 ký tự)
» [Java]Chương trình Client-Server sử dụng Socket ở chế độ có nối kết TCP/IP Đề tài LIST Tenthumuc
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|