4 CRC – CCITT(16- BIT)
#include <stdio.h>
#include <string.h>
#define POLY 0x1021 // CRC-CCITT polynomial
#define INITIAL_CRC 0x0000
unsigned short crc_ccitt(const unsigned char *data, int length) {
unsigned short crc = INITIAL_CRC;
for (int i = 0; i < length; i++) {
crc ^= (data[i] << 8); // Move byte into MSB of 16-bit CRC
for (int j = 0; j < 8; j++) {
if (crc & 0x8000) // If MSB is set
crc = (crc << 1) ^ POLY;
else
crc <<= 1;
return crc;
int main() {
char input[1024];
printf("Enter the input data (string): ");
scanf("%s", input);
unsigned short crc = crc_ccitt((unsigned char *)input, strlen(input));
printf("CRC-CCITT Checksum: 0x%04X\n", crc);
// Simulate appending CRC to message
printf("Transmitting data: \"%s\" with CRC 0x%04X\n", input, crc);
// Error detection simulation
// Append CRC to message and re-check
unsigned char message_with_crc[1024];
int len = strlen(input);
memcpy(message_with_crc, input, len);
message_with_crc[len] = (crc >> 8) & 0xFF; // High byte
message_with_crc[len + 1] = crc & 0xFF; // Low byte
unsigned short check_crc = crc_ccitt(message_with_crc, len + 2);
if (check_crc == 0)
printf("No error detected in received data.\n");
else
printf("Error detected in received data! CRC check failed (0x%04X).\n", check_crc);
return 0;
}#include <stdio.h>
#include <string.h>
#define POLY 0x1021 // CRC-CCITT polynomial
#define INITIAL_CRC 0x0000
unsigned short crc_ccitt(const unsigned char *data, int length) {
unsigned short crc = INITIAL_CRC;
for (int i = 0; i < length; i++) {
crc ^= (data[i] << 8); // Move byte into MSB of 16-bit CRC
for (int j = 0; j < 8; j++) {
if (crc & 0x8000) // If MSB is set
crc = (crc << 1) ^ POLY;
else
crc <<= 1;
return crc;
int main() {
char input[1024];
printf("Enter the input data (string): ");
scanf("%s", input);
unsigned short crc = crc_ccitt((unsigned char *)input, strlen(input));
printf("CRC-CCITT Checksum: 0x%04X\n", crc);
// Simulate appending CRC to message
printf("Transmitting data: \"%s\" with CRC 0x%04X\n", input, crc);
// Error detection simulation
// Append CRC to message and re-check
unsigned char message_with_crc[1024];
int len = strlen(input);
memcpy(message_with_crc, input, len);
message_with_crc[len] = (crc >> 8) & 0xFF; // High byte
message_with_crc[len + 1] = crc & 0xFF; // Low byte
unsigned short check_crc = crc_ccitt(message_with_crc, len + 2);
if (check_crc == 0)
printf("No error detected in received data.\n");
else
printf("Error detected in received data! CRC check failed (0x%04X).\n", check_crc);
return 0;
OUTPUT
Enter the input data (string): 3
CRC-CCITT Checksum: 0x0630
Transmitting data: "3" with CRC 0x0630
No error detected in received data.
5 Sliding window protocol in the data link layer
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define MAX_FRAMES 50 // Total number of frames
#define WINDOW_SIZE 4 // Sliding window size
#define LOSS_PROBABILITY 30 // % chance of frame loss (0-100)
void send_frames(int start, int end) {
printf("Sending frames: ");
for (int i = start; i < end; i++) {
printf("[%d] ", i);
printf("\n");
bool simulate_ack(int frame) {
int chance = rand() % 100;
return chance >= LOSS_PROBABILITY;
int main() {
srand(time(0));
int base = 0;
int next_frame = 0;
while (base < MAX_FRAMES) {
int end = base + WINDOW_SIZE;
if (end > MAX_FRAMES)
end = MAX_FRAMES;
send_frames(base, end);
for (int i = base; i < end; i++) {
if (simulate_ack(i)) {
printf("Acknowledgment received for frame %d\n", i);
base++; // Slide window
} else {
printf("Frame %d lost! Retransmitting from frame %d\n", i, i);
break; // Go-Back-N: retransmit from lost frame
printf("Current window: [%d to %d)\n\n", base, base + WINDOW_SIZE);
printf("All frames sent successfully using Sliding Window Protocol!\n");
return 0;
OUTPUT
Sending frames: [0] [1] [2] [3]
Acknowledgment received for frame 0
Acknowledgment received for frame 1
Frame 2 lost! Retransmitting from frame 2
Current window: [2 to 6)
Sending frames: [2] [3] [4] [5]
Acknowledgment received for frame 2
Frame 3 lost! Retransmitting from frame 3
Current window: [3 to 7)
Sending frames: [3] [4] [5] [6]
Frame 3 lost! Retransmitting from frame 3
Current window: [3 to 7)
Sending frames: [3] [4] [5] [6]
Frame 3 lost! Retransmitting from frame 3
Current window: [3 to 7)
Sending frames: [3] [4] [5] [6]
Acknowledgment received for frame 3
Acknowledgment received for frame 4
Acknowledgment received for frame 5
Acknowledgment received for frame 6
Current window: [7 to 11)
All frames sent successfully using Sliding Window Protocol!
6 shortest path between vertices using the bellman ford and
path vector routing algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VERTICES 10
#define INF 9999
typedef struct {
int cost;
int path[MAX_VERTICES];
int pathLen;
} PathVector;
void initializePathVector(PathVector table[MAX_VERTICES], int source, int V) {
for (int i = 0; i < V; i++) {
table[i].cost = INF;
table[i].pathLen = 0;
}
table[source].cost = 0;
table[source].path[0] = source;
table[source].pathLen = 1;
void printPathVectorTable(PathVector table[MAX_VERTICES], int V, int src) {
printf("Routing table for node %d:\n", src);
for (int i = 0; i < V; i++) {
printf("To %d: Cost = %d, Path = ", i, table[i].cost);
for (int j = 0; j < table[i].pathLen; j++) {
printf("%d ", table[i].path[j]);
printf("\n");
printf("\n");
void bellmanFordPathVector(int graph[MAX_VERTICES][MAX_VERTICES], int V, int source) {
PathVector table[MAX_VERTICES];
initializePathVector(table, source, V);
// Relax all edges |V|-1 times
for (int k = 0; k < V - 1; k++) {
for (int u = 0; u < V; u++) {
for (int v = 0; v < V; v++) {
if (graph[u][v] != INF && table[u].cost + graph[u][v] < table[v].cost) {
table[v].cost = table[u].cost + graph[u][v];
table[v].pathLen = table[u].pathLen + 1;
memcpy(table[v].path, table[u].path, sizeof(int) * table[u].pathLen);
table[v].path[table[u].pathLen] = v;
printPathVectorTable(table, V, source);
int main() {
int V, source;
int graph[MAX_VERTICES][MAX_VERTICES];
printf("Enter number of vertices: ");
scanf("%d", &V);
printf("Enter adjacency matrix (use 9999 for no direct edge):\n");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
scanf("%d", &graph[i][j]);
}
printf("Enter source vertex: ");
scanf("%d", &source);
bellmanFordPathVector(graph, V, source);
return 0;
OUTPUT
Enter number of vertices: 4
Enter adjacency matrix (use 9999 for no direct edge):
0 1 4 9999
9999 0 2 6
9999 9999 0 3
9999 9999 9999 0
Enter source vertex: 0
Routing table for node 0:
To 0: Cost = 0, Path = 0
To 1: Cost = 1, Path = 0 1
To 2: Cost = 3, Path = 0 1 2
To 3: Cost = 6, Path = 0 1 2 3
7th program
1. Server Code (server.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#define PORT 8080
#define BUFFER_SIZE 1024
int main() {
int server_fd, new_socket;
char buffer[BUFFER_SIZE] = {0};
struct sockaddr_in address;
int addrlen = sizeof(address);
// Create TCP socket
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
perror("Socket failed");
exit(EXIT_FAILURE);
}
// Configure server address
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; // 0.0.0.0
address.sin_port = htons(PORT);
// Bind the socket
bind(server_fd, (struct sockaddr*)&address, sizeof(address));
// Listen for client
listen(server_fd, 3);
printf("Server listening on port %d...\n", PORT);
// Accept incoming connection
new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
if (new_socket < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
// Read file name from client
read(new_socket, buffer, BUFFER_SIZE);
printf("Client requested file: %s\n", buffer);
FILE *fp = fopen(buffer, "r");
if (fp == NULL) {
char *msg = "File not found on server.\n";
send(new_socket, msg, strlen(msg), 0);
} else {
// Read and send file contents
while (fgets(buffer, BUFFER_SIZE, fp)) {
send(new_socket, buffer, strlen(buffer), 0);
fclose(fp);
printf("File sent successfully or error message sent.\n");
close(new_socket);
close(server_fd);
return 0;
OUTPUT
Server listening on port 8080...
8th pro
1. Server Code (udp_server.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define SERVER_PORT 8080
#define BUFFER_SIZE 1024
int main() {
int sockfd;
char buffer[BUFFER_SIZE];
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
// Create UDP socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
printf("UDP server ready. Waiting for client...\n");
// Wait to receive dummy packet from client to get its address
recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
printf("Client connected.\n");
while (1) {
printf("Enter message to send (type 'exit' to quit): ");
fgets(buffer, BUFFER_SIZE, stdin);
// Send message to client
sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&client_addr, addr_len);
if (strncmp(buffer, "exit", 4) == 0)
break;
}
close(sockfd);
return 0;
}
gcc udp_server.c -o udp_server
./udp_server
OUTPUT
Enter message to send (type 'exit' to quit): Hello client!
Enter message to send (type 'exit' to quit): exit
2. Client Code (udp_client.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define SERVER_PORT 8080
#define BUFFER_SIZE 1024
int main() {
int sockfd;
char buffer[BUFFER_SIZE];
struct sockaddr_in server_addr;
socklen_t addr_len = sizeof(server_addr);
// Create UDP socket
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Configure server address
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
// Send dummy packet to server to notify presence
sendto(sockfd, "Hello", 5, 0, (struct sockaddr *)&server_addr, addr_len);
printf("Client is ready to receive messages from server...\n");
while (1) {
int n = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0, NULL, NULL);
buffer[n] = '\0';
printf("Message from server: %s", buffer);
if (strncmp(buffer, "exit", 4) == 0)
break;
}
close(sockfd);
return 0;
}
OUTPUT
Client is ready to receive messages from server...
Message from server: Hello client!
Message from server: exit
9TH Prog
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
int modInverse(int e, int phi) {
int d = 1;
while ((e * d) % phi != 1)
d++;
return d;
}
long long modExp(long long base, long long exp, long long mod) {
long long result = 1;
base %= mod;
while (exp > 0) {
if (exp % 2 == 1)
result = (result * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return result;
}
int main() {
int p, q, n, phi, e, d;
int message;
long long encrypted, decrypted;
// Choose 2 small prime numbers
printf("Enter two prime numbers (e.g., 3 and 11): ");
scanf("%d %d", &p, &q);
n = p * q;
phi = (p - 1) * (q - 1);
// Choose e such that 1 < e < phi and gcd(e, phi) == 1
for (e = 2; e < phi; e++) {
if (gcd(e, phi) == 1)
break;
}
d = modInverse(e, phi);
printf("Public Key: (e = %d, n = %d)\n", e, n);
printf("Private Key: (d = %d, n = %d)\n", d, n);
printf("Enter a message (as integer): ");
scanf("%d", &message);
encrypted = modExp(message, e, n);
decrypted = modExp(encrypted, d, n);
printf("Encrypted message: %lld\n", encrypted);
printf("Decrypted message: %lld\n", decrypted);
return 0;
}
OUTPUT
Enter two prime numbers (e.g., 3 and 11): 3 11
Public Key: (e = 3, n = 33)
Private Key: (d = 7, n = 33)
Enter a message (as integer): 5
Encrypted message: 26
Decrypted message: 5
10th pro
#include <stdio.h>
#define BUCKET_SIZE 10
#define OUTPUT_RATE 3
int main() {
int n, i, incoming, bucket = 0;
int time = 1;
printf("Enter the number of incoming packets: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nTime %d:\n", time++);
printf("Enter the size of incoming packet %d: ", i + 1);
scanf("%d", &incoming);
if (incoming > BUCKET_SIZE) {
printf("Packet size too large. Entire packet dropped.\n");
} else {
if ((bucket + incoming) <= BUCKET_SIZE) {
bucket += incoming;
printf("Packet accepted. Bucket size now: %d\n", bucket);
} else {
int dropped = (bucket + incoming) - BUCKET_SIZE;
bucket = BUCKET_SIZE;
printf("Bucket overflow! %d packet(s) dropped. Bucket size now: %d\n", dropped,
bucket);
}
}
// Transmit data at output rate
int transmitted = (bucket >= OUTPUT_RATE) ? OUTPUT_RATE : bucket;
bucket -= transmitted;
printf("Transmitted %d packet(s). Remaining in bucket: %d\n", transmitted, bucket);
}
// Empty remaining data in bucket
while (bucket > 0) {
printf("\nTime %d:\n", time++);
int transmitted = (bucket >= OUTPUT_RATE) ? OUTPUT_RATE : bucket;
bucket -= transmitted;
printf("Transmitted %d packet(s). Remaining in bucket: %d\n", transmitted, bucket);
}
return 0;
}
OUTPUT
Enter the number of incoming packets: 5
Time 1:
Enter the size of incoming packet 1: 4
Packet accepted. Bucket size now: 4
Transmitted 3 packet(s). Remaining in bucket: 1
Time 2:
Enter the size of incoming packet 2: 7
Packet accepted. Bucket size now: 8
Transmitted 3 packet(s). Remaining in bucket: 5
Time 3:
Enter the size of incoming packet 3: 6
Bucket overflow! 1 packet(s) dropped. Bucket size now: 10
Transmitted 3 packet(s). Remaining in bucket: 7
Time 4:
Enter the size of incoming packet 4: 2
Packet accepted. Bucket size now: 9
Transmitted 3 packet(s). Remaining in bucket: 6
Time 5:
Enter the size of incoming packet 5: 3
Packet accepted. Bucket size now: 9
Transmitted 3 packet(s). Remaining in bucket: 6
Time 6:
Transmitted 3 packet(s). Remaining in bucket: 3
Time 7:
Transmitted 3 packet(s). Remaining in bucket: 0