0% found this document useful (0 votes)
28 views21 pages

Program 7 To 12 CN Lab With Outputs

The document describes programs to implement various network protocols: 1. A C program to implement the selective repeat sliding window protocol for reliable data transmission over unreliable networks. It uses a window of 4 frames and sends frames in a loop while also receiving acknowledgments. 2. A C program to implement the stop-and-wait protocol where a sender waits for an acknowledgment for each frame before sending the next one. It uses a simple loop to repeatedly send, receive, and acknowledge frames. 3. A C program for congestion control using the leaky bucket algorithm. It simulates adding packets to a bucket, removing packets at a set rate, and checks if packets can be added without overflowing the bucket

Uploaded by

palivelasrinu12
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)
28 views21 pages

Program 7 To 12 CN Lab With Outputs

The document describes programs to implement various network protocols: 1. A C program to implement the selective repeat sliding window protocol for reliable data transmission over unreliable networks. It uses a window of 4 frames and sends frames in a loop while also receiving acknowledgments. 2. A C program to implement the stop-and-wait protocol where a sender waits for an acknowledgment for each frame before sending the next one. It uses a simple loop to repeatedly send, receive, and acknowledge frames. 3. A C program for congestion control using the leaky bucket algorithm. It simulates adding packets to a bucket, removing packets at a set rate, and checks if packets can be added without overflowing the bucket

Uploaded by

palivelasrinu12
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/ 21

7. Write a C program to implement Sliding window protocol Selective repeat.

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

#include<string.h>

#define WINDOW_SIZE 4

#define MAX_SEQ_NUM 8

typedef struct {

int seq_num;

bool acked;

bool sent;

} Frame;

