0% found this document useful (0 votes)
5 views41 pages

Lab_Manual10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views41 pages

Lab_Manual10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

PROGRAM 1

AIM: WRITE A JAVA PROGRAM TO IMPLEMENT BIT STUFFING

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.*;

public class Bitstuffing

public static void main(String args[])throws IOException

System.out.println("enter the binary message");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

String res=new String();

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')

System.out.println("enter the binary values");

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;
}

System.out.println("the original message:"+str);

System.out.println("the Bitstuffing message:"+res);

res="01111110"+res+"01111110";

System.out.println("the message stuffing:"+res);

Output 1:-

enter the binary message

011111101

the original message:011111101

the Bitstuffing message:0111110101


the message stuffing:01111110011111010101111110

Output 2:-

enter the binary message

10110

the original message:10110

the Bitstuffing message:10110

the message stuffing:011111101011001111110


PROGRAM 2

AIM: WRITE A JAVA PROGRAM TO IMPLEMENT BIT DESTUFFING PROGRAM

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.*;

public class Bitunstuffing

public static void main(String args[])throws IOException

System.out.println("enter the binary message");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

String res=new String();

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')

System.out.println("enter the binary values");

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:-

enter the binary message

01111110011111010101111110

the string is:011111101

Output 2:-

enter the binary message

011111101011001111110

1
1

the string is:10110


PROGRAM 3

AIM: WRITE A JAVA PROGRAM TO IMPLEMENT CHARACTER STUFFING

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.*;

public class Bytestuffing

public static void main(String args[])throws IOException

System.out.println("enter message");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

String res=new String();

System.out.println("enter starting flag");

char sf=br.readLine().charAt(0);

System.out.println("enter ending flag");

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);

System.out.println("the original messsage is:\n" + str);

System.out.println("the stuffed message is:\n"+res);

res=sf+res+ef;

System.out.println("the message after stuffing :\n"+res);


}

Output 1:-
enter message

cnoslab

enter starting flag

enter ending flag

the original messsage is:

cnoslab

the stuffed message is:

cnoslaabb

the message after stuffing:


acnoslaabbb

Output2:-
enter message

aceec

enter starting flag

enter ending flag

the original messsage is:

aceec

the stuffed message is:

aacceecc

the message after stuffing:

aaacceeccc
PROGRAM 4

AIM: A JAVA PROGRAM TO IMPLEMENT CHARACTER DESTUFFING

DESCRIPTION:

1. Input stuffed sequence.


2. Remove start of frame from sequence (DLE STX).
3. For every character in input,
a. Append character to output sequence.
b. Is character DLE?Yes: Remove next DLE from input sequence. No: Continue.
4. Remove stop of frame character to output sequence (DLE ETX).

SOURCE CODE:

import java.io.*;

import java.util.*;

import java.lang.*;

public class Byteunstuffing


{

public static void main(String args[])throws IOException

System.out.println("enter message");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

String res=new String();

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);

System.out.println("the string is:\n"+res);

Output1:-
enter message

acnoslaabbb

the string is:

cnoslab

Output2:-
enter message

aaacceeccc

the string is:

aceec

PROGRAM 5

AIM:WRITE A JAVA PROGRAM TO IMPLEMENT CRC CODE

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

public static void main(String args[]) throws IOException

CRCFN obj=new CRCFN();

obj.send();

obj.receive();

class CRCFN

int n,h,l,dl;

char p[]=new char[50];

char t[]=new char[50];

char om[]=new char[50];

char dv[]=new char[50];

char r[]=new char[50];

int i,j,k,z;
String str1=new String("");

String str2=new String("");

String poly=new String("");

String tempdiv=new String("");

void send() throws IOException

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter crc to be used");

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("\nat sender side");

System.out.println("-----------------");

System.out.println("\n enter the original frame");

str1=br.readLine();

om=str1.toCharArray();

for(int i=0;i<om.length;i++)

System.out.print(om[i]);
h=om.length;

str2+=str1;

System.out.println("\n lenght and original message\n"+h+"\t\n"+str2);

if(n==12)

poly+=("1100000001111");

tempdiv+=("0000000000000");

str2+=("000000000000");

System.out.println(poly+" \n"+tempdiv+" \n" + str2);

else if(n==160)

poly+=("11000000000000101");

tempdiv+=("00000000000000000");

str2+=("000000000000");

System.out.println(poly+" \n"+tempdiv+" \n" +str2);

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("\nlenght of polynamial and dividend\n"+l+"\t\n "+dl);

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]);

System.out.println("\nthe divisor for each step\nof modulo-2");

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];

