[go: up one dir, main page]

0% found this document useful (0 votes)
17 views12 pages

Compnt

The document outlines a series of steps to set up a development environment for network simulation, including the installation of necessary tools and the NS2 network simulator. It provides code examples for various error detection and correction methods such as Even Parity, Two-Dimensional Parity, Checksum, Hamming Code, Cyclic Redundancy Check (CRC), and the Stop-and-Wait protocol. Each section includes code snippets, input/output examples, and explanations of the algorithms used for simulating network communication and error handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

Compnt

The document outlines a series of steps to set up a development environment for network simulation, including the installation of necessary tools and the NS2 network simulator. It provides code examples for various error detection and correction methods such as Even Parity, Two-Dimensional Parity, Checksum, Hamming Code, Cyclic Redundancy Check (CRC), and the Stop-and-Wait protocol. Each section includes code snippets, input/output examples, and explanations of the algorithms used for simulating network communication and error handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 12

STEP 1: SET UP YOUR DEVELOPMENT TOOLS

🔹 A. Install C Compiler
sudo apt update
STEP 2:sudo apt install build-essential gedit
INSTALL NS2 NETWORK SIMULATOR
🔹 A. Install NS2 and NAM
sudo apt install ns2 nam
ns//Verify Installation
step3
Create a folder for your lab work:
mkdir ~/cn_lab
cd ~/cn_lab
file
~/cn_lab/
├── 1_parity.c
├── 2_2D_parity.c
├── 3_checksum.c
├── 4_hamming.c
├── 5_crc.c
├── 6_stop_and_wait.c
├── 7_go_back_n.c
├── 8_selective_repeat.c
└── 9_distance_vector.tcl

1. Simulate Even Parity generator and checker.


#include <stdio.h>
#include <string.h>
int calculateParity(char data[]) {
int count = 0;
for (int i = 0; i < strlen(data); i++) {
if (data[i] == '1')
count++;
}
return (count % 2 == 0) ? 0 : 1; // return parity bit
}
int main() {
char data[100];
char received[100];
printf("=== EVEN PARITY GENERATOR ===\n");
printf("Enter binary data: ");
scanf("%s", data);
int parity = calculateParity(data);
printf("Parity bit (even): %d\n", parity);
char transmitted[101];
strcpy(transmitted, data);
int len = strlen(transmitted);
transmitted[len] = parity + '0'; // convert int to char
transmitted[len + 1] = '\0';
printf("Transmitted data with parity: %s\n", transmitted);
printf("\n=== EVEN PARITY CHECKER ===\n");
printf("Enter received data (with parity bit): ");
scanf("%s", received);
int check = calculateParity(received);
if (check == 0)
printf("No error detected in received data.\n");
else
printf("Error detected in received data.\n");

return 0;
}
Output:
=== EVEN PARITY GENERATOR ===
Enter binary data: 1011
Parity bit (even): 1
Transmitted data with parity: 10111

=== EVEN PARITY CHECKER ===


Enter received data (with parity bit): 10111
No error detected in received data.

2. Simulate two dimensional Parity generator and checker.


#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int data[ROWS][COLS];
int rowParity[ROWS] = {0};
int colParity[COLS] = {0};
printf("Enter a %dx%d binary data matrix (only 0s and 1s):\n", ROWS, COLS);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &data[i][j]);
if (data[i][j] != 0 && data[i][j] != 1) {
printf("Invalid input! Only binary values allowed.\n");
return 1;
}
rowParity[i] ^= data[i][j];
colParity[j] ^= data[i][j];
}
}
printf("\nData 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", rowParity[i]);
printf("\n");
}

// Display column parity


for (int j = 0; j < COLS; j++) {
printf("--");
}
printf("\n");
for (int j = 0; j < COLS; j++) {
printf("%d ", colParity[j]);
}
printf(" <- Column Parity\n");
int error = 0;
printf("\nEnter received matrix (with same size %dx%d):\n", ROWS, COLS);
int recv[ROWS][COLS];
int rParity[ROWS] = {0};
int cParity[COLS] = {0};
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &recv[i][j]);
rParity[i] ^= recv[i][j];
cParity[j] ^= recv[i][j];
}
}
printf("\nChecking Received Data...\n");
for (int i = 0; i < ROWS; i++) {
if (rParity[i] != rowParity[i]) {
printf("Error detected in Row %d\n", i);
error = 1;
}
}
for (int j = 0; j < COLS; j++) {
if (cParity[j] != colParity[j]) {
printf("Error detected in Column %d\n", j);
error = 1;
}
}
if (!error)
printf("No error detected in received data.\n");
return 0;
}
input:
Enter a 3x3 binary data matrix:
1 0 1
0 1 1
1 1 0
Output:
Data with Row Parity:
1 0 1 | 0
0 1 1 | 0
1 1 0 | 0
------
1 0 0 <- Column Parity

