Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

COLab File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

COMPUTER NETWORKS LAB

CO-306

Computer Science & Engineering Department

Submitted To: Submitted by:


Prof. Anil Singh Parihar Vishal Verma

Professor 2K21/EC/249

Delhi Technological University

(Formerly Delhi College of Engineering)

Shahbad Daulatpur, Bawana Road, Delhi 110042

1
EXPERIMENT-1

Aim:- Write a program to implement bit stuffing.

CODE-
#include <iostream>
#include <vector>
using namespace std;
vector<int> bitStuffing(const vector<int> &data) {
vector<int> stuffedData;
int count = 0;
for (int bit : data) {
stuffedData.push_back(bit);
if (bit == 1) {
count++;
if (count == 5) {
stuffedData.push_back(0);
count = 0; // Reset count after stuffing
}
} else {
count = 0; // Reset count if bit is 0
}
}
return stuffedData;
}
int main() {
vector<int> data = {1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0};
vector<int> stuffedData = bitStuffing(data);
cout << "Original Data: ";
for (int bit : data)
cout << bit;
cout << endl;
cout << "Stuffed Data: ";
for (int bit : stuffedData)
cout << bit;
cout << endl
return 0;
}
OUTPUT-

LEARNING OUTCOME: We successfully implemented the


program and understood how to implement bit stuffing.
EXPERIMENT-2

Aim:- Write a program for byte stuffing.

CODE-

#include <iostream>
#include <string.h>
using namespace std;
int main() {
char a[50], b[100] = "", sd, ed;
int size, j = 0;
cout << "\n Enter input string :";
cin >> a;
size = strlen(a);
cout << " \n Enter starting delimiter : ";
cin >> sd;
cout << " \n Enter ending delimiter : ";
cin >> ed;
b[j++] = sd;
for (int i = 0; i < size; i++) {
if (a[i] == sd) {
b[j++] = sd;
}
b[j++] = a[i];
}
b[j++] = ed;
cout << "\n After Character Stuffing :";
cout << b << endl;
return 0;
}
OUTPUT-

LEARNING OUTCOME: In this experiment, we implemented


byte stuffing.
EXPERIMENT-3

AIM: Write A program for Cyclic Redundancy check

CODE-
#include <iostream>
#include <string>
using namespace std;
string xor1(string a, string b) {
string result = "";
int n = b.length();
for (int i = 1; i < n; i++) {
if (a[i] == b[i])
result += "0";
else
result += "1";
}
return result;
}
string mod2div(string divident, string divisor) {
int pick = divisor.length();
string tmp = divident.substr(0, pick);
int n = divident.length();
while (pick < n) {
if (tmp[0] == '1')
tmp = xor1(divisor, tmp) + divident[pick];
else
tmp = xor1(string(pick, '0'), tmp) + divident[pick];
pick += 1;
}
if (tmp[0] == '1')
tmp = xor1(divisor, tmp);
else
tmp = xor1(string(pick, '0'), tmp);
return tmp;
}
void encodeData(string data, string key) {
int l_key = key.length();
string appended_data = (data + string(l_key - 1, '0'));
string remainder = mod2div(appended_data, key);
string codeword = data + remainder;
cout << "Remainder : " << remainder << "\n";
cout << "Encoded Data (Data + Remainder) :" << codeword << "\n";
}
int main() {
string data;
cout << "Enter data : ";
cin >> data;
string key;
cout << "Enter key : ";
cin >> key;
encodeData(data, key);
return 0;
}

OUTPUT-

LEARNING OUTCOME: We learnt the implementation of


CRC(cyclic redundancy check).
EXPERIMENT-4

AIM: Write A program for the stop and wait protocol.

