Câu 2: Thiết kế một Server phục vụ ở chế độ song song có nối kết. Trong đó:
+ Server làm nhiệm vụ kiểm tra xem ký tự a có xuất hiện trong chuỗi hay không.
+ Client sẽ nhập vào một chuỗi, gửi qua Server, nhận kết quả trả về từ Server
và thể hiện lên màn hình là “Có” hay “Không”.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package kiemthu_cau2;
import java.net.*;
import java.io.*;
import java.util.Scanner;
/**
*
* @author PTVLNE
*/
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try{
Socket s = new Socket("127.0.0.1",7);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Nhap chuoi");
String str = scanner.nextLine();
dos.writeUTF(str);
boolean data= dis.readBoolean();
if (data) {
System.out.println("Chuoi co ky tu 'a' ");
} else {
System.out.println("Chuoi khong co ky tu 'a' ");
}
}catch (IOException e){
System.out.println("Chuoi khong co ky tu 'a'");
}
finally{
s.close();
System.out.println("dong ket noi");
}
}catch (IOException e){
System.out.println("Loi ket noi"+e.getMessage());
}
}
}