import java.net.*; import java.io.*; /** The class Browse consists only of a main method that takes a web server and a web page as parameters and then retrieves the page from the server. */ public class Browse { static Socket socket; static DataInputStream inStream; static PrintStream outStream; static String hostName; static String page; static InetAddress host; /** Main takes a web server and a web page as parameters and then retrieves the page from the server. */ public static void main(String args[]) { // Terminate if wrong number of arguments if(args.length!=2 ){ System.out.println("Usage: java Browse webserver page\nI.e. java Browse www.soi.city.ac.uk /~ad225/index.html"); System.exit(1); } // the host name is the first argument hostName=args[0]; // the page the second page=args[1]; // get the IP address of the server try{ ///////////////////////////////////// // ToDo: Look up the java API for the // class InetAddress which provides // the mehtod getByName that // converts host names such as "www.soi.ac.uk" // into IP addresses like "158.123.176.68" ///////////////////////////////////// host= } catch (Exception e) { System.out.println("Failed to get server's IP address\n"+e); System.exit(1); } // Create the socket try{ ///////////////////////////////////// // ToDo: Look up the java API for the // class Socket which provides a constructor // with a host and a port as parameters. // Web servers are listening for requests on // the port number 80. ///////////////////////////////////// socket = inStream = new DataInputStream(socket.getInputStream()); outStream = new PrintStream(socket.getOutputStream()); } catch (Exception e) { System.out.println("Failed to create socket\n"+e); System.exit(1); } // Get web page try{ //send request outStream.println("GET "+page); //get reply and print it token for token to the screen StreamTokenizer st = new StreamTokenizer(inStream); st.lowerCaseMode(true); String get = ""; while(st.nextToken()!=st.TT_EOF){ if(st.ttype==st.TT_WORD) { if(st.sval.equals("br")){ System.out.println();} else { System.out.print(st.sval+" "); //System.out.println(st.toString()); } } } socket.close(); } catch (Exception e) { System.out.println("Failed to retrieve page\n"+e); System.exit(1); } } }