-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientHandler.java
More file actions
80 lines (78 loc) · 3.58 KB
/
ClientHandler.java
File metadata and controls
80 lines (78 loc) · 3.58 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
package ue09_tcp_server_factorize;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.charset.Charset;
class ClientHandler {
private static final Charset CHARSET = Charset.forName("UTF-8");
private final Socket cltSocket;
private BufferedWriter out = null;
private BufferedReader in = null;
private boolean closed = false;
ClientHandler(Socket cltSocket) {
this.cltSocket = cltSocket;
System.out.println("Client: " + this.cltSocket.getInetAddress() + this.cltSocket.getPort() + " connected!");
new Thread(this::handleClient).start();
}
private void handleClient() {
try {
out = new BufferedWriter(new OutputStreamWriter(cltSocket.getOutputStream(), CHARSET));
in = new BufferedReader(new InputStreamReader(cltSocket.getInputStream(), CHARSET));
out.write("Welcome to the Factorize Server!\r\nType \"exit\" if you want to quit!.\r\n");
out.flush();
while (!closed) {
out.write("\r\nWhich Long-Number should be factorized? ");
out.flush();
String temp = in.readLine();
if (!temp.trim().isEmpty()) {
if (temp.equals("exit")) {
out.write("Bye!\n");
out.flush();
this.disconnect();
cltSocket.close();
System.out.println("Client: " + this.cltSocket.getInetAddress() + this.cltSocket.getPort() + " disconnected!");
break;
} else if (temp.equals("?")) {
if (!Server.isFinished) {
out.write("At this time " + Server.getPrimSize() + " primes were found after " + Server.currentTime + "ms (largest: " + Server.getLargestPrime() + ")");
} else {
out.write("All " + Server.getPrimSize() + " prime numbers were found after " + Server.currentTime + "ms (largest: " + Server.getLargestPrime() + ")");
}
out.flush();
} else {
try {
if (Long.parseLong(temp) > 0) {
try {
out.write(Server.factorize(Long.parseLong(temp)));
out.flush();
} catch (ArithmeticException e) {
out.write("Factorizing is not ready. Please wait a bit.");
out.flush();
}
} else {
throw new NumberFormatException("Negative Number");
}
} catch (NumberFormatException e) {
out.write("NumberFormatError: " + temp + " is not a (positive) long-number");
out.flush();
}
}
}
}
} catch (SocketException | NullPointerException e) {
System.out.println("Client: " + this.cltSocket.getInetAddress() + this.cltSocket.getPort() + " disconnected!");
} catch (IOException e) {
e.printStackTrace();
}
}
private void disconnect() {
try {
closed = true;
this.in.close();
this.out.close();
this.cltSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}