Net Class Examples                         Peter Komisar
 


// a few examples of calls from the InetAddress and the URL class

import java.net.*;
import java.io.*;
class netT{
  public static void main(String args[]){
    DataInputStream dis;
    try{

       // InetAddress has a lot of static methods. It is recommended you reference them via the class name

      InetAddress iaLH=InetAddress.getLocalHost( );
      System.out.println(iaLH);

     InetAddress iaBN=InetAddress.getByName("www.abcnews.com");
     System.out.println(iaBN);

     InetAddress[] iaALL= InetAddress.getAllByName("www.zdnet.com");
     for(int i=0;i<iaALL.length;i++)
         System.out.println(iaALL[i]);

      URL u=new URL("http://home.netscape.com");
      dis=new DataInputStream(u.openConnection().getInputStream());
 
     String s;
      while( ( s=dis.readLine()) !=null ){
      RandomAccessFile raf=new RandomAccessFile("X.file","rw");
      raf.writeBytes(s);
      }
     // inserted  this in to make a file of whatever was read into String s

 
     System.out.println("   File: "    + u.getFile( ) +
                       "\n Host: "     + u.getHost( ) +
                       "\n Port: "     + u.getPort( ) +
                       "\n Protocol: " + u.getProtocol( ) +
                       "\n hashCode: " + u.hashCode( )
                       );

  }
  // you have to catch this exception for InetAddress class
  catch(UnknownHostException uhe){
    System.out.println("Unknown Host");
    }
   catch(MalformedURLException mue){
    System.out.println("malformed url");
    }
   catch(IOException ioe){
   System.out.println("IO exception");
   }
  }
}



Q.1 In a table, associate the class, method call and the result of each action as it is displayed on the console.
Q.2 What is in the contents of X.file?
Q.3 What part of a html page is represented here?
Q.4 What would it take to make it loadable in a browser.