// File: TrivialWebBrowser.java (Module 13) // // Author: Rahul Simha // Created: Nov 30, 2010 // // Downloading a web page. import java.net.*; import java.io.*; public class TrivialWebBrowser { public static void main (String[] argv) { try { // Open a socket to a known webserver. //Socket soc = new Socket ("localhost", 40013); Socket soc = new Socket ("unix.seas.gwu.edu", 40013); InetAddress remoteMachine = soc.getInetAddress(); System.out.println ("Connection made to " + remoteMachine); // Now create the output and input streams. Start with output. OutputStream outStream = soc.getOutputStream (); PrintWriter pw = new PrintWriter (outStream); // Next: input. InputStream inStream = soc.getInputStream(); InputStreamReader isr = new InputStreamReader (inStream); LineNumberReader lnr = new LineNumberReader (isr); // Ask for the index.html page. pw.println ("GET index.html"); pw.flush (); // Get the page and spew it out to the screen. 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); } } }