Enter received matrix:


1 0 1
0 1 1
1 0 0
Checking Received Data...
Error detected in Row 2
Error detected in Column 1

3. Simulate checksum generator and checker.


#include <stdio.h>
#define SIZE 4
#define BLOCKS 3
void binaryAdd(int a[], int b[], int result[]) {
int carry = 0;
for (int i = SIZE - 1; i >= 0; i--) {
int sum = a[i] + b[i] + carry;
result[i] = sum % 2;
carry = sum / 2;
}
if (carry) {
for (int i = SIZE - 1; i >= 0; i--) {
int sum = result[i] + carry;
result[i] = sum % 2;
carry = sum / 2;
}
}
}
void onesComplement(int a[]) {
for (int i = 0; i < SIZE; i++) {
a[i] = a[i] == 0 ? 1 : 0;
}
}
void readBlock(int block[]) {
for (int i = 0; i < SIZE; i++) {
scanf("%d", &block[i]);
if (block[i] != 0 && block[i] != 1) {
printf("Invalid input! Use only 0s and 1s.\n");
i--;
}
}
}
int main() {
int data[BLOCKS][SIZE];
int sum[SIZE] = {0};
int checksum[SIZE];
printf("Enter %d blocks of %d-bit binary data:\n", BLOCKS, SIZE);
for (int i = 0; i < BLOCKS; i++) {
printf("Block %d: ", i + 1);
readBlock(data[i]);
binaryAdd(sum, data[i], sum);
}
for (int i = 0; i < SIZE; i++) {
checksum[i] = sum[i];
}
onesComplement(checksum);
printf("\nChecksum: ");
for (int i = 0; i < SIZE; i++) {
printf("%d", checksum[i]);
}
printf("\n\nReceiver Side:\nEnter %d data blocks and 1 checksum block:\n",
BLOCKS);
int recv[BLOCKS + 1][SIZE], recvSum[SIZE] = {0};

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


printf("Block %d: ", i + 1);
readBlock(recv[i]);
binaryAdd(recvSum, recv[i], recvSum);
}
int valid = 1;
for (int i = 0; i < SIZE; i++) {
if (recvSum[i] != 1) {
valid = 0;
break;
}
}
if (valid)
printf("\nNo Error: Data received correctly.\n");
else
printf("\nError Detected in received data.\n");
return 0;
}
Input:
Enter 3 blocks of 4-bit binary data:
Block 1: 1 0 1 0
Block 2: 0 1 1 0
Block 3: 1 1 0 1
Checksum: 0 1 1 0
Receiver:
Enter 3 data blocks and 1 checksum block:
Block 1: 1 0 1 0
Block 2: 0 1 1 0
Block 3: 1 1 0 1
Block 4: 0 1 1 0

No Error: Data received correctly.

4. Simulate Hamming code method.


#include <stdio.h>
#include <math.h>
int calculateParity(int position, int code[], int size) {
int parity = 0;
for (int i = position; i <= size; i += 2 * position) {
for (int j = i; j < i + position && j <= size; j++) {
parity ^= code[j];
}
}
return parity;
}

int main() {
int data[20], code[20];
int m, r = 0, n, i, j, k = 0;
printf("Enter the number of data bits: ");
scanf("%d", &m);
printf("Enter the data bits (LSB to MSB): ");
for (i = 0; i < m; i++) {
scanf("%d", &data[i]);
}
while ((1 << r) < (m + r + 1)) {
r++;
}
n = m + r;
for (i = 1, j = 0; i <= n; i++) {
if ((i & (i - 1)) == 0) {
code[i] = 0; // parity bit placeholder
} else {
code[i] = data[j++];
}
}
for (i = 1; i <= n; i <<= 1) {
code[i] = calculateParity(i, code, n);
}
printf("\nHamming Code (1-indexed): ");
for (i = 1; i <= n; i++) {
printf("%d ", code[i]);
}
printf("\n");
int received[20];
printf("\nEnter received code of %d bits:\n", n);
for (i = 1; i <= n; i++) {
scanf("%d", &received[i]);
}

int errorPos = 0;
for (i = 1; i <= n; i <<= 1) {
int parity = calculateParity(i, received, n);
if (parity != 0) {
errorPos += i;
}
}

if (errorPos == 0) {
printf("No error detected in the received code.\n");
} else {
printf("Error detected at position: %d\n", errorPos);
// Correct the error
received[errorPos] ^= 1;
printf("Corrected code: ");
for (i = 1; i <= n; i++) {
printf("%d ", received[i]);
}
printf("\n");
}
return 0;
}
Input:
Enter the number of data bits: 4
Enter the data bits (LSB to MSB): 1 0 1 1
Output:
Hamming Code: 0 1 1 0 1 0 1
Simulate receiving with error:
Enter received code of 7 bits:
0 1 1 1 1 0 1
Error detected at position: 4
Corrected code: 0 1 1 0 1 0 1

