KEMBAR78
Computer Networking Lab Experiments | PDF | Integer (Computer Science) | Computer Engineering
0% found this document useful (0 votes)
13 views32 pages

Computer Networking Lab Experiments

The document outlines a series of computer networking lab experiments focused on various error detection and correction techniques, including Even Parity, Two-Dimensional Parity, Checksum, Hamming Code, Cyclic Redundancy Check (CRC), Stop-and-Wait Protocol, and Go-Back-N Protocol. Each experiment includes code implementations in C, demonstrating how to generate and check for errors in transmitted data. The outputs of each experiment illustrate the successful detection and correction of errors in simulated data transmissions.

Uploaded by

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

Computer Networking Lab Experiments

The document outlines a series of computer networking lab experiments focused on various error detection and correction techniques, including Even Parity, Two-Dimensional Parity, Checksum, Hamming Code, Cyclic Redundancy Check (CRC), Stop-and-Wait Protocol, and Go-Back-N Protocol. Each experiment includes code implementations in C, demonstrating how to generate and check for errors in transmitted data. The outputs of each experiment illustrate the successful detection and correction of errors in simulated data transmissions.

Uploaded by

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

COMPUTER NETWORKING LAB EXPERIMENTS

Exp-1
Simulate Even Parity generator and checker
Ans:-
#include <stdio.h>

#include <string.h>

// Function to count number of 1s

