Unit-3 IP Adressing
Unit-3 IP Adressing
Unit-3 (Part-2)
Internet Addressing
Overview
Internet Addressing refers to the system used to identify and locate devices on a network. The most common forms of
addressing are:
• IP Address: A numerical label assigned to each device (e.g., 192.168.1.1 for IPv4 or 2001:db8::1 for IPv6).
• Hostname: A human-readable name associated with an IP address (e.g., www.google.com).
• DNS (Domain Name System): Resolves hostnames to IP addresses.
IP Addressing
IP addressing is the system used to identify devices on a network. Each device (or host) is assigned a unique address to
facilitate communication.
Structure of an IP Address
An IP address is a 32-bit (IPv4) or 128-bit (IPv6) identifier used in networking.
IPv4 Address
• Consists of 4 octets (8 bits each), separated by dots: e.g., 192.168.1.1
• Each octet can range from 0 to 255 (8 bits = 28=256 possible values).
IPv6 Address
• Consists of 8 groups of 4 hexadecimal digits (16 bits per group), separated by colons:
e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334
IPv4 Addressing
Classes of IPv4 Addresses
IPv4 addresses are divided into classes based on their leading bits, which define their range and network/host structure.
Class Range Purpose Default Subnet Mask
A 1.0.0.0 to 126.255.255.255 Large networks 255.0.0.0
B 128.0.0.0 to 191.255.255.255 Medium-sized networks 255.255.0.0
C 192.0.0.0 to 223.255.255.255 Small networks 255.255.255.0
D 224.0.0.0 to 239.255.255.255 Multicast -
E 240.0.0.0 to 255.255.255.255 Experimental -
IPv6 Addressing
IPv6 addresses are designed to overcome IPv4's limitations, offering a much larger address space.
Structure
• 128 bits divided into 8 groups of 16 bits, written in hexadecimal.
• Example: 2001:0db8:85a3:0000:0000:8a2e:0370:7334.
Abbreviation Rules
1. Omit leading zeros in each block.
o Example: 2001:0db8:0000:0000:0000:8a2e:0370:7334 → 2001:db8:0:0:0:8a2e:370:7334.
2. Use :: to replace consecutive blocks of zeros.
o Example: 2001:db8:0:0:0:8a2e:370:7334 → 2001:db8::8a2e:370:7334.
IPv4 vs IPv6
Feature IPv4 IPv6
Address Size 32 bits 128 bits
Address Space 2322^{32} (4.3 billion) 21282^{128} (340 undecillion)
Header Size 20 bytes 40 bytes
Address Notation Dotted decimal Hexadecimal
Security Optional (IPSec) Mandatory (IPSec built-in)
InetAddress Class
Overview
The InetAddress class in Java represents an IP address. It provides methods to resolve hostnames to IP addresses and vice
versa.
Key Features
Factory Methods
1. InetAddress.getByName(String host):
o Resolves a hostname to an IP address.
o Example:
InetAddress address = InetAddress.getByName("www.google.com");
System.out.println(address);
2. InetAddress.getAllByName(String host):
o Returns all IP addresses for a given hostname.
o Example:
InetAddress[] addresses = InetAddress.getAllByName("www.google.com");
for (InetAddress addr : addresses) {
System.out.println(addr);
}
3. InetAddress.getLocalHost():
o Retrieves the address of the local machine.
o Example:
InetAddress localAddress = InetAddress.getLocalHost();
System.out.println(localAddress);
4. InetAddress.getLoopbackAddress():
o Returns the loopback address (127.0.0.1).
Instance Methods
1. getHostName():
o Returns the hostname of the InetAddress.
o Example:
System.out.println(address.getHostName());
2. getHostAddress():
o Returns the IP address as a string.
o Example:
System.out.println(address.getHostAddress());
3. isReachable(int timeout):
o Checks if the address is reachable within the given timeout.
o Example:
o System.out.println(address.isReachable(5000)); // 5 seconds
TCP/IP
Overview
• TCP (Transmission Control Protocol): Reliable, connection-oriented protocol for data transmission.
• IP (Internet Protocol): Handles addressing and routing of packets across networks.
• TCP/IP: Combination of TCP and IP protocols that form the foundation of the Internet.
Client Sockets
Overview
Client sockets enable communication between a client application and a server using TCP.
Key Class: Socket
Constructor
• Socket(String host, int port):
o Creates a connection to a server.
o Example:
o Socket socket = new Socket("example.com", 80);
Methods
1. getInputStream():
o Returns the input stream to read data from the server.
2. getOutputStream():
o Returns the output stream to send data to the server.
3. close():
o Closes the connection.
URL Class
Overview
The URL class represents a Uniform Resource Locator and is used to access resources on the web.
Key Methods
1. URL(String spec):
o Creates a URL object from a string.
o Example:
o URL url = new URL("https://example.com");
2. openStream():
o Opens a stream to read data from the URL.
URLConnection Class
Overview
The URLConnection class represents a communication link between an application and a URL.
Key Methods
1. getInputStream():
o Reads data from the URL.
2. getOutputStream():
o Sends data to the URL.
Datagram (UDP)
Overview
The DatagramSocket and DatagramPacket classes are used for communication over UDP.
Key Classes
1. DatagramSocket:
o Represents a socket for sending and receiving datagrams.
o Example:
o DatagramSocket socket = new DatagramSocket();
2. DatagramPacket:
o Represents a data packet sent or received over a DatagramSocket.
socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received: " + received);
socket.close();
}
}