CODE-

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <time.h>
#include <unistd.h>
using namespace std;
class timer {
private:
unsigned long begTime;
public:
void start() { begTime = clock(); }
unsigned long elapsedTime() {
return ((unsigned long)clock() - begTime) / CLOCKS_PER_SEC;
}
bool isTimeout(unsigned long seconds) { return seconds >= elapsedTime(); }
};
int main() {
int frames[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
unsigned long seconds = 5;
srand(time(NULL));
timer t;
cout << "Sender has to send frames : ";
for (int i = 0; i < 10; i++)
cout << frames[i] << " ";
cout << endl;
int count = 0;
bool delay = false;
cout << endl << "Sender\t\t\t\t\tReceiver" << endl;
do {
bool timeout = false;
cout << "Sending Frame : " << frames[count];
cout.flush();
cout << "\t\t";
t.start();
if (rand() % 2) {
int to = 24600 + rand() % (64000 - 24600) + 1;
for (int i = 0; i < 64000; i++)
for (int j = 0; j < to; j++) {
}
}
if (t.elapsedTime() <= seconds) {
cout << "Received Frame : " << frames[count] << " ";
if (delay) {
cout << "Duplicate";
delay = false;
}
cout << endl;
count++;
} else {
cout << "---" << endl;
cout << "Timeout" << endl;
timeout = true;
}
t.start();
if (rand() % 2 || !timeout) {
int to = 24600 + rand() % (64000 - 24600) + 1;
for (int i = 0; i < 64000; i++)
for (int j = 0; j < to; j++) {
}
if (t.elapsedTime() > seconds) {
cout << "Delayed Ack" << endl;
count--;
delay = true;
} else if (!timeout)
cout << "Acknowledgement : " << frames[count] - 1 << endl;
}
} while (count != 10);
return 0;
}
OUTPUT-

LEARNING OUTCOME: In this experiment we learnt to


implement a stop and wait protocol.
EXPERIMENT-5
AIM: Write A program sliding window protocol(Go-Back-N
ARQ)
CODE-

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
int nf, N;
int no_tr = 0;
srand(time(NULL));
cout << "Enter the number of frames : ";
cin >> nf;
cout << "Enter the Window Size : ";
cin >> N;
int i = 1;
while (i <= nf) {
int x = 0;
for (int j = i; j < i + N && j <= nf; j++) {
cout << "Sent Frame " << j << endl;
no_tr++;
}
for (int j = i; j < i + N && j <= nf; j++) {
int flag = rand() % 2;
if (!flag) {
cout << "Acknowledgment for Frame " << j << endl;
x++;
} else {
cout << "Frame " << j << " Not Received" << endl;
cout << "Retransmitting Window" << endl;
break;
}
}
cout << endl;
i += x;
}
cout << "Total number of transmissions : " << no_tr << endl;
return 0;
}
OUTPUT-

LEARNING OUTCOME: In this experiment we learnt to


implement sliding window protocol.
EXPERIMENT-6
AIM: Write A program of sliding window protocol(Selective
Repeat ARQ)
CODE-

#include <iostream>
#include <cstdlib>
using namespace std;
int simulate(int);
int receiver(int);
int negack(int);
int main() {
int tmp1, tmp2, tmp3, tmp4 = 0, tmp5 = 0, i, windowsize = 4, noofPacket,
morePacket;
for (int i = 0; i < 10; i++)
rand();
noofPacket = rand() % 10;
cout << "Number of frames is: " << noofPacket << endl;
morePacket = noofPacket;
while (morePacket > 0) {
tmp1 = simulate(windowsize);
windowsize -= tmp1;
tmp4 += tmp1;
if (tmp4 > noofPacket)
tmp4 = noofPacket;
for (i = noofPacket - morePacket + 1; i <= tmp4; i++)
cout << "Sending Frame " << i << endl;
tmp2 = receiver(tmp1);
tmp3 += tmp2;
if (tmp3 > noofPacket)
tmp3 = noofPacket;
tmp2 = negack(tmp1);
tmp5 += tmp2;
if (tmp5 != 0) {
cout << "No acknowledgement for the frame " << tmp5 << endl;
cout << "Retransmitting frame " << tmp5 << endl;
}
morePacket -= tmp1;
if (windowsize <= 0)
windowsize = 4;
}
cout << "Selective Repeat Protocol Ends. All packets are successfully
transmitted." << endl;
return 0;
}
int receiver(int tmp1) {
int i;
for (i = 0; i < 5; i++)
rand();
i = rand() % tmp1;
return i;
}
int negack(int tmp1) {
int i;
for (i = 0; i < 5; i++)
rand();
i = rand() % tmp1;
return i;
}
int simulate(int windowsize) {
int tmp1, i;
for (i = 0; i < 5; i++)
tmp1 = rand();
if (tmp1 == 0)
tmp1 = simulate(windowsize);
i = tmp1 % windowsize;
if (i == 0)
return windowsize;
else
return tmp1 % windowsize;
}
OUTPUT-

LEARNING OUTCOME: In this experiment we learnt to


implement sliding window protocol
EXPERIMENT-7

AIM:Write a program to show network class of IPv4 address.


CODE-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char findClass(char str[]);
void separate(char str[], char ipClass);
int main() {
char str[30];
printf("Enter the IP address: ");
(void)scanf("%29s", str); // Cast to void to suppress the warning
char ipClass = findClass(str);
printf("Given IP address belongs to Class %c\n", ipClass);
separate(str, ipClass);
return 0;
}
char findClass(char str[]) {
char arr[4];
int i = 0;
while (str[i] != '.') {
arr[i] = str[i];
i++;
}
arr[i] = '\0';
int ip = atoi(arr);
if (ip >= 1 && ip <= 126)
return 'A';
else if (ip >= 128 && ip <= 191)
return 'B';
else if (ip >= 192 && ip <= 223)
return 'C';
else if (ip >= 224 && ip <= 239)
return 'D';
else
return 'E';
}
void separate(char str[], char ipClass) {
char network[30], host[30];
memset(network, 0, sizeof(network));
memset(host, 0, sizeof(host));
int i = 0, j = 0, dotCount = 0;
while (str[j] != '.' && str[j] != '\0') {
network[i++] = str[j++];
}
while (str[j] != '\0') {
if (str[j] == '.') {
dotCount++;
if (dotCount ==
(ipClass - 'A' + 1)) { // ipClass - 'A' + 1 to get the number of dots
j++;
break;
}
}
host[i++] = str[j++];
}
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}

