You are on page 1of 12

PHẦN 3 (7.

5 ĐIỂM) – Tập trung phòng máy


11. (3.000 Points)
Để truy cập vào hệ thống, Client phải nhập User và Password của mình và gởi cho
Server xác thực. Quá trình xác thực trên Server diễn ra như sau :
- Nếu Client nhập User (hoặc Password) không đúng, Server trả về cho
Client thông báo “ User (hoặc Password) của bạn không đúng, yêu cầu nhập
lại”.
- Nếu sau 3 lần đăng nhập liên tiếp vẫn không đúng, Server trả về cho Client
thông báo “ Bạn đã nhập sai 3 lần, bạn đã hết quyền truy cập vào hệ thống.
“ Hệ thống đóng kết nối.
- Nếu Client nhập User và Password đúng, Server trả về cho Client thông báo
“Bạn đã truy cập thành công “.
Biết rằng, User và Password ở Server được mặc định là :
User =”CS420”, Password =”123”
Yêu cầu:
Viết chương trình hiển thị kết quả trên màn hình Client theo kỹ thuật UDP trong các
trường hợp sau :
1.Dãy số nguyên nhập từ bàn phím trên cùng dòng, mỗi giá trị cách nhau bởi dấu “;”
2. Xử lý trường hợp quá trình nhập sẽ dừng lại cho đến khi Client nhập vào chữ “
stop”

CLIENT
package Review.Login;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Scanner;

public class Client {

private final InetAddress host;


private final int port;

private final Scanner scanner = new Scanner(System.in);


public final static byte[] BUFFER = new byte[40960];

public Client(InetAddress host, int port) {


this.host = host;
this.port = port;
}

private void execute() throws IOException {


try (DatagramSocket client = new DatagramSocket()) {
System.out.println("Client started ");
while (true) {
int count_ = 0;
do {
//enter data
ArrayList<String> temp = enterData();
String userName = temp.get(0);
String password = temp.get(1);
count_++;

byte[] data = (userName + ";" + password + ";" +


count_).getBytes();
DatagramPacket dp = new DatagramPacket(data,
data.length, host, port);
client.send(dp);

DatagramPacket incoming = new DatagramPacket(BUFFER,


BUFFER.length);
client.receive(incoming);
System.out.println(new String(incoming.getData(), 0,
incoming.getLength()));
} while (count_ < 3);
}
} catch (IOException e) {
System.err.println(e);
}
}

private ArrayList<String> enterData() {


String userName, password;

userName = scanner.nextLine();
password = scanner.nextLine();

ArrayList<String> strings = new ArrayList<>();


strings.add(userName);
strings.add(password);

return strings;
}

public static void main(String[] args) throws IOException {


Client client = new Client(InetAddress.getByName("localhost"),
2022);
client.execute();
}
}

