0% found this document useful (0 votes)
31 views

Computer Networks Record To Print

Uploaded by

A0554
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Computer Networks Record To Print

Uploaded by

A0554
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

1.

Implement the data link layer framing methods such as


character, character-stuffing and bit stuffing

BIT STUFFING:

Program
#include<stdio.h>
#include<string.h
> void rev(char
s[]){ char r[100];
int i;
for(i=0;i<strlen(s);i
++)
r[i] = s[i];
r[i] = '\0';
for(i=0;i<strlen(r);i+
+)
s[i] = r[strlen(r)-i-
1]; s[i] = '\0';
}
void main()
{ int
k,count,i;
char
inp[30],stuff[30],dest[30];
k =0;
count = 0;
printf("Enter the input data frame
:"); scanf("%s",inp);
for(i = strlen(inp)-
1;i>=0;i--){ stuff[k++]
= inp[i];
if(inp[i] ==
'1') count+
+;
else
count = 0;
if(count == 5){
count =0;
stuff[k++] = '0';
}
}
stuff[k] = '\
0'; rev(stuff);
printf("The stuffed data is %s
:",stuff); k=0;
count=0;
for(i = strlen(stuff)-1;i>=0;i--)
{ dest[k++] = stuff[i];
if(stuff[i] ==
'1') count+
+;
else
count = 0;
if(count == 5){
count
=0; i--;
}
}
dest[k] = '\
0'; rev(dest);
printf("\nThe destuffed data is %s \n",dest);
}

Output:
BYTE STUFFING:

Program
#include<stdio.h>
#include<string.h
> void rev(char
s[]){ char r[100];
int i;
for(i=0;i<strlen(s);i
++)
r[i] = s[i];
r[i] = '\0';
for(i=0;i<strlen(r);i+
+)
s[i] = r[strlen(r)-i-
1]; s[i] = '\0';
}
void main()
{ int k,i;
char
inp[30],stuff[30],dest[30];
k =0;
printf("Enter the input frame :");
scanf("%s",inp);
for(i = strlen(inp)-
1;i>=0;i--){ stuff[k++]
= inp[i];
if(inp[i] == 'E' || inp[i]
== 'F') stuff[k++] =
'E';
}
stuff[k] = '\
0'; rev(stuff);
printf("The stuffed data is %s
",stuff); k =0;
for(i = strlen(stuff)-
1;i>=0;i--){ dest[k++]
= stuff[i];
if(stuff[i] == 'E' || stuff[i]
== 'F') i--;
}
dest[k] = '\
0'; rev(dest);
printf("\nThe destuffed data is %s \n",dest);
}

Output
2. Write a program to compute CRC code for the polynomials
CRC-12, CRC-16 and CRC CCIP

Program
#include<stdio.h>
#include<string.h
>
char
data[50],div[50],total[50];
int datalen,divlen,len;
void check()
{ int j,i;
for(j=0;j<divlen;j
++) data[j] =
total[j];
while(j<=strlen(tot
al)){ if(data[0]
== '1'){
for(i=0;i<divlen;i++)
data[i] = (data[i] == div[i])?'0' :'1';
}
for(i=0;i<divlen-1;i+
+) data[i] =
data[i+1]; data[i] =
total[j++];
}
}
void main()
{ int flag,i;
printf("Enter the frame to be transmitted :");
scanf("%s",data);
printf("Enter the generator bits :");
scanf("%s",div);
datalen = strlen(data)

; divlen = strlen(div);
len = datalen + divlen -1;
for(i=0;i<datalen;i++)
total[i] = data[i];
for(i=datalen;i<len;
i++)
total[i] = '0';
total[i] = '\0';
check();
for(i=0;i<divlen-
1;i++)
total[i+datalen] = data[i];
printf("The tranmitted frame is %s ",total);
printf("\nEnter the received frame :");
scanf("%s",total);
printf("%s",total
); check();
flag = 1;
for(i=0;i<divlen-
1;i++){ if(data[i]
== '1')
flag = 0;
}
if(flag)
printf("no
errors"); else
printf("error detected\n");
}

Output

CRC-

12

CRC-16
3. Develop a simple data link layer that performs the flow control
using the sliding window protocol, and loss recovery using the
Go-Back-N mechanism

Program
#include<stdio.h>
void main(){
int n,i,k,ws,ack,z;
printf("Enter the number of frames to be tranmitted :");
scanf("%d",&n);
printf("Enter the window size :");
scanf("%d",&ws);
i = 1;
k = 0;
while(i<=n
){ z = 0;
for(k = i;k<i+ws&&k<=n;k++){
printf("\nThe frame %d is transmitted ",k);
}
for(k=i;k<i+ws&&k<=n;k++){

printf("\nEnter ack for %d (1Y,0N) :",k);


scanf("%d",&ack);
if(ack==1)
z++;
else{
printf("\n Error in transmitting %d Retranmitting entire
:",k) window
;
break;
}
}
i = i+z;
}
printf("\n");
}
Output
4. Implement Dijsktra’s algorithm to compute the shortest path
through a network

Program
#include<stdio.h>
#include<stdbool.
h> #define MAX
100 void main(){
int n,cost[100]
[100],prev[100],dist[100],src,dest,m,actpath[100],path[100];
bool vis[100];
int i,j,min,curr;
printf("Enter the number of nodes
:"); scanf("%d",&n);
printf("Enter the cost
matrix :"); for(i=0;i<n;i+
+){ for(j=0;j<n;j++){
scanf("%d",&cost[i][j]);
if(cost[i][j] == 0)
cost[i][j] = MAX;
}
}
printf("Enter the source
:"); scanf("%d",&src);
printf("Enter the destination
:"); scanf("%d",&dest);
for(i=0;i<n;i++){
vis[i] = false;
prev[i] = -1;
dist[i] = MAX;
}
dist[n] = MAX;
dist[src] = 0;
for(i=0;i<n;i+
+){ min = n;
for(j=0;j<n;j+
+){
if(vis[j] == false &&
dist[j]<=dist[min]) min = j;
}
vis[min] = true;
for(m=0;m<n;m
++){
if(vis[m] == false && dist[m] >dist[min] + cost[min]
[m]){ dist[m] = dist[min] + cost[min][m];
prev[m] = min;
}
}
if(min == dest) break;
}
curr =
dest; i =
0;
while(curr!=-1){
path[i++] =
curr; curr =
prev[curr];
}
for(j=0;j<i;j++){
actpath[j] = path[i-
j-1];
}
printf("The shortest distance is %d :\
n",dist[dest]); printf("\nThe path is :");
for(i = 0;i<j;i++){
printf("%d ",actpath[i
]);
}
}
Output
5. Take an example subnet of hosts and obtain a broadcast
tree for the subnet

Program
#include<stdio.h>
int p,q,u,v,n;int
min=99,mincost=0; int t[50]
[2],i,j;
int parent[50],edge[50]
[50]; main()
{
clrscr();
printf("\n Enter the number of
nodes"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%c\
t",65+i);
parent[i]=-1;
}
printf("\n");
for(i=0;i<n;i++)
{
printf("%c",65+i);
for(j=0;j<n;j++)
scanf("%d",&edge[i]
[j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if(edge[i][j]!=99)
if(min>edge[i][j])
{
min=edge[i]
[j]; u=i;v=j;
}
p=find(u);
q=find(v);
if(p!=q)
{ t[i]
[0]=u;
t[i][1]=v;
mincost=mincost+edge[u]
[v]; sunion(p,q);
}
else{t[i][0]=-1;
t[i][1]=-1;
}
min=99;
}
printf("Minimum cost is %d\n Minimum spanning tree is\n"
,mincost); for(i=0;i<n;i++)
if(t[i][0]!=-1 && t[i][1]!=-1)
{
printf("%c %c %d", 65+t[i][0], 65+t[i][1],edge[t[i][0]][t[i]
[1]]); printf("\n");
}
getch();
}
sunion(int l,int m)
{
parent[l]=m;
}find(int l)
{
if(parent[l]>
0)
l=parent[l];
return l;
}
Output
6. Implement distance vector routing algorithm for obtaining
routing tables at each node

Program
#include<stdio.h>
#define MAX 100
struct node{
int
dist[10];
int
nhop[10];
}nodes[10];

void main(){
int n,cost[10]
[10],i,j,k,count;
printf("Enter the n :");
scanf("%d",&n);
printf("Enter the cost matrix (Lower
Traingle):"); for(i=0;i<n;i++){
cost[i][i] = 0;
for(j=0;j<i;j++)
{ scanf("%d",&cost[i]
[j]);
if(cost[i][j] == 0) cost[i][j] = MAX;
cost[j][i] = cost[i][j];
}
}
for(i=0;i<n;i++)
{ for(j=0;j<n;j+
+){
nodes[i].dist[j] = cost[i]
[j]; nodes[i].nhop[j] = j;
}
}
while(1){
count =
0;
for(i=0;i<n;i++)
for(j=0;j<n;j+
+)
for(k=0;k<n;k
++)
if(nodes[i].dist[j] > nodes[k].dist[j] + cost[i][k]){
nodes[i].dist[j] = nodes[k].dist[j] +
cost[i][k]; nodes[i].nhop[j] = k;
count++;
}

if(count == 0)break;
}
for(i=0;i<n;i++){
printf("\n The routing table for %d
",i); printf("\nNODE
DISTANCE NHOP
");
for(j=0;j<n;j++)
printf("\n%d %d %d
",j,nodes[i].dist[j],nodes[i].nhop[j]);

}
printf("\n");
}
Output
7. Implement data encryption and data decryption

Program
#include<stdio.h>
#include<string.h
>
void encrypt(char data[20],int
key); void decrypt(char
data[20],int key); int main()
{
char
data[20]; int
key;
printf("enter data to be
encrypted\n"); scanf("%s",data);
printf("enter key\
n");
scanf("%d",&key);
encrypt(data,key);
decrypt(data,key);
return 0;
}
void encrypt(char data[20],int key)
{
int
i=0;
char
ch;
for(i=0;i<strlen(data);i++)
{
ch=data[i];
if(ch>='a' && ch<='z')
{
ch=ch+key;
if(ch>'z')
{
ch=ch-'z'+'a'-1;
}
data[i]=ch;
}
else if(ch>='A' && ch<='Z')
{
ch=ch+key;
if(ch>'Z')
{
ch=ch-'Z'+'A'-1;
}
data[i]=ch;
}
}
printf("Encrypted message=%s\n",data);
}
void decrypt(char data[20],int key)
{
int
i=0;
char
ch;
for(i=0;i<strlen(data);i++)
{
ch=data[i];
if(ch>='a' && ch<='z')
{
ch=ch-key;
if(ch<'a')
{
ch=ch+'z'-'a'+1;
}
data[i]=ch;
}
else if(ch>='A' && ch<='Z')
{
ch=ch-key;
if(ch<'A')
{
ch=ch+'Z'-'A'+1;
}
data[i]=ch;
}
}
printf("Decrypted message=%s\n",data);
}

Output
8. Write a program for congestion control using Leaky bucket
algorithm

Program
#include<stdio.h>
void main(){
int
N,B,R,fill;
int flag =1;
printf("Enter the number of packets leaking from the bucket :");
scanf("%d",&R);
printf("Enter the bucket size :");
scanf("%d",&B);
fill = 0;
while(flag)
{
printf("\nEnter the number of packets being added by the
sender "); scanf("%d",&N);
if(N-R<0){
if(fill+N<=R){
printf("\nThe number of packets tranmsitted are %d
",fill+N); fill=0;
printf("\nNo pkts lost and the bucket is empty ");
}
else{
printf("No. of packets tranmitted are %d
",R); fill = fill +N-R;
printf("\nNo. of packets left are %d in the bucket and none
lost
",fill);
}
}
else
{ fill += N-R;
printf("No. of packets transmitted are %d ",R);
if(fill>B){
printf("Packet overflow and %d lost ",fill-B);
printf("Bucket full ");
fill = B;
}
else{ if(fill
==B)
printf("Bucket full");
printf("The no. of packets left are %d and none
lost",fill); if(fill==0)
printf("\n Bucket Empty ");
}
}
printf("\nDo u want to continue (1Y,0N) :");
scanf("%d",&flag);
}
}

Output
9. Write a program for frame sorting technique used in buffers

Program
#include<stdio.h>
#include<string.h
> #define
frameSize 3
#define maxFrames 127

char str[frameSize *
maxFrames]; struct frame{
char
text[frameSize]; int
seq_no;
}fr[maxFrames], shuf_ary[maxFrames];

int assign_seq_no();
void generate(int *random_ary, const int
limit); void shuffle(const int no_frames);
void sort(const int no_frames);

int main(){
int no_frames,i;
printf("Enter the
message: "); gets(str);
no_frames =
assign_seq_no();
shuffle(no_frames);
sort(no_frames); printf("\n\
nAFTER SORTING\n");
for(i=0;i<no_frames;i++){
printf("%s",shuf_ary[i].text);
}
printf("\n\n");
}

int
assign_seq_no()
{ int k=0,i,j;
for(i=0; i < strlen(str);
k++){ fr[k].seq_no =
k;
for(j=0; j < frameSize && str[i]!='\0'; j++)
{ fr[k].text[j] = str[i++];
}
}
printf("\nAfter assigning sequence numbers:\
n"); for(i=0; i < k; i++){
printf("%d:%s ",i,fr[i].text);
}
return k;
}

void generate(int *random_ary, const int


limit){ int r, i=0, j;
while(i < limit){
r = random() % limit;
for(j=0; j < i; j++){
if( random_ary[j] ==
r ) break;
}
if(i == j)
random_ary[i++] = r;
}
}

void shuffle(const int no_frames){


int i, k=0,
random_ary[no_frames];
generate(random_ary,
no_frames); for(i=0; i <
no_frames; i++){
shuf_ary[i] = fr[random_ary[i]];
}
printf("\n\nAFTER SHUFFLING:\n");
for(i=0; i < no_frames; i++){
printf("%d:%s ",shuf_ary[i].seq_no,shuf_ary[i].text);
}
}

void sort(const int


no_frames){ struct frame
temp;
for(int i=0; i < no_frames-1 ; i++)
{ for(int j=0; j < no_frames-1-i;
j++){
if(shuf_ary[j].seq_no >
shuf_ary[j+1].seq_no){ temp =
shuf_ary[j];
shuf_ary[j] = shuf_ary[j+1];
shuf_ary[j+1] = temp;

}
}
}
}

Output
10.Wireshark
i. Packet Capture Using Wire shark
ii. Starting Wire shark
iii.Viewing Captured Traffic
iv.Analysis and Statistics & Filters.

Wireshark:
Wireshark is a free open-source network protocol analyzer. It is
used for network troubleshooting and communication protocol
analysis. Wireshark captures network packets in real time and
displays them in
human-readable format. It provides many advanced features
including live capture and offline analysis, three-pane packet
browser, coloring rules for analysis.

Getting Wireshark:
The Kai Linux has Wireshark installed. Wireshark can also be
downloaded from here: https://www.wireshark.org/download.html

Starting Wireshark:
When you run the Wireshark program, the Wireshark graphic user
interface will be shown.
Then, you need to choose an interface. If you are running the
Wireshark on your laptop,you need to select the WiFi interface. If
you are at a desktop, you need to select the Ethernet interface
being used. Note that there could be multiple interfaces. After
you select the interface, you can click start to capture the packets
The Wireshark interface has five major components:

The command menus are standard pulldown menus located at the


top of the window. Of interest to us now is the File and Capture
menus. The File menu allows you to save captured packet data or
open a file containing previously captured packet data, and exit
the Wireshark application. The Capture menu allows you to begin
packet capture.

The packet-listing window displays a one-line summary for each


packet captured, including the packet number (assigned by
Wireshark; this is not a packet number contained in any protocol’s
header), the time at which the packet was captured, the packet’s
source and destination addresses, the protocol type, and protocol-
specific information contained in the packet.
The packet-header details window provides details about the
packet selected (highlighted) in the packet-listing window. (To
select a packet in the packet-listing window, place the cursor over
the packet’s one-line summary in the packet-listing window and
click with the left mouse button.).

The packet-contents window displays the entire contents of the


captured frame, in both ASCII and hexadecimal format.

Towards the top of the Wireshark graphical user interface, is the


packet display filter field, into which a protocol name or other
information can be entered in order to filter the information
displayed in the packet-listing window (and hence the packet-
header and packet-contents windows).

Capturing packets, filtering,

inspecting Do the following steps:


1. Start up the Wireshark program (select an interface and press
start to
capture packets).
2. Start up your favorite browser (ceweasel in Kali Linux).
3. In your browser, go to Wayne State homepage
by typing www.wayne.edu.
4. After your browser has displayed the http://www.wayne.edu
page, stop Wireshark packet capture by selecting stop in the
Wireshark capture window. This will cause the Wireshark capture
window to disappear and the main Wireshark window to display
all packets captured since you began packet capture.
Color Coding: You’ll probably see packets highlighted in green,
blue, and black. Wireshark uses colors to help you identify the
types of traffic at a glance. By default, green is TCP traffic, dark
blue is DNS traffic, light blue is UDP traffic, and black identifies TCP
packets with problems

To filter the connections to the ones we want to focus on, we


have to use the filtering functionality of Wireshark by typing
“http” in the filtering field.

To further filter packets in Wireshark, we need to use a more


precise filter. By setting the http.host==www.wayne.edu, we are
restricting the view to packets that have as an http host the
www.wayne.edu website.
Observing UDP Stream

Select one of the packets and press the right mouse button
Click on Follow UDP Stream
11.Write a program to implement selective repeat

Program
#include<stdio.h>
int getAck(int w[],int
nr[],int m){ int ack;
int k = 0;
int i;
for(i=0;i<m;i++){
printf("\nDid you receive frame %d(1Y,0N)
",w[i]); scanf("%d",&ack);
if(ack == 0)
nr[k++] =
w[i];
}
return k;
}
void main(){
int n,ws,k,j,z,i;
int
w[10],nr[10];
i =0;
k = 0;
printf("Enter n :");
scanf("%d",&n);
printf("Enter the window size :");
scanf("%d",&ws);
while(1)
{ if(k==0&&i=
=n) break;
for(z=0;z<k;z+
+){
w[z] = nr[z];
printf("\nThe frame %d has been transmitted again ",w[z]);
}
for(j=k;j<ws&&i<n;j
++){ w[j] = i+1;
printf("\n The frame %d has been tranmitted ",w[j]);
i++;
}
k = getAck(w,nr,j);
}
}

Output
12.Write a program to implement one bit sliding window protocol

Program
#include<stdio.h>
#include<stdlib.h
>

int count;
void sender(int i){
printf("\n The frame %d has been sent ",i);
}
void receiver(int i)
{ char ack;
printf("\nDid you receive %d
(Y,N)",i); scanf("%s",&ack);
if(ack==
'Y')
return;
else{
if(count<5)
{ count++;
sender(i);
receiver(i);
}
else{
printf("Limit exceeded
:"); exit(0);
}

}
}
void main()
{ int i,n;
printf("Enter the number of frames :"
); scanf("%d",&n);
for(i=0;i<n;i++){
count = 0;
sender(i+1);
receiver(i+1);
}
}

Output
13.Write a program to implement IP fragmentation

Program
#include<stdio.h>
#include<math.h
> void main(){
int pktsize,hedsize,MTU,count,p,q,x,i,offset;
printf("Enter the packet size to be transmitted
:"); scanf("%d",&pktsize);
printf("Enter the header size
:"); scanf("%d",&hedsize);
printf("Enter the path MTU ");
scanf("%d",&MTU);
p = pktsize - hedsize;
q = MTU - hedsize;
count =
ceil((float)p/q); x =
p;
offset = 0;
for(i=0;i<count;i++){
if(i!=count-1){
printf("Fragment no %d \n",i+1);
printf("FragmentBodyOffset MoreFragments \
n"); printf("%d %d %d \n",q,offset,1);
offset =
q+offset; x =
x-q;

}
else
{ printf("Fragment no %d \n",i+1);
printf("FragmentBodyOffset MoreFragments \
n"); printf("%d %d %d \n",x,offset,0);
x = 0;

}
}
}

Output

You might also like