OUTPUT-

LEARNING OUTCOME: In this experiment we learnt to write a


program to show network class of of IPv4 address
EXPERIMENT-8

AIM:Write a program to show net ID and host id of IPv4


address.
CODE-

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char findClass(char str[]);
void separate(char str[], char ipClass);
int main() {
char str[30];
printf("Enter the IP address: ");
(void)scanf("%29s", str); // Limit input length to prevent buffer overflow
char ipClass = findClass(str);
printf("Given IP address belongs to Class %c\n", ipClass);
separate(str, ipClass);
return 0;
}
char findClass(char str[]) {
char arr[4];
int i = 0;
while (str[i] != '.') {
arr[i] = str[i];
i++;
}
arr[i] = '\0';
int ip = atoi(arr);
// Class A
if (ip >= 1 && ip <= 126)
return 'A';
// Class B
else if (ip >= 128 && ip <= 191)
return 'B';
// Class C
else if (ip >= 192 && ip <= 223)
return 'C';
// Class D
else if (ip >= 224 && ip <= 239)
return 'D';
// Class E
else
return 'E';
}
void separate(char str[], char ipClass) {
char network[12], host[12];
memset(network, 0, sizeof(network));
memset(host, 0, sizeof(host));
int i = 0, j = 0, dotCount = 0;
while (str[j] != '.' && str[j] != '\0') {
network[i++] = str[j++];
}
while (str[j] != '\0') {
if (str[j] == '.') {
dotCount++;
if (dotCount ==
(ipClass - 'A' + 1)) { // ipClass - 'A' + 1 to get the number of dots
j++;
break;
}
}
host[i++] = str[j++];
}
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}
OUTPUT-

LEARNING OUTCOME: In this experiment we learnt to write a


program to show net ID of IPv4 address.
EXPERIMENT-9
AIM:Write a program for distance vector routing protocol.
CODE-

#include <stdio.h>
struct node {
unsigned dist[20];
unsigned from[20];
} rt[10];
int main() {
int costmat[20][20];
int nodes, i, j, k;
printf("\nEnter the number of nodes : ");
(void) scanf("%d", &nodes);
printf("\nEnter the cost matrix :\n");
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
(void)scanf("%d", &costmat[i][j]);
rt[i].dist[j] = costmat[i][j];
rt[i].from[j] = j;
}
}
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
for (k = 0; k < nodes; k++) {
if (rt[i].dist[j] > costmat[i][k] + rt[k].dist[j]) {
rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j];
rt[i].from[j] = k;
}
}
}
}

printf("\n\n");
for (i = 0; i < nodes; i++) {
printf("\n\n For router %d\n", i + 1);
for (j = 0; j < nodes; j++) {
printf("\t\nnode %d via %d Distance %d ", j + 1, rt[i].from[j] + 1,
rt[i].dist[j]);
}
}
printf("\n\n");
return 0;
}

OUTPUT-

LEARNING OUTCOME: In this experiment we learnt to


implement distance vector routing protocol.
EXPERIMENT-10
AIM:Write a program for link state routing protocol.
CODE-
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char *argv[]) {
int count, src_router, i, j, w, v, min;
int cost_matrix[100][100], dist[100], last[100];
int flag[100];
cout << "Enter the no of routers : ";
cin >> count;
cout << "Enter the cost matrix values :" << endl;
for (i = 0; i < count; i++) {
for (j = 0; j < count; j++) {
cin >> cost_matrix[i][j];
if (cost_matrix[i][j] < 0) {
cost_matrix[i][j] = 1000;
}
}
}
cout << "Enter the source router : ";
cin >> src_router;
for (v = 0; v < count; v++) {
flag[v] = 0;
last[v] = src_router;
dist[v] = cost_matrix[src_router][v];
}
flag[src_router] = 1;
for (i = 0; i < count; i++) {
min = 1000;
for (w = 0; w < count; w++) {
if (!flag[w] && dist[w] < min) {
v = w;
min = dist[w];
}
}
flag[v] = 1;
for (w = 0; w < count; w++) {
if (!flag[w]) {
if (min + cost_matrix[v][w] < dist[w]) {
dist[w] = min + cost_matrix[v][w];
last[w] = v;
}
}
}
}
for (i = 0; i < count; i++) {
cout << endl;
cout << src_router << "==> " << i << " :Path taken : " << i << endl;
w = i;
while (w != src_router) {
cout << "<--" << last[w];
w = last[w];
}
cout << "Shortest path cost : " << dist[i] << endl;
}
return 0;
}

OUTPUT-

You might also like