0% found this document useful (0 votes)
16 views4 pages

Exp 6

Ghdhdh

Uploaded by

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

Exp 6

Ghdhdh

Uploaded by

c.mayuresh58
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Advanced Java Programming TY B tech

Experiment No-6
Title: Write a program to retrieve hostname and IP address in InetAddress class.
Aim: To study different classes used to perform networking.

Theory:

Java InetAddress class

Java InetAddress class represents an IP address. The java.net.InetAddress class provides methods to get the
IP of any host name for example www.javatpoint.com, www.google.com, www.facebook.com, 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

o An IP address helps to identify a specific resource on the network using a numerical representation.
o 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:

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:

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

2. IPv6

1
Advanced Java Programming TY B tech

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:

o It has a stateful and stateless both configurations.


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

TCP/IP Protocol

o TCP/IP is a communication protocol model used connect devices over a network via internet.
o TCP/IP helps in the process of addressing, transmitting, routing and receiving the data packets over
the internet.
o 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 host) It returns the instance of InetAddress


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.

2
Advanced Java Programming TY B tech

Java URL

The Java URL class represents an URL. URL is an acronym for Uniform Resource Locator. It points to a
resource on the World Wide Web. For example:

1. https://www.javatpoint.com/java-tutorial

A URL contains many information:

1. Protocol: In this case, http is the protocol.


2. Server name or IP Address: In this case, www.javatpoint.com is the server name.
3. Port Number: It is an optional attribute. If we write http//ww.javatpoint.com:80/sonoojaiswal/ , 80
is the port number. If port number is not mentioned in the URL, it returns -1.
4. File Name or directory name: In this case, index.jsp is the file name.

Constructors of Java URL class


1. URL(String spec)
Creates an instance of a URL from the String representation.

2. URL(String protocol, String host, int port, String file)

Creates an instance of a URL from the given protocol, host, port number, and file.

3. URL(String protocol, String host, int port, String file, URLStreamHandler handler)

Creates an instance of a URL from the given protocol, host, port number, file, and handler.

4. URL(String protocol, String host, String file)

Creates an instance of a URL from the given protocol name, host name, and file name.

5. URL(URL context, String spec)

Creates an instance of a URL by parsing the given spec within a specified context.

6. URL(URL context, String spec, URLStreamHandler handler)

Creates an instance of a URL by parsing the given spec with the specified handler within a given
context.
3
Advanced Java Programming TY B tech

Commonly used methods of Java URL class


The java.net.URL class provides many methods. The important methods of URL class are given
below.

Method Description

public String getProtocol() it returns the protocol of the URL.

public String getHost() it returns the host name of the URL.

public String getPort() it returns the Port Number of the URL.

public String getFile() it returns the file name of the URL.

public String getAuthority() it returns the authority of the URL.

public String toString() it returns the string representation of the URL.

public String getQuery() it returns the query string of the URL.

public String getDefaultPort() it returns the default port of the URL.

Public URLConnection it returns the instance of URLConnection i.e. associated


openConnection() with this URL.

public boolean equals(Object obj) it compares the URL with the given object.

public Object getContent() it returns the content of the URL.

public String getRef() it returns the anchor or reference of the URL.

public URI toURI() it returns a URI of the URL.

Conclusion: In this experiment we have learnt constructors and method of InetAddress class and URL class.
Exercise:
1) Develop a program using InetAddress class to retrive IP address of computer when hostname is
entered by user.
2) Write a program using URL class to retieve the host, protocol, port and file of URL
https://sitcoe.ac.in/

You might also like