String rstr=new String(r);

System.out.println("\nthe reminder is\n"+rstr);

str1+=rstr;

System.out.println("\n the frame sent is:\n"+str1);

void receive()throws IOException

//char om[50],dv[50],r[20];
char om[]=new char[50];

char dv[]=new char[50];

char r[]=new char[50];

int i,j,k,z,c=0;

String omstr=new String("");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("\n at the receiver\n");

System.out.println("...................\n");

System.out.println("\n enter the frame received:\n");

omstr=br.readLine();

om=omstr.toCharArray();

for(i=0;i<om.length;i++)

dv[i]=om[i];

//strcpy(dv,om);

System.out.println("\n the divisor for each step \n of modulo-2 division is:\n");

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';

System.out.println("\n the remainder is\n");

for(int q=0;q<r.length;q++)

System.out.print(r[q]);

System.out.println("\n\n\n"+c);

if(c==l-1)

System.out.println("\n the frame received is error free:\n");

//om[h]='\0';

System.out.println("\n the original frame is \n");

for(int q=0;q<om.length;q++)

System.out.print(om[q]);

System.out.println("\n the original message is \n");

for(int x=0;x<h;x++)
System.out.print(om[x]);

else

System.out.println("\n received frame is corrupted:FRAME HAVE ERRORS\n");

Output1:-
enter crc to be used

enter 12 for crc-12


160 for crc-16
161 for crc-16-ccitt
12

at the sender side


.........................

enter the original frame


10110
10110
Length and original message
5
10110
1100000001111
0000000000000
10110000000000000
length of polynomial and dividend
13
17
augmented didvdend
10110000000000000
Generator
1100000001111

temp data
00000000000000

the divisor for each step


of modulo-2
0111000001111
0010000010001
0100000100010
0100001001011
010001011001

the remainder is
100010011001

the frame sent is:


10110100010011001

at the receiver
.........................

enter the frame received:


10110100010011001

the divisor for each step


of modulo-2 division is:
0111010000110
0010100000010
0101000000100
0110000000111
0000000000000

the remainder is 0000000000000


12
the frame received is error free:
the original frame is
10110100010011001

the original message is


10110

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

AIM:WRITE A JAVA PROGRAM TO IMPLEMENT DIJKSTRA’S ALGORITHM TO COMPUTE THE


SHORTEST PATH THROUGH A GRAPH.

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

public static void main(String args[]) throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int INF=1000;

int i,n,k,t,s,j,dest,temp1,min;

int path[]=new int[10];

int x,y;

int route[]=new int[10];

int dist[][]=new int[10][10];

STATES state[]=new STATES[10];

for(i=0;i<10;i++)
state[i] = new STATES();

System.out.println("Enter no.of nodes");

n=Integer.parseInt(br.readLine());

for(i=1;i<=n;i++)

System.out.println("enter the no.of neighbours for node ::"+i);

j=Integer.parseInt(br.readLine());

System.out.println("enter Neighbour number ,Distance");

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++)

if(i==j && dist[i][j]==0)

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");

System.out.println("To find the path \n\n enter starting node");

t=Integer.parseInt(br.readLine());

System.out.println("Enter ending node");

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(dist[k][i]!=0 && state[i].label==0)

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++)

if(state[i].label==0 && state[i].l<min)

min=state[i].l;

k=i;

}
state[k].label=1;

}while(k!=s);

i=0;

k=s;

System.out.println("\n Shortest Path is:");

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]+"->");

System.out.println("\n Minimum cost is "+min);

Output1:-
enter no. of nodes

5
enter the no. of neighbours for node ::1

enter Neighbour number ,Distance

enter the no.of neighbours for node ::2

enter Neighbour number ,Distance

enter the no.of neighbours for node ::3

enter Neighbour number ,Distance

2
2

enter the no.of neighbours for node ::4

enter Neighbour number ,Distance

enter the no.of neighbours for node ::5

enter Neighbour number ,Distance

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

to find the path

enter starting node

enter ending node

Shortest Path is:

1->3->4->

Minimum cost is 3

Output2:-
enter no.of nodes

enter the no.of neighbours for node ::1

2
enter Neighbour number ,Distance

enter the no.of neighbours for node ::2

enter Neighbour number ,Distance

enter the no.of neighbours for node ::3

enter Neighbour number ,Distance

enter the no.of neighbours for node ::4


4

enter Neighbour number ,Distance

matrix

0 99 2 2

2 0 1 1

99 99 0 1

2 1 1 0

to find the path

enter starting node

enter ending node

2
Shortest Path is:

1->4->2->

Minimum cost is 3

You might also like