Java and The Net
Java and The Net
Presented by:-
Sanskruti Gaikwad
Nidhi Pathak
Aakanksha Khambayat
Aayush Nalawade
Renuka Garad
java.net package:-
Java's networking capabilities are an integral part of its platform,
enabling communication between applications over the network.
The java.net package provides a rich set of classes and interfaces
that simplify network programming tasks such as connecting to
web resources, sending and receiving data over networks, and
implementing both client-side and server-side applications.
Overview of Java Networking Classes and Interfaces The key
classes and interfaces in the java.net package provide different
levels of abstraction for networking tasks.
Here’s a breakdown of the most
important classes and interfaces:
1) InetAddress:
• Purpose:
The InetAddress class represents an IP address (either IPv4 or IPv6) and
is used for both local and remote host address resolution. It provides
methods to get the host name, the IP address as a string, and to check if
the address is reachable.
• Common Methods:
1. InetAddress.getByName(String host): Returns an InetAddress object
for the specified hostname.
2. InetAddress.getLocalHost(): Returns the InetAddress object for the
local machine.
3. InetAddress.isReachable(int timeout): Checks if the host is reachable
within the specified timeout in milliseconds.
Lets understand with a code:-
import java.net.InetAddress;
public class InetAddressExample {
public static void main(String[] args) {
try {
// Get the IP address of a specific website
InetAddress address = InetAddress.getByName("www.google.com");
System.out.println("Hostname: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
// Get the IP address of the local machine
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println("Local Hostname: " + localAddress.getHostName());
System.out.println("Local IP Address: " + localAddress.getHostAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output of the code:-
Hostname: www.google.com
IP Address: 142.250.183.68 (this may vary depending on location)
Local Hostname: my-computer-name
Local IP Address: 192.168.1.10 (this may vary based on your
network)
Explanation:-
1. InetAddress.getByName("www.google.com"): Resolves the IP
address of google.com.
2. InetAddress.getLocalHost(): Retrieves the IP address and
hostname of the local machine running the code.
2)URL (Uniform Resource Locator):
• Purpose:
The URL class represents a pointer to a resource on the web (like
a webpage or file). It provides methods to break down and
analyze different parts of the URL, such as the protocol, host,
port, and file path. URLs can be used to create connections to
resources over HTTP, FTP, and other protocols.
• Common Methods:
URL.getProtocol(): Returns the protocol of the URL (e.g., http,
https).
URL.getHost(): Returns the hostname in the
URL.URL.openStream(): Opens an input stream to read data from
the URL resource.
Lets understand with a code:-
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
String inputLine;
System.out.println("\nReading data from URL:");
while ((inputLine = reader.readLine()) != null) {
System.out.println(inputLine); // Print each line of the web page
content
}