-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.java
More file actions
89 lines (76 loc) · 3.91 KB
/
tempCodeRunnerFile.java
File metadata and controls
89 lines (76 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.*;
import java.net.*;
import java.util.concurrent.Semaphore;
public class SensorServerCombined {
private static int countMessages = 0;
private static final Semaphore semaphore = new Semaphore(2);
public static synchronized void tangDem() {
countMessages++;
}
public static void main(String[] args) {
int port = 12345;
String host = "127.0.0.1";
try (ServerSocket serverSocket = new ServerSocket(port, 50, InetAddress.getByName(host))) {
System.out.println("Server dang cho ket noi tai " + host + ":" + port);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(() -> {
try {
semaphore.acquire();
try (
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))
) {
String input;
while ((input = in.readLine()) != null) {
tangDem();
int temp = -1;
int gas = -1;
String[] parts = input.split(" ");
for (String part : parts) {
if (part.startsWith("TEMP:")) {
try {
temp = Integer.parseInt(part.substring(5));
} catch (NumberFormatException e) {
System.out.println("Gia tri nhiet do khong hop le: " + part);
}
} else if (part.startsWith("GAS:")) {
try {
gas = Integer.parseInt(part.substring(4));
} catch (NumberFormatException e) {
System.out.println("Gia tri khi gas khong hop le: " + part);
}
}
}
System.out.print("Nhan du lieu tu sensor: " + input + " | Tong tin nhan: " + countMessages);
// LUÔN kiểm tra cảnh báo mỗi lần
boolean canhBao = false;
if (temp > 50) {
System.out.print(" | CANH BAO: Nhiet do qua cao");
canhBao = true;
}
if (gas > 8500) {
System.out.print(" | CANH BAO: Khi gas nguy hiem");
canhBao = true;
}
if (!canhBao) {
System.out.print(" | OK");
}
System.out.println(); // Xuống dòng
}
} catch (IOException e) {
System.out.println("Loi khi doc du lieu: " + e.getMessage());
} finally {
clientSocket.close();
}
} catch (InterruptedException | IOException e) {
System.out.println("Loi khi xu ly ket noi: " + e.getMessage());
} finally {
semaphore.release();
}
}).start();
}
} catch (IOException e) {
System.out.println("Khong the khoi dong server: " + e.getMessage());
}
}
}