0% found this document useful (0 votes)
21 views6 pages

Unit-3 IP Adressing

Uploaded by

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

Unit-3 IP Adressing

Uploaded by

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

1

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 -

Example: Class A IP Example: Class B IP Example: Class C IP


• Address: 10.0.0.1 • Address: 172.16.5.4 • Address: 192.168.1.10
• Network Portion: 10 • Network Portion: 172.16 • Network Portion:
• Host Portion: 0.0.1 • Host Portion: 5.4 192.168.1
• Default Subnet Mask: • Default Subnet Mask: • Host Portion: 10
255.0.0.0 255.255.0.0 • Default Subnet Mask:
255.255.255.0
Public and Private IP Addresses
• Public IPs: Globally unique and routable on the internet.
o Example: 8.8.8.8 (Google DNS server).
• Private IPs: Used within local networks, not routable on the internet.
o Class A: 10.0.0.0 to 10.255.255.255
o Class B: 172.16.0.0 to 172.31.255.255
o Class C: 192.168.0.0 to 192.168.255.255

Ajay Parashar IT@HCST


2
Subnetting
Subnetting divides a network into smaller, more manageable sub-networks (subnets).
Subnet Mask
A subnet mask determines which part of an IP address is the network portion and which is the host portion.
• Example:
o IP: 192.168.1.10
o Subnet Mask: 255.255.255.0
▪ Network Portion: 192.168.1
▪ Host Portion: 10
CIDR Notation
Classless Inter-Domain Routing (CIDR) allows flexible subnetting by specifying the number of bits used for the network
portion.
• Example:
o 192.168.1.10/24
▪ /24 means the first 24 bits are the network portion.
▪ Equivalent Subnet Mask: 255.255.255.0.

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.

IPv6 Address Types


1. Unicast: Represents a single device.
2. Multicast: Represents a group of devices.
3. Anycast: Represents a group of devices, delivering to the nearest one.

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

Ajay Parashar IT@HCST


3
• Represents both IPv4 and IPv6 addresses.
• Provides static factory methods to create InetAddress objects.

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.

Ajay Parashar IT@HCST


4

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.

Ajay Parashar IT@HCST


5
3. setRequestProperty(String key, String value):
o Sets request headers.

TCP/IP Server Sockets


Overview
The ServerSocket class is used to create a server that listens for incoming client connections.
Constructor
• ServerSocket(int port):
o Creates a server socket on the specified port.
Methods
1. accept():
o Waits for a client connection and returns a Socket object.
o Example:
o ServerSocket server = new ServerSocket(8080);
o Socket client = server.accept();
2. close():
o Closes the server socket.

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.

Practical Example: Sending a Datagram


import java.net.*;

public class UDPSender {


public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String message = "Hello, UDP!";
byte[] buffer = message.getBytes();
Ajay Parashar IT@HCST
6

InetAddress address = InetAddress.getByName("example.com");


DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, 12345);
socket.send(packet);
socket.close();
}
}

Practical Example: Receiving a Datagram


import java.net.*;

public class UDPReceiver {


public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(12345);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

socket.receive(packet);
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received: " + received);
socket.close();
}
}

Ajay Parashar IT@HCST

You might also like