You are on page 1of 27

DELHI TECHNOLOGICAL UNIVERSITY

(Formerly Delhi College of Engineering)

Shahbad Daulatpur, Bawana Road,

Delhi-110042

Department of Computer Science

LAB FILE (Computer Networks)

Submitted To: Submitted By:

Prof. Rahul Katarya Name: Suyash Shringi


Dept. of Computer Roll No. 2K21/CO/487
Science and Engineering
INDEX

S.no. Experiment Done on Signature

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.
#include<iostream>
#include<string>
using namespace std;
int main() {
int a[20], b[30], i, j, k, count, n;
cout<<"Enter frame size:";
cin>>n;
cout<<"Enter the frame in the form of 0 and 1 :";
for(i=0; i<n; i++)
cin>>a[i];
i=0;
count=1;
j=0;
while(i<n){
if(a[i]==1){
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++){
j++;
b[j]=a[k];
count++;
if(count==5){
j++;
b[j]=0;
}
i=k;
}
}
else{
b[j]=a[i];
}
i++;
j++;
}
cout<<"After Bit Stuffing :";
for(i=0; i<j; i++)
cout<<b[i];
return 0;
}
#include <iostream>
using namespace std;

int main() {
string str;
cout << "Enter the input Data having 0s and 1s only:";
cin >> str;
string stuff = "";
string add = "01111110";
stuff += add;
int i = 0;
while (i < str.length()) {
if (str.length() - i >= 8 && str.substr(i, 8) == "01111110"){ stuff +=
str.substr(i, 8);
stuff += add;
i = i + 8;
}
else
stuff += str[i++];
}
stuff += add;
cout << "The stuffed bit string is: " << stuff << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
int i, j, n, p, count = 0;
char a[30], b[4] = "dle";
printf("Enter the string: ");
scanf("%s", a);
n = strlen(a);
printf("length is %d \n", n);
printf("\nframe after stuffing:\n");
printf("dlestx");
for (i = 0; i < n; i++){
count = 0;
p = i;
for (int j = 0; j < 3; j++){
if (a[i] == b[j]){
count = count + 1;
i++;
} }
if (count != 3){
i = p;
}
if (count == 3){
printf("dledle");
}
else{
printf("%c", a[i]); } }
printf("dleetx\n");
return 0;
}
#include <iostream>
using namespace std;
int main() {
int w, i, f, frames[50];
cout << "Enter window size: ";
cin >> w;
cout << "\nEnter number of frames to transmit: ";
cin >> f;
cout << "\nEnter " << f << " frames: ";
for (i = 1; i <= f; i++)
cin >> frames[i];
cout << "\nWith sliding window protocol the frames will be sent in the
following manner (assuming no corruption of frames)\n\n";
cout << "After sending " << w << " frames at each stage sender waits
for acknowledgement sent by the receiver\n\n";
for (i = 1; i <= f; i++) {
if (i % w == 0) {
cout << frames[i] << "\n";
cout << "Acknowledgement of above frames sent is received by
sender\n\n";
}
else
cout << frames[i] << " ";
}
if (f % w != 0)
cout << "\nAcknowledgement of above frames sent is received by
sender\n";
return 0;
}
#include<bits/stdc++.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; }
#include<bits/stdc++.h>
using namespace std;

string xorem(string input,string key){


int key_len=key.length();
int n=input.length();
for(int i=0;i<n-key_len+1;i++){
for(int j=0;j<key_len;j++){
input[i+j]=input[i+j]==key[j]?'0':'1'; }
for(;i<n&&input[i]!='1';i++);
if(input[i]=='1')
i--; }
return input.substr(n-key_len+1);
}