5. Simulate Cyclic Redundancy Check (CRC) error detection algorithm for noisy
channel.
#include <stdio.h>
#include <string.h>
void xor(char *dividend, char *divisor, int len) {
for (int i = 0; i < len; i++) {
dividend[i] = (dividend[i] == divisor[i]) ? '0' : '1';
}
}
int main() {
char data[100], generator[50], temp[100], remainder[50];
int dataLen, genLen;
printf("Enter data bits: ");
scanf("%s", data);
printf("Enter generator polynomial bits: ");
scanf("%s", generator);
dataLen = strlen(data);
genLen = strlen(generator);
strcpy(temp, data);
for (int i = 0; i < genLen - 1; i++) {
temp[dataLen + i] = '0';
}
temp[dataLen + genLen - 1] = '\0';
char curr[50];
for (int i = 0; i <= dataLen; i++) {
strncpy(curr, temp + i, genLen);
curr[genLen] = '\0';
if (curr[0] == '1') {
xor(curr, generator, genLen);
}
for (int j = 1; j < genLen; j++) {
temp[i + j] = curr[j];
}
}
strncpy(remainder, temp + dataLen, genLen - 1);
remainder[genLen - 1] = '\0';
printf("\nCRC bits: %s\n", remainder);
printf("Transmitted code: %s%s\n", data, remainder);
char received[100];
printf("\nEnter received code: ");
scanf("%s", received);
strcpy(temp, received);
int recvLen = strlen(received);
for (int i = 0; i <= recvLen - genLen; i++) {
strncpy(curr, temp + i, genLen);
curr[genLen] = '\0';
if (curr[0] == '1') {
xor(curr, generator, genLen);
}

for (int j = 1; j < genLen; j++) {


temp[i + j] = curr[j];
}
}
int error = 0;
for (int i = recvLen - genLen + 1; i < recvLen; i++) {
if (temp[i] != '0') {
error = 1;
break;
}
}

if (error)
printf("Error detected in received code.\n");
else
printf("No error detected. Data is correct.\n");

return 0;
}
Input:
Enter data bits: 1001
Enter generator polynomial bits: 1101
Output:
CRC bits: 111
Transmitted code: 1001111

Enter received code: 1001111


No error detected. Data is correct.
With error:
Enter received code: 1001101
Error detected in received code.

6. Simulate and implement stop and wait protocol for noisy channel.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int main() {
int frames;
srand(time(NULL));
printf("Enter number of frames to send: ");
scanf("%d", &frames);
for (int i = 0; i < frames; i++) {
printf("\nSending Frame %d...", i);
sleep(1); // Simulate transmission delay
int ackLost = rand() % 2;
if (ackLost == 0) {
printf("\nACK for Frame %d received.\n", i);
} else {
printf("\nACK for Frame %d lost! Resending...\n", i);
sleep(1);
printf("Resending Frame %d...\n", i);
sleep(1);
printf("ACK for Frame %d received.\n", i);
}
}
printf("\nAll frames sent successfully using Stop-and-Wait ARQ.\n");
return 0;
}
Output
Enter number of frames to send: 4
Sending Frame 0...
ACK for Frame 0 received.
Sending Frame 1...
ACK for Frame 1 lost! Resending...
Resending Frame 1...
ACK for Frame 1 received.
Sending Frame 2...
ACK for Frame 2 received.
Sending Frame 3...
ACK for Frame 3 lost! Resending...
Resending Frame 3...
ACK for Frame 3 received.
All frames sent successfully using Stop-and-Wait ARQ.

