Answers to Exercises - Internet
  1. Write a program that checks to see what ports on a given host are active. The test can be made using this:
       Socket socket = new Socket(host, port);
       (socket.getOutputStream()).write((new Byte((byte)65)).byteValue());
    where port is an integer between 1 and 64000 and host is a String giving a host name (ex: If an exception is thrown the port is not active. Otherwise it is.

  2. Develop the above applet further so that it displays output from each port that is open. It is likely that an open port will expect a message in response to its output. In that case, add a "Next" button which allows proceeding to the next open port.

  3. Write a program that gets the weather from any desired METAR station. This service is provided by the following URL:
    where XXXX is the METAR code of the location you are interested in. For example, METAR for Lunken Airport is KLUK, for Cincinnati/Northern Kentucky airport its KCVG. This is really easy to do thanks to the URL class provided by Java. The following code gives the general idea:
       String line;
       String url = "";
       String station = "KCVG";
       URL weather = new URL(url+"/"+station+".TXT");
       InputStream is = (InputStream)weather.getContent();
       InputStreamReader isr = new InputStreamReader(is);
       BufferedReader bis = new BufferedReader(isr);
       while ((line = bis.readLine()) != null) text.append(line+"\n");
    Be sure to put this in a second, threaded class (that is, outside the applet class).