0% found this document useful (0 votes)
3 views6 pages

#6java InetAddress Class - Javatpoint

The Java InetAddress class represents an IP address and provides methods to retrieve the IP of any host name. It supports both IPv4 and IPv6 addressing schemes, with IPv4 being a 32-bit protocol and IPv6 offering a 128-bit address space. The class includes methods for obtaining local host information and demonstrates various functionalities through example code.

Uploaded by

Farhan Mansuri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

#6java InetAddress Class - Javatpoint

The Java InetAddress class represents an IP address and provides methods to retrieve the IP of any host name. It supports both IPv4 and IPv6 addressing schemes, with IPv4 being a 32-bit protocol and IPv6 offering a 128-bit address space. The class includes methods for obtaining local host information and demonstrates various functionalities through example code.

Uploaded by

Farhan Mansuri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

4/13/23, 8:40 PM Java InetAddress class- javatpoint

 [Link]

 4 min read

Java InetAddress class- javatpoint


next → ← prev

Java InetAddress class represents an IP address. The [Link] class


provides methods to get the IP of any host name for example [Link],
[Link], [Link], etc.

An IP address is represented by 32-bit or 128-bit unsigned number. An instance of


InetAddress represents the IP address with its corresponding host name. There are two
types of addresses: Unicast and Multicast. The Unicast is an identifier for a single
interface whereas Multicast is an identifier for a set of interfaces.

Moreover, InetAddress has a cache mechanism to store successful and unsuccessful


host name resolutions.

IP Address
An IP address helps to identify a specific resource on the network using a numerical
representation.
Most networks combine IP with TCP (Transmission Control Protocol). It builds a
virtual bridge among the destination and the source.

There are two versions of IP address:

[Link] 1/6
4/13/23, 8:41 PM Java InetAddress class- javatpoint

Video Player is loading.

Current Time 0:00

Duration 18:10

Loaded: 0.37%

Stream Type LIVE

Remaining Time 18:10

1. IPv4

IPv4 is the primary Internet protocol. It is the first version of IP deployed for production in
the ARAPNET in 1983. It is a widely used IP version to differentiate devices on network
using an addressing scheme. A 32-bit addressing scheme is used to store 232 addresses
that is more than 4 million addresses.

Features of IPv4:

It is a connectionless protocol.
It utilizes less memory and the addresses can be remembered easily with the class
based addressing scheme.
It also offers video conferencing and libraries.

2. IPv6

IPv6 is the latest version of Internet protocol. It aims at fulfilling the need of more internet
addresses. It provides solutions for the problems present in IPv4. It provides 128-bit
address space that can be used to form a network of 340 undecillion unique IP
addresses. IPv6 is also identified with a name IPng (Internet Protocol next generation).

Features of IPv6:

[Link] 2/6
4/13/23, 8:41 PM Java InetAddress class- javatpoint

It has a stateful and stateless both configurations.


It provides support for quality of service (QoS).
It has a hierarchical addressing and routing infrastructure.

TCP/IP Protocol
TCP/IP is a communication protocol model used connect devices over a network via
internet.
TCP/IP helps in the process of addressing, transmitting, routing and receiving the
data packets over the internet.
The two main protocols used in this communication model are:
1. TCP i.e. Transmission Control Protocol. TCP provides the way to create a
communication channel across the network. It also helps in transmission of
packets at sender end as well as receiver end.
2. IP i.e. Internet Protocol. IP provides the address to the nodes connected on
the internet. It uses a gateway computer to check whether the IP address is
correct and the message is forwarded correctly or not.

Java InetAddress Class Methods

Method Description

public static InetAddress getByName(String It returns the instance of InetAddress


host) throws UnknownHostException containing LocalHost IP and name.

public static InetAddress getLocalHost() throws It returns the instance of InetAdddress


UnknownHostException containing local host name and address.

public String getHostName() It returns the host name of the IP address.

public String getHostAddress() It returns the IP address in string format.

Example of Java InetAddress Class

Let's see a simple example of InetAddress class to get ip address of [Link]


website.

[Link] 3/6
4/13/23, 8:41 PM Java InetAddress class- javatpoint

[Link]

1. import [Link].*;
2. import [Link].*;
3. public class InetDemo{
4. public static void main(String[] args){
5. try{
6. InetAddress ip=[Link]("[Link]");
7.
8. [Link]("Host Name: "+[Link]());
9. [Link]("IP Address: "+[Link]());
10. }catch(Exception e){[Link](e);}
11. }
12. }

Test it Now

Output:

Host Name: [Link]


IP Address: [Link]

Program to demonstrate methods of InetAddress class

[Link]

1. import [Link].Inet4Address;
2. import [Link];
3. import [Link];
4. public class InetDemo2
5. {
6. public static void main(String[] arg) throws Exception
7. {
8. InetAddress ip = [Link]("[Link]");
9. InetAddress ip1[] = [Link]("[Link]");
10. byte addr[]={72, 3, 2, 12};
11. [Link]("ip : "+ip);
[Link] 4/6
4/13/23, 8:41 PM Java InetAddress class- javatpoint

12. [Link]("\nip1 : "+ip1);


13. InetAddress ip2 = [Link](addr);
14. [Link]("\nip2 : "+ip2);
15. [Link]("\nAddress : " +[Link]([Link]()));
16. [Link]("\nHost Address : " +[Link]());
17. [Link]("\nisAnyLocalAddress : " +[Link]());
18. [Link]("\nisLinkLocalAddress : " +[Link]());
19. [Link]("\nisLoopbackAddress : " +[Link]());
20. [Link]("\nisMCGlobal : " +[Link]());
21. [Link]("\nisMCLinkLocal : " +[Link]());
22. [Link]("\nisMCNodeLocal : " +[Link]());
23. [Link]("\nisMCOrgLocal : " +[Link]());
24. [Link]("\nisMCSiteLocal : " +[Link]());
25. [Link]("\nisMulticastAddress : " +[Link]());
26. [Link]("\nisSiteLocalAddress : " +[Link]());
27. [Link]("\nhashCode : " +[Link]());
28. [Link]("\n Is ip1 == ip2 : " +[Link](ip2));
29. }
30. }

Output:

[Link] 5/6
4/13/23, 8:41 PM Java InetAddress class- javatpoint

In the above Java code, the various boolean methods of InetAdress class are
demonstrated.

Next TopicDatagramSocket And DatagramPacket

← prev next →

Generated with Reader Mode

[Link] 6/6

You might also like