FileDocCategorySizeDatePackage
daytimeURLConnection.javaAPI DocExample1103Thu Apr 03 15:27:06 BST 1997None

daytimeURLConnection.java

import java.net.*;
import java.io.*;

public class daytimeURLConnection extends URLConnection {

  Socket theConnection = null;
  public final static int defaultPort = 13;

  public daytimeURLConnection (URL u) {
    super(u);
  }

  public synchronized InputStream getInputStream() throws IOException {
  
    if (!connected) {
      connect();
    }
    DataInputStream dis = new DataInputStream(theConnection.getInputStream());
    String time = dis.readLine();
    String html = "<html><head><title>The Time at " +
      url.getHost() + "</title></head><body><h1>" +
      time + "</h1></body></html>";
      
    return new StringBufferInputStream(html);
    
  }

  public String getContentType() {
    return "text/html";
  }

  public Object getContent() throws IOException {
    return getInputStream();
  }
  
  public synchronized void connect() throws IOException {
  
    int port;
  
    if (!connected) {
      port = url.getPort();
      if ( port < 0) {
        port = defaultPort;
      }
      theConnection = new Socket(url.getHost(), port);
      connected = true;
    } 
  
  }

}