Lecture 29 - Java Network Programming Concepts
Lecture 29 - Java Network Programming Concepts
1
Protocols
2
● For instance, HTTP is the protocol employed by web browsers and servers to
transfer hypertext pages and images.
● It is a relatively simple protocol for basic web page browsing. Here's how it
works:
a. When a client requests a file from an HTTP server (known as a hit), it
simply sends the file's name in a specific format to a predefined port
and reads back the file's contents.
b. The server also responds with a status code to indicate whether the
request can be fulfilled and why.
IP Addresses
● An essential element of the Internet is the address, which is unique to every
computer on the network.
● An Internet address is a numerical value that serves as an identifier for each
computer.
● Initially, all Internet addresses consisted of 32-bit values organized as four
8-bit segments. This address format was specified by IPv4 (Internet Protocol,
version 4).
● However, a newer addressing scheme called IPv6 (Internet Protocol, version
6) has emerged.
● IPv6 employs a 128-bit value to represent an address, divided into eight
16-bit sections.
● IPv6 offers several advantages, but the main one is its ability to support a
significantly larger address space compared to IPv4.
● Fortunately, when using Java, you generally don't need to worry about
whether IPv4 or IPv6 addresses are used because Java handles the details for
you.
● Similar to how the numbers in an IP address indicate a network hierarchy, the
name of an Internet address, referred to as its domain name, describes the
machine's location within a naming system.
3
● For example, www.HerbSchildt.com belongs to the COM top-level domain
(reserved for commercial sites), with "HerbSchildt" being the specific name
and "www" identifying the server for web requests.
● The Domain Naming Service (DNS) maps these domain names to their
respective IP addresses, so users work with the domain name and not the IP
address.
InetAdress
● The InetAddress class is a part of the java.net package and represents an
Internet Protocol (IP) address.
● It provides functionality to work with both IPv4 and IPv6 addresses.
● The InetAddress class allows you to perform various operations related to IP
addresses, such as retrieving the host name associated with an IP address or
obtaining the IP address of a host name.
● It also enables you to check if an IP address is reachable, get the byte
representation of an IP address, and perform hostname resolution.
Methods
4
● Consider this example of using InetAddress
import java.net.InetAddress;
● In the example above, we obtain the `InetAddress` object for the host
"www.visionthondoya.com" and then retrieve the host name, IP address, and
check if it is reachable within 5 seconds.
5
2. Inet6Address: This class represents an IPv6 address, which is the next
generation of IP addresses. IPv6 addresses are 128-bit addresses expressed
in hexadecimal notation, separated by colons (e.g.,
"2001:0db8:85a3:0000:0000:8a2e:0370:7334"). The Inet6Address class
provides methods for working specifically with IPv6 addresses.
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
6
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
● By utilizing Inet4Address and Inet6Address, you can work with IPv4 and IPv6
addresses in a more specialized manner, making it easier to handle the
differences between the two address types when necessary.