int countOnes(char data[]) {

int count = 0;

for (int i = 0; data[i] != '\0'; i++) {

if (data[i] == '1') count++;

return count;

// Function to generate parity bit (Even Parity)

char generateParityBit(char data[]) {

int ones = countOnes(data);

return (ones % 2 == 0) ? '0' : '1'; // Add '1' if odd number of 1s

// Function to check parity at receiver

void checkReceivedData(char received[]) {

int ones = countOnes(received);

if (ones % 2 == 0) {

printf("✅ Data received correctly (Even Parity).\n");

} else {
printf("❌ Error detected in received data.\n");

int main() {

char data[100], transmitted[100];

printf("Enter binary data (e.g., 1011): ");

scanf("%s", data);

// Generate even parity bit

char parityBit = generateParityBit(data);

printf("Parity bit to be added (even parity): %c\n", parityBit);

// Append parity bit to data

strcpy(transmitted, data);

int len = strlen(transmitted);

transmitted[len] = parityBit;

transmitted[len + 1] = '\0';

printf("Transmitted data (with parity): %s\n", transmitted);

// Receiver side

printf("\nEnter received data (e.g., 10111): ");

char received[100];

scanf("%s", received);
// Check at receiver

checkReceivedData(received);

return 0;

Output: Enter binary data (e.g., 1011): 101011001

Parity bit to be added (even parity): 1

Transmitted data (with parity): 1010110011

Enter received data (e.g., 10111): 1010110011

✅ Data received correctly (Even Parity).

Enter binary data (e.g., 1011): 1101

Parity bit to be added (even parity): 1

Transmitted data (with parity): 11011

Enter received data (e.g., 10111): 1110

❌ Error detected in received data.

EXP-2
Simulate two dimensional parity generator and checker
Ans:
#include <stdio.h>

#define ROWS 3

#define COLS 3

// Function to calculate row parity and append

void generateRowParity(int data[ROWS][COLS], int rowParity[ROWS]) {

for (int i = 0; i < ROWS; i++) {


int count = 0;

for (int j = 0; j < COLS; j++) {

if (data[i][j] == 1)

count++;

rowParity[i] = count % 2 == 0 ? 0 : 1; // even parity

// Function to calculate column parity

void generateColParity(int data[ROWS][COLS], int colParity[COLS]) {

for (int j = 0; j < COLS; j++) {

int count = 0;

for (int i = 0; i < ROWS; i++) {

if (data[i][j] == 1)

count++;

colParity[j] = count % 2 == 0 ? 0 : 1;

// Function to check parity

void checkParity(int data[ROWS][COLS], int rowParity[ROWS], int colParity[COLS]) {

int errorDetected = 0;

// Check row parity


for (int i = 0; i < ROWS; i++) {

int count = 0;

for (int j = 0; j < COLS; j++) {

if (data[i][j] == 1)

count++;

if ((count % 2 == 0 ? 0 : 1) != rowParity[i]) {

printf("❌ Row %d parity error\n", i);

errorDetected = 1;

// Check column parity

for (int j = 0; j < COLS; j++) {

int count = 0;

for (int i = 0; i < ROWS; i++) {

if (data[i][j] == 1)

count++;

if ((count % 2 == 0 ? 0 : 1) != colParity[j]) {

printf("❌ Column %d parity error\n", j);

errorDetected = 1;

if (!errorDetected)
printf("✅ No error detected in 2D parity check.\n");

int main() {

int data[ROWS][COLS] = {

{1, 0, 1},

{1, 1, 0},

{0, 0, 1}

};

int rowParity[ROWS], colParity[COLS];

generateRowParity(data, rowParity);

generateColParity(data, colParity);

printf("Original Data with Row Parity:\n");

for (int i = 0; i < ROWS; i++) {

for (int j = 0; j < COLS; j++) {

printf("%d ", data[i][j]);

printf("| %d\n", rowParity[i]);

printf("Column Parity: ");

for (int j = 0; j < COLS; j++) {

printf("%d ", colParity[j]);


}

printf("\n");

// Simulate error (uncomment to test)

// data[1][1] = 0;

printf("\nChecking received data...\n");

checkParity(data, rowParity, colParity);

return 0;

Output:- Original Data with Row Parity:

101|0

110|0

001|1

Column Parity: 0 1 0

Checking received data...

✅ No error detected in 2D parity check.

EXP-3
Simulate checksum generator and checker
Ans:-
#include <stdio.h>

int computeChecksum(int data[], int size) {

int sum = 0;

for(int i = 0; i < size; i++) {

sum += data[i];
}

// Wrap around carry (simulate 8-bit addition)

while (sum >> 8) {

sum = (sum & 0xFF) + (sum >> 8);

// Return 1's complement

return ~sum & 0xFF;

int main() {

int data[100], size;

printf("Enter number of data bytes: ");

scanf("%d", &size);

printf("Enter data bytes (in decimal):\n");

for(int i = 0; i < size; i++) {

scanf("%d", &data[i]);

// Sender side

int checksum = computeChecksum(data, size);

printf("\nChecksum generated: %02X\n", checksum);


// Add checksum to data (simulate transmission)

data[size] = checksum;

// Receiver side

int result = computeChecksum(data, size + 1);

if (result == 0)

printf("Data received successfully. No error.\n");

else

printf("Error detected in received data. Checksum = %02X\n", result);

return 0;

Output: Enter number of data bytes: 2

Enter data bytes (in decimal):

20

30

Checksum generated: CD

Data received successfully. No error.

EXP-4
Simulate Hamming Code Method
Ans:-
#include <stdio.h>

// Function to calculate parity

int calculateParity(int pos, int code[]) {

int parity = 0;
for (int i = 1; i < 8; i++) {

if (i & pos) {

parity ^= code[i];

return parity;

int main() {

int data[8] = {0}; // index 1 to 7 used

int received[8] = {0};

printf("Enter 4 data bits (d1 d2 d3 d4):\n");

scanf("%d%d%d%d", &data[3], &data[5], &data[6], &data[7]);

// Calculate parity bits

data[1] = data[3] ^ data[5] ^ data[7]; // p1

data[2] = data[3] ^ data[6] ^ data[7]; // p2

data[4] = data[5] ^ data[6] ^ data[7]; // p4

printf("\nGenerated Hamming Code (7 bits): ");

for (int i = 1; i < 8; i++) {

printf("%d", data[i]);

printf("\n\nEnter received Hamming Code (7 bits one by one):\n");


for (int i = 1; i < 8; i++) {

scanf("%d", &received[i]);

int p1 = received[1] ^ received[3] ^ received[5] ^ received[7];

int p2 = received[2] ^ received[3] ^ received[6] ^ received[7];

int p4 = received[4] ^ received[5] ^ received[6] ^ received[7];

int errorPos = p4 * 4 + p2 * 2 + p1 * 1;

if (errorPos == 0) {

printf("\nNo error detected.\n");

} else {

printf("\nError detected at position: %d\n", errorPos);

received[errorPos] ^= 1; // Correct the error

printf("Corrected Code: ");

for (int i = 1; i < 8; i++) {

printf("%d", received[i]);

printf("\n");

return 0;

Output: Enter 4 data bits (d1 d2 d3 d4): 1 0 1 1


Generated Hamming Code: 0 1 1 0 0 1 1
Enter received Hamming Code: 0 1 1 1 0 1 1
Error detected at position: 4
Corrected Code: 0110011

EXP-5
Simulate Cyclic Redundancy Check(CRC) error detection algorithm for noisy channel
Ans:
#include <stdio.h>

#include <string.h>

void xor(char *crc, char *key, int len) {

for (int i = 1; i < len; i++) {

crc[i] = (crc[i] == key[i]) ? '0' : '1';

void crc_gen(char *data, char *key, char *crc) {

int datalen = strlen(data);

int keylen = strlen(key);

char temp[100];

strcpy(temp, data);

for (int i = 0; i < keylen - 1; i++) {

strcat(temp, "0"); // Append zeroes

strncpy(crc, temp, keylen);


for (int i = 0; i <= strlen(data) - 1; i++) {

if (crc[0] == '1') {

xor(crc, key, keylen);

} else {

xor(crc, "00000000000000000000", keylen); // All-zero string

crc[keylen - 1] = temp[i + keylen];

crc[keylen - 1] = '\0';

void transmit_with_error(char *data) {

int pos;

printf("\nIntroduce error at position (0 = no error): ");

scanf("%d", &pos);

if (pos > 0 && pos <= strlen(data)) {

data[pos - 1] = (data[pos - 1] == '0') ? '1' : '0';

int main() {

char data[100], key[30], crc[30], transmitted[130];

printf("Enter Data Bits: ");

scanf("%s", data);
printf("Enter Generator Polynomial (key): ");

scanf("%s", key);

crc_gen(data, key, crc);

printf("Generated CRC: %s\n", crc);

strcpy(transmitted, data);

strcat(transmitted, crc);

printf("Transmitted Data: %s\n", transmitted);

// Simulate error in transmission

transmit_with_error(transmitted);

printf("Received Data: %s\n", transmitted);

crc_gen(transmitted, key, crc);

// Check for error

int error = 0;

for (int i = 0; i < strlen(key) - 1; i++) {

if (crc[i] != '0') {

error = 1;

break;

}
if (error) {

printf("\n❌ Error Detected in Received Data\n");

} else {

printf("\n✅ No Error Detected in Received Data\n");

return 0;

Output: Enter Data Bits: 100110101

Enter Generator Polynomial (key): 1101

Generated CRC: 110

Transmitted Data: 100110101110

Introduce error at position (0 = no error): 8

Received Data: 100110111110

❌ Error Detected in Received Data

EXP-6
Simulate and implement stop and wait protocol for noisy channel
Ans:-
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define TIMEOUT_PROBABILITY 30 // 30% chance of frame loss (simulate noise)

#define TOTAL_FRAMES 5

void send_frame(int frame_no) {


printf("Sender: Sending Frame %d\n", frame_no);

int receive_ack() {

int r = rand() % 100;

if (r < TIMEOUT_PROBABILITY) {

return 0; // Simulate ACK loss

} else {

return 1; // ACK received

void stop_and_wait_arq() {

int frame = 1;

while (frame <= TOTAL_FRAMES) {

send_frame(frame);

printf("Receiver: Waiting for Frame %d...\n", frame);

int ack = receive_ack();

if (ack) {

printf("Receiver: Frame %d received successfully.\n", frame);

printf("Sender: ACK %d received.\n\n", frame);

frame++;

} else {
printf("Receiver: Frame %d lost or corrupted! No ACK sent.\n", frame);

printf("Sender: Timeout! Resending Frame %d...\n\n", frame);

int main() {

srand(time(0));

printf("Simulating Stop-and-Wait ARQ Protocol for %d Frames\n", TOTAL_FRAMES);

stop_and_wait_arq();

return 0;

Output: Simulating Stop-and-Wait ARQ Protocol for 5 Frames

Sender: Sending Frame 1

Receiver: Waiting for Frame 1...

Receiver: Frame 1 received successfully.

Sender: ACK 1 received.

Sender: Sending Frame 2

Receiver: Waiting for Frame 2...

Receiver: Frame 2 received successfully.

Sender: ACK 2 received.

Sender: Sending Frame 3

Receiver: Waiting for Frame 3...

Receiver: Frame 3 received successfully.


Sender: ACK 3 received.

Sender: Sending Frame 4

Receiver: Waiting for Frame 4...

Receiver: Frame 4 lost or corrupted! No ACK sent.

Sender: Timeout! Resending Frame 4...

Sender: Sending Frame 4

Receiver: Waiting for Frame 4...

Receiver: Frame 4 lost or corrupted! No ACK sent.

Sender: Timeout! Resending Frame 4...

Sender: Sending Frame 4

Receiver: Waiting for Frame 4...

Receiver: Frame 4 lost or corrupted! No ACK sent.

Sender: Timeout! Resending Frame 4...

Sender: Sending Frame 4

Receiver: Waiting for Frame 4...

Receiver: Frame 4 received successfully.

Sender: ACK 4 received.

Sender: Sending Frame 5

Receiver: Waiting for Frame 5...

Receiver: Frame 5 lost or corrupted! No ACK sent.

Sender: Timeout! Resending Frame 5...


Sender: Sending Frame 5

Receiver: Waiting for Frame 5...

Receiver: Frame 5 received successfully.

Sender: ACK 5 received.

=== Code Execution Successful ===

EXP-7
Simulate and implement go-back n sliding window protocol
Ans:-
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define TOTAL_FRAMES 10

#define WINDOW_SIZE 4

#define LOSS_PROBABILITY 20 // Probability (%) of a frame getting lost

// Simulate sending a frame

void send_frame(int frame) {

printf("Sender: Sending Frame %d\n", frame);

// Simulate ACK reception

int receive_ack(int frame) {

int r = rand() % 100;

if (r < LOSS_PROBABILITY) {

printf("Receiver: Frame %d lost/corrupted. No ACK sent.\n", frame);


return 0;

} else {

printf("Receiver: Frame %d received. Sending ACK.\n", frame);

return 1;

// Go-Back-N protocol implementation

void go_back_n() {

int base = 1;

int next_frame = 1;

while (base <= TOTAL_FRAMES) {

// Send window

for (int i = 0; i < WINDOW_SIZE && next_frame <= TOTAL_FRAMES; i++) {

send_frame(next_frame);

next_frame++;

// Simulate ACKs

int all_acked = 1;

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

if (!receive_ack(i)) {

all_acked = 0;

printf("Sender: ACK for Frame %d not received. Go back and retransmit from here.\n\
n", i);
next_frame = i; // Go back to this frame

break;

if (all_acked) {

base = next_frame;

printf("Sender: All frames in window acknowledged. Sliding window forward.\n\n");

} else {

printf("Sender: Retransmitting window starting from Frame %d...\n\n", base);

printf("All frames successfully sent and acknowledged.\n");

int main() {

srand(time(NULL)); // Seed for randomness

printf("Simulating Go-Back-N Protocol with %d Frames, Window Size %d\n\n",


TOTAL_FRAMES, WINDOW_SIZE);

go_back_n();

return 0;

Output: Simulating Go-Back-N Protocol with 10 Frames, Window Size 4

Sender: Sending Frame 1


Sender: Sending Frame 2

Sender: Sending Frame 3

Sender: Sending Frame 4

Receiver: Frame 1 received. Sending ACK.

Receiver: Frame 2 received. Sending ACK.

Receiver: Frame 3 received. Sending ACK.

Receiver: Frame 4 received. Sending ACK.

Sender: All frames in window acknowledged. Sliding window forward.

Sender: Sending Frame 5

Sender: Sending Frame 6

Sender: Sending Frame 7

Sender: Sending Frame 8

Receiver: Frame 5 lost/corrupted. No ACK sent.

Sender: ACK for Frame 5 not received. Go back and retransmit from here.

Sender: Retransmitting window starting from Frame 5...

Sender: Sending Frame 5

Sender: Sending Frame 6

Sender: Sending Frame 7

Sender: Sending Frame 8

Receiver: Frame 5 received. Sending ACK.

Receiver: Frame 6 received. Sending ACK.

Receiver: Frame 7 received. Sending ACK.

Receiver: Frame 8 received. Sending ACK.


Sender: All frames in window acknowledged. Sliding window forward.

Sender: Sending Frame 9

Sender: Sending Frame 10

Receiver: Frame 9 lost/corrupted. No ACK sent.

Sender: ACK for Frame 9 not received. Go back and retransmit from here.

Sender: Retransmitting window starting from Frame 9...

Sender: Sending Frame 9

Sender: Sending Frame 10

Receiver: Frame 9 received. Sending ACK.

Receiver: Frame 10 received. Sending ACK.

Sender: All frames in window acknowledged. Sliding window forward.

All frames successfully sent and acknowledged.

=== Code Execution Successful ===

EXP-8
Simulate and implement selective repeat sliding window protocol
Ans:-
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define TOTAL_FRAMES 10

#define WINDOW_SIZE 4

#define LOSS_PROBABILITY 30 // 30% chance a frame is lost


typedef struct {

int frame_no;

int ack;

} Frame;

void send_frame(int frame_no) {

printf("Sender: Sending Frame %d\n", frame_no);

int receive_frame(int frame_no) {

int chance = rand() % 100;

if (chance < LOSS_PROBABILITY) {

printf("Receiver: Frame %d lost/corrupted.\n", frame_no);

return 0;

} else {

printf("Receiver: Frame %d received correctly. Sending ACK.\n", frame_no);

return 1;

void selective_repeat() {

Frame window[WINDOW_SIZE];

int base = 0;

int next_frame = 0;
// Initialize ACK array

for (int i = 0; i < WINDOW_SIZE; i++) {

window[i].ack = 0;

window[i].frame_no = -1;

while (base < TOTAL_FRAMES) {

// Send all frames in the current window that are not ACKed

for (int i = 0; i < WINDOW_SIZE && (base + i) < TOTAL_FRAMES; i++) {

if (!window[i].ack) {

window[i].frame_no = base + i + 1;

send_frame(window[i].frame_no);

// Receive ACKs for all sent frames

for (int i = 0; i < WINDOW_SIZE && (base + i) < TOTAL_FRAMES; i++) {

if (!window[i].ack) {

window[i].ack = receive_frame(window[i].frame_no);

// Slide window forward for ACKed frames

int slide_count = 0;

for (int i = 0; i < WINDOW_SIZE; i++) {

if (window[i].ack) {
slide_count++;

} else {

break;

// Shift window

for (int i = 0; i < WINDOW_SIZE - slide_count; i++) {

window[i] = window[i + slide_count];

for (int i = WINDOW_SIZE - slide_count; i < WINDOW_SIZE; i++) {

window[i].ack = 0;

window[i].frame_no = -1;

base += slide_count;

printf("Sliding window... Base is now Frame %d\n\n", base + 1);

printf("All frames sent and acknowledged successfully using Selective Repeat.\n");

int main() {

srand(time(NULL));

printf("Selective Repeat Protocol Simulation\n");


printf("-------------------------------------\n\n");

selective_repeat();

return 0;

Output: Selective Repeat Protocol Simulation

-------------------------------------

Sender: Sending Frame 1

Sender: Sending Frame 2

Sender: Sending Frame 3

Sender: Sending Frame 4

Receiver: Frame 1 lost/corrupted.

Receiver: Frame 2 received correctly. Sending ACK.

Receiver: Frame 3 received correctly. Sending ACK.

Receiver: Frame 4 lost/corrupted.

Sliding window... Base is now Frame 1

Sender: Sending Frame 1

Sender: Sending Frame 4

Receiver: Frame 1 lost/corrupted.

Receiver: Frame 4 received correctly. Sending ACK.

Sliding window... Base is now Frame 1

Sender: Sending Frame 1

Receiver: Frame 1 received correctly. Sending ACK.

Sliding window... Base is now Frame 5


Sender: Sending Frame 5

Sender: Sending Frame 6

Sender: Sending Frame 7

Sender: Sending Frame 8

Receiver: Frame 5 received correctly. Sending ACK.

Receiver: Frame 6 received correctly. Sending ACK.

Receiver: Frame 7 received correctly. Sending ACK.

Receiver: Frame 8 received correctly. Sending ACK.

Sliding window... Base is now Frame 9

Sender: Sending Frame 9

Sender: Sending Frame 10

Receiver: Frame 9 received correctly. Sending ACK.

Receiver: Frame 10 received correctly. Sending ACK.

Sliding window... Base is now Frame 11

All frames sent and acknowledged successfully using Selective Repeat.

=== Code Execution Successful ===

EXP-9
Simulate and implement distance vector routing algorithm
Ans:-
#include <stdio.h>

#define INFINITY 9999

#define MAX 10
int dist[MAX][MAX], via[MAX][MAX], n;

void distanceVectorRouting() {

int i, j, k, count = 0;

do {

count = 0;

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

for (k = 0; k < n; k++) {

if (dist[i][j] > dist[i][k] + dist[k][j]) {

dist[i][j] = dist[i][k] + dist[k][j];

via[i][j] = k;

count++;

} while (count != 0);

void printRoutingTable() {

printf("\nDistance Vector Routing Tables:\n");

for (int i = 0; i < n; i++) {

printf("\nFor Router %d:\n", i + 1);


printf("Destination\tDistance\tNext Hop\n");

for (int j = 0; j < n; j++) {

if (i != j) {

printf("Router %d\t\t%d\t\tRouter %d\n", j + 1, dist[i][j], via[i][j] + 1);

int main() {

int i, j;

printf("Enter number of routers: ");

scanf("%d", &n);

printf("Enter cost matrix (use %d for no direct link):\n", INFINITY);

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

scanf("%d", &dist[i][j]);

if (dist[i][j] == INFINITY || i == j)

via[i][j] = -1;

else

via[i][j] = j;

}
distanceVectorRouting();

printRoutingTable();

return 0;

Output: Enter number of routers: 3

Enter cost matrix (use 9999 for no direct link):

013

101

310

Distance Vector Routing Tables:

For Router 1:

Destination Distance Next Hop

Router 2 1 Router 2

Router 3 2 Router 2

For Router 2:

Destination Distance Next Hop

Router 1 1 Router 1

Router 3 1 Router 3

For Router 3:

Destination Distance Next Hop

Router 1 2 Router 2
Router 2 1 Router 2

=== Code Execution Successful ===

You might also like