-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdviceServer.java
36 lines (31 loc) · 904 Bytes
/
AdviceServer.java
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
import java.io.*;
import java.net.*;
import java.util.Random;
public class AdviceServer {
String[] adviceList = {"H-O-T-A-P-E", "Get out of your comfort zone", "Dont waste time",
"Keep your thoughts in check", "Always respect your parents"};
public void go() {
try {
ServerSocket ssoc = new ServerSocket(4343);
while(true) {
Socket sock = ssoc.accept();
PrintWriter writer = new PrintWriter(sock.getOutputStream());
String advice = getAdvice();
writer.println(advice);
writer.close();
System.out.println(advice);
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
private String getAdvice() {
Random r = new Random();
int random = r.nextInt(adviceList.length);
return adviceList[random];
}
public static void main(String[] args) {
AdviceServer ser = new AdviceServer();
ser.go();
}
}