Networking basics
Sockets and ports
● At the center of Java's networking support is the idea of a socket.
● A socket represents an endpoint in a network.
● Sockets are fundamental to modern networking because they allow a single
computer to handle multiple clients simultaneously and handle different
types of information.
● This is achieved through the use of ports, which are numbered sockets on a
specific machine.
● A server process "listens" to a port until a client establishes a connection with
it.
● A server can accept multiple clients connected to the same port number, but
each session is unique.
● To manage multiple client connections, a server process must be
multithreaded or utilize other means of handling simultaneous input/output
(I/O).
1
Protocols
● Socket communication relies on a protocol.
● The Internet Protocol (IP) is a low-level routing protocol that breaks data into
small packets and sends them across a network to an address.
● However, it does not guarantee the delivery of these packets to the
destination.
● The Transmission Control Protocol (TCP) is a higher-level protocol that
effectively assembles these packets, sorting and retransmitting them as
needed to ensure reliable data transmission.
● User Datagram Protocol (UDP) is another protocol that works alongside TCP
and can be used directly to facilitate fast, connectionless, and unreliable
packet transport.
● Once a connection is established, a higher-level protocol comes into play,
depending on the port being used.
● TCP/IP reserves the lower 1,024 ports for specific protocols.
● For instance, port number 21 is designated for FTP, 23 for Telnet, 25 for
e-mail, 43 for whois, 80 for HTTP, 119 for netnews, and so on.
● Each protocol determines how a client should interact with the port.
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.
Java networking classes
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
1. getByName(String host): Returns an `InetAddress` object representing the IP
address of the specified host.
2. getLocalHost(): Returns the `InetAddress` object representing the IP address
of the local host.
3. getHostAddress(): Returns the IP address in the textual format (e.g.,
"192.168.0.1").
4. getHostName(): Returns the host name associated with the IP address.
5. isReachable(int timeout): Checks if the IP address is reachable within the
specified timeout.
6. getAddress(): Returns the IP address as a byte array.
7. toString(): Returns a string representation of the IP address.
4
● Consider this example of using InetAddress
import java.net.InetAddress;
public class InetAddressExample {
public static void main(String[] args) {
try {
InetAddress address =
InetAddress.getByName("www.visionthondoya.com");
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
System.out.println("Reachable: " + address.isReachable(5000));
} catch (Exception e) {
e.printStackTrace();
}
}
}
● 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.
Inet4Address and Inet6Address
● Inet4Address and Inet6Address are subclasses of the InetAddress class in
Java that specifically represent IPv4 and IPv6 addresses, respectively.
1. Inet4Address: This class represents an IPv4 address, which is the most
commonly used version of IP addresses. IPv4 addresses are 32-bit addresses
expressed in dotted-decimal notation (e.g., "192.168.0.1"). The Inet4Address
class provides methods for working specifically with IPv4 addresses.
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.
● Both Inet4Address and Inet6Address inherit the methods and functionality of
the InetAddress class and add additional methods specific to their respective
address types.
● These subclasses allow for more specialized handling of IPv4 and IPv6
addresses, while still benefiting from the common methods provided by the
InetAddress class.
● Here's an example that demonstrates the usage of Inet4Address and
Inet6Address:
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPAddressExample {
public static void main(String[] args) {
try {
InetAddress address =
InetAddress.getByName("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
if (address instanceof Inet4Address) {
System.out.println("IPv4 Address");
Inet4Address ipv4Address = (Inet4Address) address;
// Perform IPv4-specific operations
} else if (address instanceof Inet6Address) {
System.out.println("IPv6 Address");
Inet6Address ipv6Address = (Inet6Address) address;
// Perform IPv6-specific operations
}
6
System.out.println("Host Name: " + address.getHostName());
System.out.println("IP Address: " + address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
● In the example above, we obtain an InetAddress object representing an IPv6
address.
● We then check the type of the address using the instanceof operator and
perform IPv4-specific or IPv6-specific operations accordingly.
● 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.