2 * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
4 * Permission to use, copy, modify, and distribute this software
5 * and its documentation for NON-COMMERCIAL purposes and without
6 * fee is hereby granted provided that this copyright notice
7 * appears in all copies. Please refer to the file "copyright.html"
8 * for further important copyright and licensing information.
10 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
11 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
12 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
13 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
14 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
15 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
21 public class QuoteServerThread extends Thread {
23 protected DatagramSocket socket = null;
24 protected BufferedReader in = null;
25 protected boolean moreQuotes = true;
27 public QuoteServerThread() throws IOException {
28 this("QuoteServerThread");
31 public QuoteServerThread(String name) throws IOException {
35 socket = new DatagramSocket(4445);
36 } catch (Exception exc) {
37 System.err.println("Could not create socket: " + exc);
41 in = new BufferedReader(new FileReader("one-liners.txt"));
42 } catch (FileNotFoundException e) {
43 System.err.println("Could not open quote file. Serving time instead.");
51 byte[] buf = new byte[256];
54 DatagramPacket packet = new DatagramPacket(buf, buf.length);
55 socket.receive(packet);
57 // figure out response
58 String dString = null;
60 dString = new Date().toString();
62 dString = getNextQuote();
63 buf = dString.getBytes();
65 // send the response to the client at "address" and "port"
66 InetAddress address = packet.getAddress();
67 int port = packet.getPort();
68 packet = new DatagramPacket(buf, buf.length, address, port);
70 } catch (IOException e) {
78 protected String getNextQuote() {
79 String returnValue = null;
81 if ((returnValue = in.readLine()) == null) {
84 returnValue = "No more quotes. Goodbye.";
86 } catch (IOException e) {
87 returnValue = "IOException occurred in server.";