Experiment-5
5. Take an example subnet of hosts and obtain a broadcast tree for the subnet.
C Program to Build and Print a Broadcast Tree
// File Name: broadcast_tree.c
#include <stdio.h>
#include <stdlib.h>
#define MAX_HOSTS 4
// Structure to represent a node in the broadcast tree
struct Node {
char *ip;
struct Node *next;
};
// Function to add a child node
struct Node *addNode(char *ip) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->ip = ip;
newNode->next = NULL;
return newNode;
}
// Function to build the broadcast tree
void buildBroadcastTree(struct Node **root) {
*root = addNode("192.168.1.1"); // Root node (Host A)
(*root)->next = addNode("192.168.1.2"); // Child node (Host B)
(*root)->next->next = addNode("192.168.1.3"); // Child node (Host C)
(*root)->next->next->next = addNode("192.168.1.4"); // Child node (Host D)
}
// Function to print the broadcast tree
void printBroadcastTree(struct Node *root) {
struct Node *current = root;
while (current != NULL) {
printf("%s -> ", current->ip);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node *broadcastTree = NULL;
buildBroadcastTree(&broadcastTree);
printBroadcastTree(broadcastTree);
// Free the allocated memory
struct Node *current = broadcastTree;
struct Node *nextNode;
while (current != NULL) {
nextNode = current->next;
free(current);
current = nextNode;
}
return 0;
}
Compile : gcc -o broadcast_tree broadcast_tree.c
Execution : ./broadcast_tree
Output :
192.168.1.1 -> 192.168.1.2 -> 192.168.1.3 -> 192.168.1.4 -> NULL
Explanation
What is a Subnet?
A subnet (short for "subnetwork") is a logical subdivision of an IP network. The main purpose of
subnetting is to improve network performance and security by dividing a larger network into
smaller, more manageable segments. Each subnet operates independently and can be managed
separately.
Key Components of a Subnet:
• IP Address: A unique identifier for each device on a network (e.g., 192.168.1.1).
• Subnet Mask: Used to determine which portion of an IP address is the network part and
which part is the host part (e.g., 255.255.255.0).
• Network Address: Identifies the subnet and is obtained by performing a bitwise AND
operation between the IP address and the subnet mask (e.g., 192.168.1.0).
• Broadcast Address: Used to send data to all devices on the subnet. It is obtained by setting
all the host bits in the IP address to 1 (e.g., 192.168.1.255).
What is a Broadcast Tree?
A broadcast tree is a type of spanning tree used in computer networks to ensure that a broadcast
message sent by any node (host) reaches every other node in the network without causing loops. In
a broadcast tree, each node can reach every other node through the tree structure.
Example Subnet and Broadcast Tree
Example Subnet
Let's consider a simple subnet with the following hosts and their respective IP addresses:
• Host A: 192.168.1.1
• Host B: 192.168.1.2
• Host C: 192.168.1.3
• Host D: 192.168.1.4
Building the Broadcast Tree
A broadcast tree is built by connecting all hosts in a way that any broadcast from the root node
(Host A) will reach all other hosts.
1. Choose a Root Node:
• Let's select Host A (192.168.1.1) as our root node.
2. Add Direct Neighbors:
• Connect Host A to Host B (192.168.1.2)
• Connect Host A to Host C (192.168.1.3)
• Connect Host A to Host D (192.168.1.4)
Resulting Broadcast Tree
The resulting broadcast tree would look like this:
Host A
/ | \
Host B Host C Host D
Root: Host A (192.168.1.1)
• Child Nodes: Host B (192.168.1.2), Host C (192.168.1.3), Host D (192.168.1.4)