

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of the java inetaddress class, focusing on its methods and usage. The class is used to work with ip addresses and hostnames. Two examples demonstrating how to use the getlocalhost(), getallbyname(), and getbyname() methods to obtain ip addresses and hostnames. It also explains the purpose of each method and its parameters.
Typology: Study notes
1 / 2
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