void send_frame(Frame frame) {

printf("Sending frame with sequence number %d\n", frame.seq_num);

// Simulate transmission delay

sleep(1);

void receive_ack(Frame frame) {

printf("Received ACK for frame with sequence number %d\n", frame.seq_num);

int main() {
Frame frames[MAX_SEQ_NUM];

int base = 0;

int next_seq_num = 0;

while (base < MAX_SEQ_NUM) {

// Send new frames within the window

while (next_seq_num < base + WINDOW_SIZE && next_seq_num < MAX_SEQ_NUM) {

Frame frame = {next_seq_num, false, true};

frames[next_seq_num] = frame;

send_frame(frame);

next_seq_num++;

// Receive acknowledgments

for (int i = base; i < next_seq_num; i++) {

if (!frames[i].acked) {

receive_ack(frames[i]);

frames[i].acked = true;

// Slide the window

while (base < MAX_SEQ_NUM && frames[base].acked) {

base++;

}
}

return 0;

OUTPUT:

Sending frame with sequence number 0

Sending frame with sequence number 1

Sending frame with sequence number 2

Sending frame with sequence number 3

Received ACK for frame with sequence number 0

Received ACK for frame with sequence number 1

Received ACK for frame with sequence number 2

Received ACK for frame with sequence number 3

Sending frame with sequence number 4

Sending frame with sequence number 5

Sending frame with sequence number 6

Sending frame with sequence number 7

Received ACK for frame with sequence number 4

Received ACK for frame with sequence number 5

Received ACK for frame with sequence number 6

Received ACK for frame with sequence number 7


8. Write a C program to implement Stop and Wait Protocol

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

#include<unistd.h>

#define PACKET_SIZE 1024

typedef struct {

int seq_num;

char data[PACKET_SIZE];

} Packet;

void send_packet(Packet packet) {

printf("Sending packet with sequence number: %d\n", packet.seq_num);

// Simulate transmission delay

usleep(1000);

Packet receive_packet() {

Packet packet;

printf("Receiving packet...\n");

// Simulate reception delay

usleep(1000);

return packet;

}
void send_ack(int seq_num) {

printf("Sending ACK for sequence number: %d\n", seq_num);

// Simulate transmission delay

usleep(1000);

int main() {

int expected_seq_num = 0;

while (true) {

Packet packet = receive_packet();

if (packet.seq_num == expected_seq_num) {

send_packet(packet);

expected_seq_num++;

send_ack(expected_seq_num - 1);

return 0;

}
OUTPUT:

Receiving packet...

Sending ACK for sequence number: -1

Receiving packet...

Sending packet with sequence number: 0

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...
Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0

Receiving packet...

Sending ACK for sequence number: 0


9). Write a C program for Congestion control using leaky bucket algorithm
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define BUCKET_SIZE 100 // Maximum bucket size


#define OUTPUT_RATE 10 // Output rate in packets per second

typedef struct {
int size; // Current bucket size
int output_rate; // Output rate in packets per second
} LeakyBucket;

void initializeBucket(LeakyBucket *bucket) {


bucket->size = 0;
bucket->output_rate = OUTPUT_RATE;
}

bool isBucketFull(LeakyBucket *bucket, int packet_size) {


if ((bucket->size + packet_size) > BUCKET_SIZE)
return true;
else
return false;
}

void addToBucket(LeakyBucket *bucket, int packet_size) {


if (!isBucketFull(bucket, packet_size))
bucket->size += packet_size;
}

void removeFromBucket(LeakyBucket *bucket) {


if (bucket->size > 0)
bucket->size--;
}

int main() {
LeakyBucket bucket;
initializeBucket(&bucket);

int total_packets, packet_size;


printf("Enter the total number of packets: ");
scanf("%d", &total_packets);
printf("Enter the size of each packet: ");
scanf("%d", &packet_size);

int i;
for (i = 1; i <= total_packets; i++) {
printf("\nPacket %d\n", i);
printf("Incoming packet size: %d\n", packet_size);

if (isBucketFull(&bucket, packet_size)) {
printf("Packet dropped due to congestion!\n");
} else {
addToBucket(&bucket, packet_size);
printf("Packet added to the bucket.\n");
}

printf("Bucket size after adding packet: %d\n", bucket.size);

int j;
for (j = 1; j <= bucket.output_rate; j++) {
if (bucket.size > 0) {
removeFromBucket(&bucket);
printf("Packet sent.\n");
printf("Bucket size after sending packet: %d\n", bucket.size);
} else {
printf("Bucket is empty.\n");
}
}
}

return 0;
}

OUTPUT:

Enter the total number of packets: 2

Enter the size of each packet: 3

Packet 1

Incoming packet size: 3

Packet added to the bucket.


Bucket size after adding packet: 3

Packet sent.

Bucket size after sending packet: 2

Packet sent.

Bucket size after sending packet: 1

Packet sent.

Bucket size after sending packet: 0

Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.

Packet 2

Incoming packet size: 3

Packet added to the bucket.

Bucket size after adding packet: 3

Packet sent.

Bucket size after sending packet: 2

Packet sent.

Bucket size after sending packet: 1

Packet sent.

Bucket size after sending packet: 0


Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.

Bucket is empty.
10.Write a C program to implement Dijkstra’s algorithm to compute the shortest path
through the graph.

#include<stdio.h>

#include<stdbool.h>

#include<limits.h>

#define V 9

int minDistance(int dist[], bool visited[]) {

int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)

if (visited[v] == false && dist[v] <= min)

min = dist[v], min_index = v;

return min_index;

void printSolution(int dist[]) {

printf("Vertex \t\t Distance from Source\n");

for (int i = 0; i < V; i++)

printf("%d \t\t %d\n", i, dist[i]);

void dijkstra(int graph[V][V], int source) {

int dist[V];
bool visited[V];

for (int i = 0; i < V; i++)

dist[i] = INT_MAX, visited[i] = false;

dist[source] = 0;

for (int count = 0; count < V - 1; count++) {

int u = minDistance(dist, visited);

visited[u] = true;

for (int v = 0; v < V; v++)

if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])

dist[v] = dist[u] + graph[u][v];

printSolution(dist);

int main() {

int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},

{4, 0, 8, 0, 0, 0, 0, 11, 0},

{0, 8, 0, 7, 0, 4, 0, 0, 2},

{0, 0, 7, 0, 9, 14, 0, 0, 0},

{0, 0, 0, 9, 0, 10, 0, 0, 0},


{0, 0, 4, 14, 10, 0, 2, 0, 0},

{0, 0, 0, 0, 0, 2, 0, 1, 6},

{8, 11, 0 ,0 ,0 ,0 ,1 ,0 ,7},

{0 ,0 ,2 ,0 ,0 ,6 ,7 ,8 ,9}};

dijkstra(graph, 0);

return 0;

OUTPUT:

Vertex Distance from Source

0 0

1 4

2 12

3 19

4 21

5 11

6 9

7 8

8 14
11. Write a C program to implement Distance vector routing algorithm by obtaining
routing table at each node by taking an example subnet graph with weights indicating
delay between nodes.
#include<stdio.h>
#include<stdlib.h>

#define INFINITY 9999


#define MAX_NODES 10

// Structure to represent a node in the network


typedef struct {
int cost[MAX_NODES]; // Cost to reach each node
int via[MAX_NODES]; // Next hop for each node
} Node;

// Function to initialize a node's routing table


void initNode(Node* node, int numNodes) {
for (int i = 0; i < numNodes; i++) {
node->cost[i] = INFINITY;
node->via[i] = -1;
}
}

// Function to update a node's routing table based on received information


void updateRoutingTable(Node* node, int neighbor, int* costs, int numNodes) {
for (int i = 0; i < numNodes; i++) {
if (costs[i] + node->cost[neighbor] < node->cost[i]) {
node->cost[i] = costs[i] + node->cost[neighbor];
node->via[i] = neighbor;
}
}
}

// Function to print a node's routing table


void printRoutingTable(Node* node, int numNodes) {
printf("Routing table for Node:\n");
printf("Destination\tCost\tNext Hop\n");
for (int i = 0; i < numNodes; i++) {
printf("%d\t\t%d\t%d\n", i, node->cost[i], node->via[i]);
}
}

int main() {
int numNodes = 5; // Number of nodes in the network

// Create an array of nodes


Node network[numNodes];

// Initialize each node's routing table


for (int i = 0; i < numNodes; i++) {
initNode(&network[i], numNodes);
}

// Set the initial costs for each node


network[0].cost[0] = 0;
network[1].cost[1] = 0;
network[2].cost[2] = 0;
network[3].cost[3] = 0;
network[4].cost[4] = 0;

// Simulate the exchange of routing information between nodes


for (int k = 0; k < numNodes; k++) {
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
if (network[i].cost[j] > network[i].cost[k] + network[k].cost[j]) {
network[i].cost[j] = network[i].cost[k] + network[k].cost[j];
network[i].via[j] = k;
}
}
}
}

// Print the final routing tables for each node


for (int i = 0; i < numNodes; i++) {
printRoutingTable(&network[i], numNodes);
printf("\n");
}

return 0;
}

OUTPUT:

Routing table for Node:

Destination Cost Next Hop

0 0 -1

1 9999 -1

2 9999 -1
3 9999 -1

4 9999 -1

Routing table for Node:

Destination Cost Next Hop

0 9999 -1

1 0 -1

2 9999 -1

3 9999 -1

4 9999 -1

Routing table for Node:

Destination Cost Next Hop

0 9999 -1

1 9999 -1

2 0 -1

3 9999 -1

4 9999 -1

Routing table for Node:

Destination Cost Next Hop

0 9999 -1

1 9999 -1

2 9999 -1

3 0 -1
4 9999 -1

Routing table for Node:

Destination Cost Next Hop

0 9999 -1

1 9999 -1

2 9999 -1

3 9999 -1

4 0 -1
12. Write a C program to implement Broadcast tree by taking subnet of hosts

#include<stdio.h>

#include<stdlib.h>

#define MAX_HOSTS 100

// Structure to represent a node in the broadcast tree

typedef struct Node {

int data;

struct Node* children[MAX_HOSTS];

int numChildren;

} Node;

// Function to create a new node

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

newNode->data = data;

newNode->numChildren = 0;

return newNode;

// Function to add a child node to a parent node

void addChild(Node* parent, Node* child) {

parent->children[parent->numChildren] = child;

parent->numChildren++;

}
// Function to perform breadth-first traversal of the broadcast tree

void broadcast(Node* root) {

if (root == NULL)

return;

// Create a queue for level order traversal

Node* queue[MAX_HOSTS];

int front = 0, rear = 0;

// Enqueue the root node

queue[rear++] = root;

while (front < rear) {

// Dequeue the front node and print its data

Node* currentNode = queue[front++];

printf("%d ", currentNode->data);

// Enqueue all children of the dequeued node

for (int i = 0; i < currentNode->numChildren; i++) {

queue[rear++] = currentNode->children[i];

}
int main() {

// Create the broadcast tree

Node* root = createNode(1);

Node* node2 = createNode(2);

Node* node3 = createNode(3);

Node* node4 = createNode(4);

Node* node5 = createNode(5);

addChild(root, node2);

addChild(root, node3);

addChild(node2, node4);

addChild(node2, node5);

// Perform broadcast traversal

printf("Broadcast Tree: ");

broadcast(root);

return 0;

OUTPUT:

Broadcast Tree: 1 2 3 4 5

You might also like