Program 7 To 12 CN Lab With Outputs
Program 7 To 12 CN Lab With Outputs
#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;
sleep(1);
int main() {
Frame frames[MAX_SEQ_NUM];
int base = 0;
int next_seq_num = 0;
frames[next_seq_num] = frame;
send_frame(frame);
next_seq_num++;
// Receive acknowledgments
if (!frames[i].acked) {
receive_ack(frames[i]);
frames[i].acked = true;
base++;
}
}
return 0;
OUTPUT:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<unistd.h>
typedef struct {
int seq_num;
char data[PACKET_SIZE];
} Packet;
usleep(1000);
Packet receive_packet() {
Packet packet;
printf("Receiving packet...\n");
usleep(1000);
return packet;
}
void send_ack(int seq_num) {
usleep(1000);
int main() {
int expected_seq_num = 0;
while (true) {
if (packet.seq_num == expected_seq_num) {
send_packet(packet);
expected_seq_num++;
send_ack(expected_seq_num - 1);
return 0;
}
OUTPUT:
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Receiving packet...
Sending ACK for sequence number: 0
Receiving packet...
Receiving packet...
typedef struct {
int size; // Current bucket size
int output_rate; // Output rate in packets per second
} LeakyBucket;
int main() {
LeakyBucket bucket;
initializeBucket(&bucket);
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");
}
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:
Packet 1
Packet sent.
Packet sent.
Packet sent.
Bucket is empty.
Bucket is empty.
Bucket is empty.
Bucket is empty.
Bucket is empty.
Bucket is empty.
Bucket is empty.
Packet 2
Packet sent.
Packet sent.
Packet sent.
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
return min_index;
int dist[V];
bool visited[V];
dist[source] = 0;
visited[u] = true;
if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
printSolution(dist);
int main() {
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{0 ,0 ,2 ,0 ,0 ,6 ,7 ,8 ,9}};
dijkstra(graph, 0);
return 0;
OUTPUT:
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>
int main() {
int numNodes = 5; // Number of nodes in the network
return 0;
}
OUTPUT:
0 0 -1
1 9999 -1
2 9999 -1
3 9999 -1
4 9999 -1
0 9999 -1
1 0 -1
2 9999 -1
3 9999 -1
4 9999 -1
0 9999 -1
1 9999 -1
2 0 -1
3 9999 -1
4 9999 -1
0 9999 -1
1 9999 -1
2 9999 -1
3 0 -1
4 9999 -1
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>
int data;
int numChildren;
} Node;
newNode->data = data;
newNode->numChildren = 0;
return newNode;
parent->children[parent->numChildren] = child;
parent->numChildren++;
}
// Function to perform breadth-first traversal of the broadcast tree
if (root == NULL)
return;
Node* queue[MAX_HOSTS];
queue[rear++] = root;
queue[rear++] = currentNode->children[i];
}
int main() {
addChild(root, node2);
addChild(root, node3);
addChild(node2, node4);
addChild(node2, node5);
broadcast(root);
return 0;
OUTPUT:
Broadcast Tree: 1 2 3 4 5