7. Simulate and implement go back n sliding window protocol.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int main() {
int totalFrames, windowSize;
srand(time(NULL));
printf("Enter total number of frames to send: ");
scanf("%d", &totalFrames);
printf("Enter window size: ");
scanf("%d", &windowSize);
int sent = 0;
while (sent < totalFrames) {
printf("\nSending frames %d to %d\n", sent, (sent + windowSize - 1 <
totalFrames) ? sent + windowSize - 1 : totalFrames - 1);
int lostAck = sent + rand() % windowSize;
if (lostAck >= totalFrames) lostAck = totalFrames - 1;
for (int i = sent; i < sent + windowSize && i < totalFrames; i++) {
printf("Frame %d sent.\n", i);
sleep(1); // Simulate delay
}
for (int i = sent; i < sent + windowSize && i < totalFrames; i++) {
if (i == lostAck) {
printf("ACK for Frame %d lost! Go-Back-N triggered...\n", i);
break;
} else {
printf("ACK for Frame %d received.\n", i);
sent++;
}
}
if (sent < totalFrames && sent != totalFrames) {
printf("Resending from Frame %d...\n", sent);
}
}
printf("\nAll frames sent successfully using Go-Back-N ARQ.\n");
return 0;
}
Output:
Enter total number of frames to send: 6
Enter window size: 3
Sending frames 0 to 2
Frame 0 sent.
Frame 1 sent.
Frame 2 sent.
ACK for Frame 0 received.
ACK for Frame 1 lost! Go-Back-N triggered...
Resending from Frame 1...
Sending frames 1 to 3
Frame 1 sent.
Frame 2 sent.
Frame 3 sent.
ACK for Frame 1 received.
ACK for Frame 2 received.
ACK for Frame 3 received.

8. Simulate and implement selective repeat sliding window protocol.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define MAX 100
int main() {
int totalFrames, windowSize, sent = 0;
int ack[MAX] = {0};
srand(time(NULL));
printf("Enter total number of frames to send: ");
scanf("%d", &totalFrames);
printf("Enter window size: ");
scanf("%d", &windowSize);
while (sent < totalFrames) {
printf("\nSending window: Frames %d to %d\n", sent, (sent + windowSize - 1
< totalFrames) ? sent + windowSize - 1 : totalFrames - 1);
for (int i = sent; i < sent + windowSize && i < totalFrames; i++) {
if (ack[i] == 0) {
printf("Sending Frame %d\n", i);
sleep(1);
}
}
for (int i = sent; i < sent + windowSize && i < totalFrames; i++) {
if (ack[i] == 0) {
int isAckLost = rand() % 2;

if (isAckLost) {
printf("ACK for Frame %d lost! Will retransmit.\n", i);
ack[i] = 0;
} else {
printf("ACK for Frame %d received.\n", i);
ack[i] = 1;
}
}
}
while (ack[sent] == 1 && sent < totalFrames) {
sent++;
}
}
printf("\nAll frames sent and acknowledged successfully using Selective Repeat
ARQ.\n");
return 0;
}
Output:
Enter total number of frames to send: 6
Enter window size: 3

Sending window: Frames 0 to 2


Sending Frame 0
Sending Frame 1
Sending Frame 2
ACK for Frame 0 received.
ACK for Frame 1 lost! Will retransmit.
ACK for Frame 2 received.

Sending window: Frames 1 to 3


Sending Frame 1
Sending Frame 3
ACK for Frame 1 received.
ACK for Frame 3 received.

Sending window: Frames 4 to 5


Sending Frame 4
Sending Frame 5
ACK for Frame 4 received.
ACK for Frame 5 received.

9. Simulate and implement distance vector routing algorithm.


#include <stdio.h>
#define MAX 10
#define INF 9999
int main() {
int distance[MAX][MAX], via[MAX][MAX];
int n;
printf("Enter the number of routers: ");
scanf("%d", &n);
printf("Enter the cost matrix (use 9999 for infinity):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &distance[i][j]);
if (i == j)
via[i][j] = i;
else if (distance[i][j] != INF)
via[i][j] = j;
else
via[i][j] = -1;
}
}
int updated;
do {
updated = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (distance[i][k] + distance[k][j] < distance[i][j]) {
distance[i][j] = distance[i][k] + distance[k][j];
via[i][j] = via[i][k];
updated = 1;
}
}
}
}
} while (updated);
printf("\nFinal Routing Tables:\n");
for (int i = 0; i < n; i++) {
printf("\nRouter %d:\n", i);
printf("Destination\tNext Hop\tDistance\n");
for (int j = 0; j < n; j++) {
if (distance[i][j] == INF)
printf("%d\t\t-\t\t∞\n", j);
else
printf("%d\t\t%d\t\t%d\n", j, via[i][j], distance[i][j]);
}
}
return 0;
}
Input:
Enter the number of routers: 3
Enter the cost matrix:
0 2 7
2 0 1
7 1 0
Output
Final Routing Tables:

Router 0:
Destination Next Hop Distance
0 0 0
1 1 2
2 1 3

Router 1:
Destination Next Hop Distance
0 0 2
1 1 0
2 2 1

Router 2:
Destination Next Hop Distance
0 1 3
1 1 1
2 2 0

You might also like