Home | | Internet Programming | InetAddress Class

Chapter: Web or internet Programming : Java Programming

InetAddress Class

The java.net.InetAddress class is Java's encapsulation of an IP address. It is used by most of the other networking classes, including Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more.

InetAddress Class

 

       The java.net.InetAddress class is Java's encapsulation of an IP address. It is used by most of the other networking classes, including Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more.

public final class InetAddress extends Object implements Serializable

 

       There are no public constructors in the InetAddress class. However, InetAddress has three static methods that return suitably initialized InetAddress objects, given a little information. They are:

public static InetAddress InetAddress.getByName(String hostName)

 

throws UnknownHostException public static InetAddress[] InetAddress.getAllByName(String hostName)

 

throws UnknownHostException public static InetAddress InetAddress.getLocalHost( )

throws UnknownHostException

 

Example

1. Program that prints the address of www.oreilly.com

 

import java.net.*;

 

public class OReillyByName {

 

public static void main (String[] args) { try {

 

InetAddress address = InetAddress.getByName("www.oreilly.com");

System.out.println(address);

}

 

catch (UnknownHostException e) { System.out.println("Could not find www.oreilly.com");

}

}

 

}

 

% java OReillyByName www.oreilly.com/204.148.40.9

 

2. Program that prints the address of 204.148.40.9

 

import java.net.*;

public class OReillyByAddress {

 

public static void main (String[] args) { try {

 

InetAddress address = InetAddress.getByName("204.148.40.9");

System.out.println (address);

}

catch (UnknownHostException e) {

 

System.out.println ("Could not find 204.148.40.9");

}}}

% java OReillyByAddress helio.ora.com/204.148.40.9

 

3. Program that prints all the addresses of www.microsoft.com

 

import java.net.*;

 

public class AllAddressesOfMicrosoft { public static void main (String[] args) {

try {

 

InetAddress[] addresses = InetAddress.getAllByName("www.microsoft.com"); for (int i = 0; i < addresses.length; i++) {

System.out.println(addresses[i]);

 

}

}

 

catch (UnknownHostException e) {

System.out.println("Could not find www.microsoft.com");

 

}

}

 

}

 

% java AllAddressesOfMicrosoft www.microsoft.com/207.46.131.15 www.microsoft.com/207.46.131.137 www.microsoft.com/207.46.130.14 www.microsoft.com/207.46.130.149 www.microsoft.com/207.46.130.150 www.microsoft.com/207.46.131.13

 

4. Find the address of the local machine

 

import java.net.*;

public class MyAddress {

 

public static void main (String[] args) { try {

 

InetAddress address = InetAddress.getLocalHost( );

System.out.println(address);

}

catch (UnknownHostException e) {

 

System.out.println("Could not find this computer's address.");

}

 

}

}

if ran the program on titan.oit.unc.edu:

% java MyAddress titan.oit.unc.edu/152.2.22.14


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Java Programming : InetAddress Class |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.