

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


import java.net.*; public class SampleInetAddress { public static void main(String [] args){ try { InetAddress local = InetAddress.getLocalHost( ); System.out.println("Name: " + local.getHostName( ) + "\t IP address: " + local.getHostAddress( ) ); } catch ( UnknownHostException e) { System.out.println("Error: " + e); } }//end of main }//end of class definition
EXAMPLE 2:
import java.net.*; public class DNSLookup {
public static void main(String [] args){ if (args.length != 1){ System.out.println("Usage: DNSLookup |
String findAddress = args[0]; boolean isReverseLookup = Character.isDigit(findAddress.charAt(0)); try { InetAddress [] allAddress = InetAddress.getAllByName(findAddress);
for (int k=0; k<allAddress.length; k++){
InetAddress address = allAddress[k]; if (isReverseLookup) System.out.println("Address: " + address.getHostAddress( ) + "\t resolves to: " + address.getHostName( ) ); else System.out.println("Hostname: " + address.getHostName( ) + "\t resolves to: " + address.getHostAddress( ) ); }//end of for } catch ( UnknownHostException e) {
System.out.println("Error: " + e); System.exit(1); } catch (java.lang.SecurityException se){
System.out.println("Socket permission:" + se); System.exit(1); } }//end of main
}//end of class definition