COMP2396
Loading...
Searching...
No Matches
DailyAdviceServer.java
Go to the documentation of this file.
1import java.io.*;
2import java.net.*;
3
4public class DailyAdviceServer {
5 String[] adviceList = {"Practice makes perfect", "Never give up", "Focus on the task at hand", "Don't look back", "Be yourself", "Believe in your own work"};
6 ServerSocket serverSock;
7
8 public String getAdvice() {
9 int random = (int) (Math.random() * adviceList.length);
10 return adviceList[random];
11 }
12
13 public static void main(String[] args) {
15 server.go();
16 }
17
18 public void go() {
19 try {
20 serverSock = new ServerSocket(5000);
21 while (true) {
22 Socket sock = serverSock.accept();
23 PrintWriter writer = new PrintWriter(sock.getOutputStream());
24 String advice = getAdvice();
25 writer.println(advice);
26 writer.close();
27 System.out.println(advice);
28 }
29 } catch (Exception ex) {
30 ex.printStackTrace();
31 }
32 } // close go
33
34}
static void main(String[] args)