forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDailyAdviceServer.java
More file actions
29 lines (29 loc) · 891 Bytes
/
Copy pathDailyAdviceServer.java
File metadata and controls
29 lines (29 loc) · 891 Bytes
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
import java.io.*;
import java.net.*;
public class DailyAdviceServer{
String[] adviceList={"Take smaller bites","Go for the tight jeans","One word:inappropriate","Just for today, be honest","You might want to rethink that haircut"};
public void go(){
try{
ServerSocket serverSock=new ServerSocket(4242);
while(true){
Socket sock=serverSock.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(){
int random=(int)(Math.random()*adviceList.length);
return adviceList[random];
}
public static void main(String[] args) {
DailyAdviceServer server=new DailyAdviceServer();
server.go();
}
}