int main(){
string data,gen;
cout<<"Enter data:";
cin>>data;
cout<<"Enter Key:";
cin>>gen;
string temp=data;
for(int i=0;i<gen.length()-1;i++)
temp+='0';
string checksum;
checksum=xorem(temp,gen);
cout<<"Encoded data:";
cout<<data+checksum;
cout<<"\nCheck Sum:";
cout<<checksum;

cout<<"\n-------------------------------------Reciever Side---------------------
------------------\n";

cout<<"Enter data recieved:";


string msg;
cin>>msg;
if(msg.length()!=data.length()){
cout<<"Error in communication";
return 0;
}
string remainder;
remainder=xorem(msg,gen);
for(auto x:remainder)
if(x!='0'){
cout<<"Error in communication";
return 0;
}
cout<<"No Error!\n"; }
#include <bits/stdc++.h>
using namespace std;
unordered_map<string, string> mask;
string findclass(string ip) {
string first_octet;
int i = 0;
while (ip[i] != '.') {
first_octet += ip[i];
i++; }
int value = stoi(first_octet);
if (value <= 126) return "A";
if (value == 127) return "No class";
if (value <= 191) return "B";
if (value <= 223) return "C";
if (value <= 239) return "D";
return "E"; }
void find__id_host(string ip, string classs) {
vector<int> posofdot(3);
int dot = 0;
for (int i = 0; i < ip.length(); i++) {
if (ip[i] == '.') {
posofdot[dot] = i;
dot++; }
}
if (classs == "A") {
cout << "Network id:" << ip.substr(0, posofdot[0])+".0.0.0";
cout << "\n";
cout << "Host id:" << ip.substr(posofdot[0] + 1);
cout << "\nBroadcast adress:" << ip.substr(0,
posofdot[0])+".255.255.255";
return; }
if (classs == "B") {
cout << "Network id:" << ip.substr(0, posofdot[1])+".0.0";
cout << "\n";
cout << "Host id:" << ip.substr(posofdot[1] + 1);
cout << "\nBroadcast adress:" << ip.substr(0, posofdot[1])
+".255.255";
return; }
if (classs == "C") {
cout <<"Network id:" << ip.substr(0, posofdot[2])+".0";
cout <<"\n";
cout <<"Host id:" << ip.substr(posofdot[2] + 1);
cout <<"\nBroadcast adress:" << ip.substr(0, posofdot[2]) +".255";
return;
}
cout<<"In this Class, IP address is not divided into Network and Host
ID\n";
}
int main() {
mask["A"] = "255.0.0.0";
mask["B"] = "255.255.0.0";
mask["C"] = "255.255.255.0";
string ip;
cout << "Enter IPV4 adress: ";
cin >> ip;
string cla_ss = findclass(ip);
find__id_host(ip, cla_ss);
}
#include <bits/stdc++.h>
using namespace std;
unordered_map<string, string> mask;
string findclass(string ip) {
string first_octet;
int i = 0;
while (ip[i] != '.') {
first_octet += ip[i];
i++; }
int value = stoi(first_octet);
if (value <= 126) return "A";
if (value == 127) return "No class";
if (value <= 191) return "B";
if (value <= 223) return "C";
if (value <= 239) return "D";
return "E"; }
void find__id_host(string ip, string classs) {
vector<int> posofdot(3);
int dot = 0;
for (int i = 0; i < ip.length(); i++)
{
if (ip[i] == '.')
{
posofdot[dot] = i;
dot++;
}
}
if (classs == "A") {
cout << "Network id:" << ip.substr(0, posofdot[0])+".0.0.0";
cout << "\n";
cout << "Host id:" << ip.substr(posofdot[0] + 1);
cout << "\nBroadcast adress:" << ip.substr(0,
posofdot[0])+".255.255.255";
return;
}
if (classs == "B") {
cout << "Network id:" << ip.substr(0, posofdot[1])+".0.0";
cout << "\n";
cout << "Host id:" << ip.substr(posofdot[1] + 1);
cout << "\nBroadcast adress:" << ip.substr(0, posofdot[1])
+".255.255";
return;
}
if (classs == "C") {
cout <<"Network id:" << ip.substr(0, posofdot[2])+".0";
cout <<"\n";
cout <<"Host id:" << ip.substr(posofdot[2] + 1);
cout <<"\nBroadcast adress:" << ip.substr(0, posofdot[2]) +".255";
return;
}
cout<<"In this Class, IP address is not divided into Network and Host
ID\n";
}
int main(){
mask["A"] = "255.0.0.0";
mask["B"] = "255.255.0.0";
mask["C"] = "255.255.255.0";
string ip;
cout << "Enter IPV4 adress: ";
cin >> ip;
string cla_ss = findclass(ip);
cout << "Class: " << cla_ss;
cout << "\nDefault mask=" << mask[cla_ss] << "\n";

}
#include <bits/stdc++.h>
using namespace std;
struct node {
unsigned dist[20];
unsigned from[20];
} rt[10];
int main() {
int costmat[20][20];
int nodes, i, j, k, count = 0;
cout << "\nEnter the number of nodes : ";
cin >> nodes;
cout << "\nEnter the cost matrix : \n";
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
cin >> costmat[i][j];
costmat[i][i] = 0;
rt[i].dist[j] = costmat[i][j];
rt[i].from[j] = j; } }
do {
count = 0;
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;
count++; } }}}}
while (count != 0);
for (i = 0; i < nodes; i++) {
cout << "\n For router" << i + 1 << " ";
for (j = 0; j < nodes; j++)
cout << "\n Node " << j + 1 << " via " << rt[i].from[j] + 1 << "
Distance"<<rt[i].dist[j]; }
cout << ("\n\n");
return 0; }
#include <bits/stdc++.h>
using namespace std;

int main() {
int count, src_router, i, j, k, w, v, min;
int cost_matrix[100][100], dist[100], last[100];
int flag[100];
cout<<"Enter the number of routers: ";
cin>>count;
cout<<"Enter the cost matrix values: ";
for (i = 0; i < count; i++) {
for (j = 0; j < count; j++) {
cout<<"\n"<<i<<" -> "<<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])
if (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<<"\n"<<src_router<<" -> "<<i<<" : Path taken : "<<i;
w = i;
while (w != src_router) {
cout<<"\n<- "<<last[w];
w = last[w]; }
cout<<"\n Shortest path cost is: "<<dist[i];
}
}

You might also like