/** DaytimeModel.java is the MVC model for a client * for the TCP/IP daytime service. * @author Joel Adams, July 2003. */ import java.net.*; // Socket, InetAddress import java.io.*; // BufferedReader, ... class DaytimeModel { private final int PORT = 13; private Socket mySocket; private BufferedReader myReader; private String myHost; public DaytimeModel(String host) { myHost = host; try { InetAddress addr = InetAddress.getByName(host); mySocket = new Socket(addr, PORT); myReader = new BufferedReader( new InputStreamReader( mySocket.getInputStream() )); } catch( Exception e ) { e.printStackTrace(); } } public String receive() { String result = ""; if (myReader != null) { try { String temp; while (true) { temp = myReader.readLine(); if (temp == null) break; result += temp; } } catch ( Exception e ) { e.printStackTrace(); } } else { result = "Unable to contact host '" + myHost + "'"; } return result; } public void finalize() { try { mySocket.close(); } catch ( Exception e ) { e.printStackTrace(); } } }