01: /**
02:  * Time-of-day server listening to port 6013.
03:  *
04:  * Figure 4.18
05:  *
06:  * @author Gagne, Galvin, Silberschatz
07:  * Operating System Concepts with Java - Sixth Edition
08:  * Copyright John Wiley & Sons - 2003.
09:  */
10:  
11: import java.net.*;
12: import java.io.*;
13: 
14: public class DateServer
15: {
16:         public static void main(String[] args) throws IOException {
17:                 Socket client = null;
18:                 ServerSocket sock = null;
19: 
20:                 try {
21:                         sock = new ServerSocket(6013);
22:                         // now listen for connections
23:                         while (true) {
24:                                 client = sock.accept();
25:                                 System.out.println("server = " + sock);
26:                                 System.out.println("client = " + client);
27: 
28:                                 // we have a connection
29:                                 PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
30:                                 // write the Date to the socket
31:                                 pout.println(new java.util.Date().toString());
32: 
33:                                 pout.close();
34:                                 client.close();
35:                         }
36:                 }
37:                 catch (IOException ioe) {
38:                                 System.err.println(ioe);
39:                 }
40:                 finally {
41:                         if (sock != null)
42:                                 sock.close();
43:                         if (client != null)
44:                                 client.close();
45:                 }
46:         }
47: }