SERVER
package Review.Login;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {


private final int port;
public final static byte[] BUFFER = new byte[40960];

public Server(int port) {


this.port = port;
}

private void execute() throws IOException {


DatagramSocket ds = null;
try {
System.out.println("Binding to port " + port + ", please
wait ...");
ds = new DatagramSocket(port);
System.out.println("Server started ");
System.out.println("Waiting for messages from Client ... ");

while (true) {
DatagramPacket incoming = new DatagramPacket(BUFFER,
BUFFER.length);
ds.receive(incoming);

String message = new String(incoming.getData(), 0,


incoming.getLength());
System.out.println("Received: " + message);

String userName = message.split(";")[0];


String password = message.split(";")[1];

int count = Integer.parseInt(message.split(";")[2]);

do {
//Check data
if (checkData(userName, password)) {
message = "Login Successfully";
} else {
if (count < 3) {
message = "Username or Password incorrect";
} else {
message = "Incorrect 3 times";
ds.close();
}
}
} while (ds.isConnected());

DatagramPacket out = new DatagramPacket(message.getBytes(),


incoming.getLength(),
incoming.getAddress(), incoming.getPort());
ds.send(out);

System.out.println(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
System.out.println("DISCONNECTED CLIENT");
}

private boolean checkData(String userName, String password) {


return userName.equals("CS420") && password.equals("123");
}

public static void main(String[] args) throws IOException {


Server server = new Server(2022);
server.execute();
}
}
12. (4.500 Points)
Client tạo một dãy số nguyên gồm 3 số a,b,c, sau đó gởi cho Server, yêu cầu Server
giải phương trình có dạng: ax2+bx + c = 0.
Hiển thị nghiệm của phương trình này trên màn hình Client.
Yêu cầu:
Viết chương trình hiển thị kết quả trên màn hình Client theo kỹ thuật TCP trong các
trường hợp sau :
1. Các giá trị a, b, c được nhập từ bàn phím trên cùng dòng, mỗi giá trị cách nhau bởi
dấu “;”
2. Xử lý trường hợp quá trình nhập sẽ dừng lại cho đến khi Client nhập vào chữ “
stop”

CLIENT:
package Review.PerfectNum;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client {

private final InetAddress host;


private final int port;

private final Scanner scanner = new Scanner(System.in);

public Client(InetAddress host, int port) {


this.host = host;
this.port = port;
}

private void execute() throws IOException {


Socket client = new Socket(host, port);
DataInputStream dataInputStream = new
DataInputStream(client.getInputStream());
DataOutputStream dataOutputStream = new
DataOutputStream(client.getOutputStream());

// Enter data
String s = enterData();
dataOutputStream.writeUTF(s);

System.out.println(dataInputStream.readUTF());
}

private String enterData() {


String s = "";
while (true) {
String temp = scanner.nextLine();
if (temp.equals("stop"))
break;
s += temp;
}
return s;
}

public static void main(String[] args) throws IOException {


Client client = new Client(InetAddress.getByName("localhost"),
2022);
client.execute();
}
}

SERVER:
package Review.QuadraticEquation;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import static java.lang.Math.abs;


import static java.lang.Math.sqrt;

public class Server {


private final int port;

public Server(int port) {


this.port = port;
}

private void execute() throws IOException {


System.out.println("Server is listening...");

//Server config
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
DataInputStream dataInputStream = new
DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new
DataOutputStream(socket.getOutputStream());

String s = dataInputStream.readUTF();
System.out.println(s);
String[] arr = getData(s);
String res = calculateRoots(Integer.parseInt(arr[0]),
Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));
dataOutputStream.writeUTF(res);
}

private String[] getData(String s) {


return s.trim().split(";");
}

private String calculateRoots(int a, int b, int c) {


if (a == 0) {
System.out.println("The value of a cannot be 0.");
return "FAIL";
}
int d = b * b - 4 * a * c;
double sqrtVal = sqrt(abs(d));
if (d > 0) {
return (-b + sqrtVal) / (2 * a) + "\n" + (-b - sqrtVal) / (2 *
a);
} else if (d == 0) {
return (-(double) b / (2 * a) + "\n" + -(double) b / (2 * a));
} else {
return (-(double) b / (2 * a) + " + i" + sqrtVal + "\n" + -
(double) b / (2 * a) + " - i" + sqrtVal);
}
}

public static void main(String[] args) throws IOException {


Server server = new Server(2022);
server.execute();
}
}
11. (3.000 Points)
Client tạo một dãy số nguyên gồm 3 số a,b,c, sau đó gởi cho Server, yêu cầu Server
giải phương trình có dạng: ax2+bx + c = 0.
Hiển thị nghiệm của phương trình này trên màn hình Client.
Yêu cầu:
Viết chương trình hiển thị kết quả trên màn hình Client theo kỹ thuật UDP trong các
trường hợp sau :
1. Các giá trị a, b, c được nhập từ bàn phím trên cùng dòng, mỗi giá trị cách nhau bởi
dấu “;”
2. Xử lý trường hợp quá trình nhập sẽ dừng lại cho đến khi Client nhập vào chữ “
stop”

CLIENT
package Review.QuadraticEquationUDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Client {

private final InetAddress host;


private final int port;

private final Scanner scanner = new Scanner(System.in);


public final static byte[] BUFFER = new byte[40960];

public Client(InetAddress host, int port) {


this.host = host;
this.port = port;
}

private void execute() throws IOException {


try (DatagramSocket client = new DatagramSocket()) {
System.out.println("Client started ");
while (true) {
// Enter data
String s = enterData();
byte[] data = s.getBytes();

DatagramPacket dp = new DatagramPacket(data, data.length,


host, port);
client.send(dp);

DatagramPacket incoming = new DatagramPacket(BUFFER,


BUFFER.length);
client.receive(incoming);
System.out.println(new String(incoming.getData(), 0,
incoming.getLength()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String enterData() {
String s = "";
while (true) {
String temp = scanner.nextLine();
if (temp.equals("stop"))
break;
s += temp;
}
return s;
}

public static void main(String[] args) throws IOException {


Client client = new Client(InetAddress.getByName("localhost"),
2022);
client.execute();
}
}

SERVER
package Review.QuadraticEquationUDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import static java.lang.Math.abs;


import static java.lang.Math.sqrt;

public class Server {


private final int port;
public final static byte[] BUFFER = new byte[40960];

public Server(int port) {


this.port = port;
}

private void execute() throws IOException {


DatagramSocket ds = null;
try {
System.out.println("Binding to port " + port + ", please
wait ...");
ds = new DatagramSocket(port);
System.out.println("Server started ");
System.out.println("Waiting for messages from Client ... ");

while (true) {
DatagramPacket incoming = new DatagramPacket(BUFFER,
BUFFER.length);
ds.receive(incoming);

String message = new String(incoming.getData(), 0,


incoming.getLength());
System.out.println("Received: " + message);

String[] arr = getData(message);


String res = calculateRoots(Integer.parseInt(arr[0]),
Integer.parseInt(arr[1]), Integer.parseInt(arr[2]));

DatagramPacket out = new DatagramPacket(res.getBytes(),


incoming.getLength(),
incoming.getAddress(), incoming.getPort());
ds.send(out);

System.out.println(res);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ds != null) {
ds.close();
}
}
System.out.println("DISCONNECTED CLIENT");
}

private String[] getData(String s) {


return s.trim().split(";");
}

private String calculateRoots(int a, int b, int c) {


if (a == 0) {
System.out.println("The value of a cannot be 0.");
return "FAIL";
}
int d = b * b - 4 * a * c;
double sqrtVal = sqrt(abs(d));
if (d > 0) {
return (-b + sqrtVal) / (2 * a) + "\n" + (-b - sqrtVal) / (2 *
a);
} else if (d == 0) {
return (-(double) b / (2 * a) + "\n" + -(double) b / (2 * a));
} else {
return (-(double) b / (2 * a) + " + i" + sqrtVal + "\n" + -
(double) b / (2 * a) + " - i" + sqrtVal);
}
}

public static void main(String[] args) throws IOException {


Server server = new Server(2022);
server.execute();
}
}

11. (4.500 Points)


Client tạo một dãy số nguyên, sau đó gởi cho Server, yêu cầu Server cho biết những
số nào trong dãy là số hoàn thiện, ở số hạng thứ mấy trong dãy. Biết rằng số hoàn
thiện là số tự nhiên mà tổng tất cả các ước tự nhiên của nó trừ chính nó thì bằng chính
nó.
Ví dụ: 6 =1+2+3  6 là số hoàn thiện
Yêu cầu:
Viết chương trình hiển thị kết quả trên màn hình Client theo kỹ thuật TCP trong các
trường hợp sau:
1. Dãy số nguyên nhập từ bàn phím trên cùng dòng, mỗi giá trị cách nhau bởi dấu “;”
2. Xử lý trường hợp quá trình nhập sẽ dừng lại cho đến khi Client nhập vào chữ “
stop”.

CLIENT:
package Review.PerfectNum;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client {

private final InetAddress host;


private final int port;

private final Scanner scanner = new Scanner(System.in);

public Client(InetAddress host, int port) {


this.host = host;
this.port = port;
}

private void execute() throws IOException {


Socket client = new Socket(host, port);
DataInputStream dataInputStream = new
DataInputStream(client.getInputStream());
DataOutputStream dataOutputStream = new
DataOutputStream(client.getOutputStream());

// Enter data
String s = enterData();
dataOutputStream.writeUTF(s);

System.out.println(dataInputStream.readUTF());
}

private String enterData() {


String s = "";
while (true) {
String temp = scanner.nextLine();
if (temp.equals("stop"))
break;
s += temp;
}
return s;
}

public static void main(String[] args) throws IOException {


Client client = new Client(InetAddress.getByName("localhost"),
2022);
client.execute();
}
}

SERVER:
package Review.PerfectNum;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;

public class Server {


private final int port;

public Server(int port) {


this.port = port;
}

private void execute() throws IOException {


System.out.println("Server is listening...");

//Server config
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
DataInputStream dataInputStream = new
DataInputStream(socket.getInputStream());
DataOutputStream dataOutputStream = new
DataOutputStream(socket.getOutputStream());

String s = dataInputStream.readUTF();
System.out.println(s);
String[] arr = getData(s);
ArrayList<Integer> integers = checkData(arr);

dataOutputStream.writeUTF(integers.toString());
}

private String[] getData(String s) {


return s.trim().split(";");
}

private ArrayList<Integer> checkData(String[] s) {


ArrayList<Integer> ints = new ArrayList<>();
for (int i = 0; i < s.length; i++) {
if (isPerfect(Integer.parseInt(s[i]))) {
ints.add(i);
}
}
return ints;
}

private boolean isPerfect(int n) {


if (n == 1)
return false;

int sum = 1;

for (int i = 2; i * i <= n; i++) {


if (n % i == 0) {
if (i * i == n)
sum += i;
else
sum += i + (n / i);
}
}

return sum == n;
}

public static void main(String[] args) throws IOException {


Server server = new Server(2022);
server.execute();
}
}

You might also like