Lab_Manual10
Lab_Manual10
DESCRIPTION:
● To prevent data being interpreted as control information. For example, many frame-
based protocols, such as X.25, signal the beginning and end of a frame with six
consecutive 1 bits. Therefore, if the actual data being transmitted has six 1 bits in a row,
a zero is inserted after the first 5 so that the dat is not interpreted as a frame delimiter.
Of course, on the receiving end, the stuffed bits must be discarded.
● For protocols that require a fixed-size frame, bits are sometimes inserted to make the
frame size equal to this set size.
● For protocols that required a continuous stream of data, zero bits are sometimes
inserted to ensure that the stream is not broken.
SOURCE CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
String str=br.readLine();
int count=0;
for(int i=0;i<str.length();i++)
System.out.println(str.charAt(i));
if(str.charAt(i)!='1'&&str.charAt(i)!='0')
return;
if(str.charAt(i)=='1')
count++;
res=res+str.charAt(i);
else
res=res+str.charAt(i);
count=0;
if(count==5)
res=res+'0';
count=0;
}
res="01111110"+res+"01111110";
Output 1:-
011111101
Output 2:-
10110
DESCRIPTION:One the bits are stuffed and sent by the sender the receiver receives the msg.The
receiver has to decode it. The algorithm used is destuffin algorithm. It removes a bit when
consequtive 5 ones appear.
1. Input the stuffed sequence.2. Remove start of frame from sequence.3. For every bit in
input,a. Append bit to output sequence. b. Is bit a 1?Yes: Increment count. If count is 5, remove
next bit (which is0) and reset count. No: Set count to 0.4. Remove end of frame bits from
sequence.
SOURCE CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
String str=br.readLine();
int count=0;
for(int i=8;i<str.length()-8;i++)
{
System.out.println(str.charAt(i));
if(str.charAt(i)!='1'&&str.charAt(i)!='0')
return;
if(str.charAt(i)=='1')
count++;
res=res+str.charAt(i);
else
res=res+str.charAt(i);
count=0;
if(count==5)
i++;
count=0;
}
System.out.println("the string is:"+res);
Output 1:-
01111110011111010101111110
Output 2:-
011111101011001111110
1
1
DESCRIPTION: In byte stuffing (or character stuffing), a special byte is added to the data section
of the frame when there is a character with the same pattern as the flag. The data section is
stuffed with an extra byte. This byte is usually called the escape character (ESC), which has a
predefined bit pattern. Whenever the receiver encounters the ESC character, it removes it from
the data section and treats the next character as data, not a delimiting flag.
SOURCE CODE:
import java.io.*;
import java.util.*;
import java.lang.*;
System.out.println("enter message");
String str=br.readLine();
char sf=br.readLine().charAt(0);
char ef=br.readLine().charAt(0);
for(int i=0;i<str.length();i++)
System.out.println(“\n”);
System.out.println(str.charAt(i));
if(str.charAt(i)==sf)
res=res+str.charAt(i)+sf;
else
if(str.charAt(i)==ef)
res=res+str.charAt(i)+ef;
else
res=res+str.charAt(i);
res=sf+res+ef;
Output 1:-
enter message
cnoslab
cnoslab
cnoslaabb
Output2:-
enter message
aceec
aceec
aacceecc
aaacceeccc
PROGRAM 4
DESCRIPTION:
SOURCE CODE:
import java.io.*;
import java.util.*;
import java.lang.*;
System.out.println("enter message");
String str=br.readLine();
char sf=str.charAt(0);
char ef=str.charAt(str.length()-1);
for(int i=1;i<str.length()-1;i++)
if(str.charAt(i)==sf)
++i;
res=res+str.charAt(i);
else if(str.charAt(i)==ef)
++i;
res=res+str.charAt(i);
else
res=res+str.charAt(i);
Output1:-
enter message
acnoslaabbb
cnoslab
Output2:-
enter message
aaacceeccc
aceec
PROGRAM 5
DESCRIPTION:
1. Let r be the degree of G(x). Append r zero bits to the low-order end of theframe, so it now
contains m+r bits and corresponds to the polynomial x^r M(x).
2. Divide the bit string corresponding to G(X) into the bit string corresponding to x^r M(x) using
modulo-2 division.
3. Subtract the remainder (which is always r or fewer bits) from the bit stringcorresponding to
x^r M(x) using modulo-2 subtraction. The result is the checksummed frame to be transmitted.
Call its polynomial T(x).The CRC-CCITT polynomial is x^16+ x^12+ x^5+ 1
SOURCE CODE:
import java.lang.*;
import java.util.*;
import java.io.*;
class CRC
obj.send();
obj.receive();
class CRCFN
int n,h,l,dl;
int i,j,k,z;
String str1=new String("");
System.out.println("enter 12 for crc -12 \n 160 for crc 16\n 161 for crc-16-cit\n");
n=Integer.parseInt(br.readLine());
if(!((n==12||(n==160)||(n==161))))
System.out.println("invalid");
System.exit(0);
System.out.println("-----------------");
str1=br.readLine();
om=str1.toCharArray();
for(int i=0;i<om.length;i++)
System.out.print(om[i]);
h=om.length;
str2+=str1;
if(n==12)
poly+=("1100000001111");
tempdiv+=("0000000000000");
str2+=("000000000000");
else if(n==160)
poly+=("11000000000000101");
tempdiv+=("00000000000000000");
str2+=("000000000000");
else if(n==161)
poly+=("1000100000010001");
tempdiv+=("00000000000000000");
str2+=("0000000000000000");
System.out.println(poly+" \n" +tempdiv+" \n"+str2);
else
System.out.println("invalid");
System.exit(0);
dv=str2.toCharArray();
p=poly.toCharArray();
t=tempdiv.toCharArray();
l=p.length;
dl=dv.length;
System.out.println("\naugmented didvdend\n");
for(int i=0;i<dv.length;i++)
System.out.print(dv[i]);
System.out.println("\n");
System.out.println("\ngenerator\n");
for(int i=0;i<p.length;i++)
System.out.print(p[i]);
System.out.println("\n");
System.out.println("\ntemp data\n");
for(int i=0;i<t.length;i++)
System.out.print(t[i]);
for(i=0;i<h;i++)
if(dv[i]=='0')
for(j=i,k=0;j<(l+i)&&k<l;j++,k++)
if(dv[j]==t[k])
dv[j]='0';
else
dv[j]='1';
else if(dv[i]=='1')
for(j=i,k=0;j<(l+i)&&k<l;j++,k++)
if(dv[j]==p[k])
dv[j]='0';
else
dv[j]='1';
else if((dv[i]!='0')||(dv[i]!='1'))
System.out.println("invalid frame");
System.exit(0);
for(z=i;z<(l+i);z++)
System.out.print(dv[z]);
System.out.println("\n");
for(i=h,j=0;i<=(dl-1);i++,j++)
r[j]=dv[i];
str1+=rstr;
//char om[50],dv[50],r[20];
char om[]=new char[50];
int i,j,k,z,c=0;
System.out.println("...................\n");
omstr=br.readLine();
om=omstr.toCharArray();
for(i=0;i<om.length;i++)
dv[i]=om[i];
//strcpy(dv,om);
for(i=0;i<=h-1;i++)
if(dv[i]=='0')
for(j=i,k=0;j<(l+i)&&k<l;j++,k++)
if(dv[j]==t[k])
dv[j]='0';
else
dv[j]='1';
else if(dv[i]=='1')
for(j=i,k=0;j<(l+i)&&k<l;j++,k++)
if(dv[j]==p[k])
dv[j]='0';
else
dv[j]='1';
else
System.out.println("invalid frame\n");
System.exit(0);
for(z=i;z<13+i;z++)
System.out.print(dv[z]);
System.out.println("\n");
for(i=h,j=0;i<=(dl-1);i++,j++)
r[j]=dv[i];
if(r[j]=='0')
c++;
//r[j]='\0';
for(int q=0;q<r.length;q++)
System.out.print(r[q]);
System.out.println("\n\n\n"+c);
if(c==l-1)
//om[h]='\0';
for(int q=0;q<om.length;q++)
System.out.print(om[q]);
for(int x=0;x<h;x++)
System.out.print(om[x]);
else
Output1:-
enter crc to be used
temp data
00000000000000
the remainder is
100010011001
at the receiver
.........................
Output2:-
enter crc to be used
enter 12 for crc-12
160 for crc-16
161 for crc-16-ccitt
32
Invalid
PROGRAM 6
AIM: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.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
char sender[50],receiver[50];
int i,winsize;
printf("\n ENTER THE WINDOWS SIZE : ");
scanf("%d",&winsize);
printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW
\n");
printf("\n ENTER THE DATA TO BE SENT: ");
fflush(stdin);
gets(sender);
for(i=0;i<winsize;i++)
receiver[i]=sender[i];
receiver[i]=NULL;
printf("\n MESSAGE SEND BY THE SENDER:\n");
puts(sender);
printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
for(i=0;i<winsize;i++);
printf("\n ACK:%d",i);
printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
puts(receiver);
printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
getch();
}
PROGRAM 7
DESCRIPTION: Let the node at which we are starting be called the initial node. Let the distance
of node Y be the distance from the initial node to Y. Dijkstra's algorithm will assign some initial
distance values and will try to improve them step by step.
1. Assign to every node a tentative distance value: set it to zero for our initial node and to
infinity for all other nodes.
2. Mark all nodes unvisited. Set the initial node as current. Create a set of the unvisited
nodes called the unvisited set consisting of all the nodes except the initial node.
3. For the current node, consider all of its unvisited neighbors and calculate their tentative
distances. For example, if the current node A is marked with a tentative distance of 6,
and the edge connecting it with a neighbor B has length 2, then the distance to B
(through A) will be 6+2=8. If this distance is less than the previously recorded tentative
distance of B, then overwrite that distance. Even though a neighbor has been examined,
it is not marked as "visited" at this time, and it remains in the unvisited set.
4. When we are done considering all of the neighbors of the current node, mark the
current node as visited and remove it from the unvisited set. A visited node will never be
checked again; its distance recorded now is final and minimal.
5. If the destination node has been marked visited (when planning a route between two
specific nodes) or if the smallest tentative distance among the nodes in the unvisited set
is infinity (when planning a complete traversal), then stop. The algorithm has finished.
6. Set the unvisited node marked with the smallest tentative distance as the next "current
node" and go back to step
SOURCE CODE:
Import java.io.*;
Import java.lang.*;
Import java.util.*;
class STATES
Int l;
Int p;
Int label;
//state [10];
class DIJ
int INF=1000;
int i,n,k,t,s,j,dest,temp1,min;
int x,y;
for(i=0;i<10;i++)
state[i] = new STATES();
n=Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
j=Integer.parseInt(br.readLine());
for(k=0;k<j;k++)
temp1=Integer.parseInt(br.readLine());
dest=Integer.parseInt(br.readLine());
dist[i][temp1]=dest;
System.out.println("\n\n\tmatrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
System.out.print("\t "+dist[i][j]);
else if(i!=j && dist[i][j]==0)
System.out.print("\t "+99);
else
System.out.print("\t "+dist[i][j]);
System.out.println("\n");
t=Integer.parseInt(br.readLine());
s=Integer.parseInt(br.readLine());
for(i=1;i<=n;i++)
state[i].p=-1;
state[i].l=INF;
state[i].label=0;
state[t].l=0;
state[t].label=1;
k=t;
do
{
for(i=1;i<=n;i++)
if(state[k].l+dist[k][i]<state[i].l)
state[i].p=k;
state[i].l=state[k].l+dist[k][i];
k=0;
min=INF;
for(i=1;i<=n;i++)
min=state[i].l;
k=i;
}
state[k].label=1;
}while(k!=s);
i=0;
k=s;
x=0;
do
path[i]=k+1;
route[x]=k;
x++;
i++;
k=state[k].p;
}while(k>0);
for(y=x-1;y>=0;y--)
System.out.print(route[y]+"->");
Output1:-
enter no. of nodes
5
enter the no. of neighbours for node ::1
2
2
matrix
0 2 1 99 3
2 0 2 99 99
1 2 0 2 99
99 99 2 0 2
3 99 99 2 0
1->3->4->
Minimum cost is 3
Output2:-
enter no.of nodes
2
enter Neighbour number ,Distance
matrix
0 99 2 2
2 0 1 1
99 99 0 1
2 1 1 0
2
Shortest Path is:
1->4->2->
Minimum cost is 3