// File: IO13.java (Module 13) // // Author: Rahul Simha // Created: Nov 30, 1998 // // Downloading a web page. import java.net.*; import java.io.*; public class IO13 { public static void main (String[] argv) { try { // Open a socket to a known webserver. Socket soc = new Socket ("www.yahoo.com", 80); InetAddress remoteMachine = soc.getInetAddress(); System.out.println ("Connection made to " + remoteMachine); // Now create the output and input streams OutputStream outStream = soc.getOutputStream (); PrintWriter pw = new PrintWriter (outStream); InputStream inStream = soc.getInputStream(); InputStreamReader isr = new InputStreamReader (inStream); LineNumberReader lnr = new LineNumberReader (isr); // Ask for the index.html page. pw.print ("GET index.html /HTTP/1.0\n\n"); pw.flush (); // Get the page. String s = lnr.readLine (); while (s != null) { System.out.println (s); s = lnr.readLine (); } // Close the streams. pw.close (); lnr.close (); soc.close (); } catch (IOException e) { System.out.println (e); } } }