You are on page 1of 10

G NATA RAJU M.

Tech (CNIS) I Sem Roll No: 11031D6427


Program No: Page No: 1
COMPUTER NETWORKS LAB

Bit Stuffing
package CN_Lab;
import java.io.*;
public class BitStuff {
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.print("Enter the binary message: ");
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
String data = br.readLine();
String res = new String();
int counter = 0;
for(int i=0;i<data.length();i++)
{ System.out.println(data.charAt(i));
if (data.charAt(i)!='1' && data.charAt(i)!='0')
{ System.out.println("Enter only Binary values");
return; }
if(data.charAt(i) == '1')
{ counter++;
res = res + data.charAt(i); }
else
{ res = res + data.charAt(i);
counter = 0; }
if(counter == 5)
{ res = res + '0';
counter = 0; }
}
System.out.println("The encrypted string is: " +res);
} }

OUTPUT:
Enter the binary message: 11111111110000000
The encrypted string is: 1111101111100000000


G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 2
COMPUTER NETWORKS LAB

Character Stuffing
package CN_Lab;
import java.io.*;
public class CharStuff {
public static void main(String[] args) throws IOException {
// TODO code application logic here
System.out.print("Enter the input: ");
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
String data = br.readLine();
String res = "DLESTX";
int i;
for(i=0;i<data.length()-2;i++)
{
if ((data.charAt(i) == 'd' || data.charAt(i) == 'D') && (data.charAt(i+1) == 'l' ||
data.charAt(i+1) == 'L') && (data.charAt(i+2) == 'e' || data.charAt(i+2) == 'E'))
{
res = res + "DLE";
}
res = res + data.charAt(i);
}
res = res + data.charAt(i) + data.charAt(i+1) + "DLEETX";
System.out.println("Output is: " +res);
}

}

OUTPUT:
Enter the input: dlefdhgddieDLEjvdvhDle
Output is: DLESTXDLEdlefdhgddieDLEDLEjvdvhDLEDleDLEETX





G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 3
COMPUTER NETWORKS LAB

CRC
package CN_Lab;
import java.util.*;
public class CRC {

static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// TODO code application logic here

char[] msg = new char[20];
char[] gen = new char[20];
System.out.print("Enter the message string: ");
String msgs = sc.nextLine();
System.out.print("Enter the generator string: ");
String gens = sc.nextLine();
for(int i=0;i<msgs.length();i++)
msg[i] = msgs.charAt(i);
for(int i=0;i<gens.length();i++)
gen[i]=gens.charAt(i);
//Adding Zeroes
for(int i=msgs.length();i<msgs.length() + gens.length()-1;i++)
msg[i]='0';
//Printing Appended Message
System.out.print("Appended string is: ");
for(int i=0;i<msgs.length() + gens.length()-1;i++)
System.out.print(msg[i]);
System.out.println();
//Computing CRC
for(int i=0;i<msgs.length();i++)
{
if(msg[i]!='1' && msg[i]!='0')
{
System.out.println("Entered message is wrong");
return;
}

G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 4
COMPUTER NETWORKS LAB

if(msg[i]=='1')
for(int j=0,k=i;j<gens.length();j++,k++)
{ msg[k]=xor(msg[k],gens.charAt(j)); }
for(int l=0;l<msgs.length()+gens.length()-1;l++)
System.out.print(msg[l]);
System.out.println();
}
//Solution
System.out.print("Checksummed message is: " +msgs);
for(int i=msgs.length();i<msgs.length() + gens.length()-1;i++)
System.out.print(msg[i]);
System.out.println();
}
public static char xor(char x,char y)
{
if((x=='1'&&y=='1')||(x=='0'&&y=='0'))
return '0';
else
return '1'; }
}







OUTPUT:
Enter the message string: 1010101111000
Enter the generator string: 1010101
Appended string is: 1010101111000000000
0000000111000000000
0000000111000000000
0000000111000000000
0000000111000000000
0000000111000000000
0000000111000000000
0000000111000000000
0000000010010100000
0000000000111110000
0000000000111110000
0000000000010100100
0000000000000001110
0000000000000001110
Checksummed message is: 1010101111000001110

G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 5
COMPUTER NETWORKS LAB

Shortest path
package CN_Lab;
import java.io.*;
class Spath
{
int pres;
int length;
boolean label;
}
public class ShortestPath {

public static void main(String[] args) throws IOException {
// TODO code application logic here

int i,j,s,n,t;
int k,min;
int dist[][] = new int[10][10];
int path[] = new int[4];
Spath ob[] = new Spath[20];
for(i=0;i<10;i++)
ob[i] = new Spath();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter n: ");
n = Integer.parseInt(br.readLine());
System.out.println("Enter adj matrix: ");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
dist[i][j] = Integer.parseInt(br.readLine());
}
}
System.out.print("Enter value of t: ");
t = Integer.parseInt(br.readLine());
System.out.print("Enter value of s: ");
s = Integer.parseInt(br.readLine());
for(i=0;i<n;i++) {

G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 6
COMPUTER NETWORKS LAB

ob[i].pres = -1;
ob[i].length = 100000;
ob[i].label = false;
}
ob[t].length = 0;
ob[t].label = true;
k = t;
do {
for(i=0;i<n;i++)
if((dist[k][i]!=0)&&(ob[i].label==false)) {
if(ob[k].length+dist[k][i]<ob[i].length) {
ob[i].pres = k;
ob[i].length=ob[k].length+dist[k][i];
}
}
k = 0;
min = 100000;
for(i=0;i<n;i++)
if((ob[i].label==false)&&(ob[i].length<min)) {
min = ob[i].length;
k = i;
}
ob[k].label = true;
}
while(k!=s);
i=0;
k=s;
int c=0;
do {
path[i++]=k;
c++;
k=ob[k].pres;
}
while(k>=0);

G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 7
COMPUTER NETWORKS LAB

System.out.println("Shortest path is: ");
for(i=c-1;i>=0;i--)
System.out.println(path[i]);
}
}
OUTPUT:

Enter n: 3
Enter adj matrix:
0
1
9
1
0
2
9
2
0
Enter value of t: 0
Enter value of s: 2
Shortest path is:
0
1
2


G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 8
COMPUTER NETWORKS LAB

Distance Vector
import java.io.*;
import java.util.*;
public class DVR {

public static void main(String[] args) throws IOException {
// TODO code application logic here

Scanner sc = new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int min,temp;
String minnode = null;
System.out.print("Enter no of nodes: ");
int n = sc.nextInt();
System.out.println("Enter the name of each node: ");
String[] name = new String[n];
for(int i=0;i<n;i++)
{
name[i] = br.readLine();
}
System.out.print("Choose one of the node: ");
String chosennode = br.readLine();
System.out.print("Enter the no. of adjacent nodes to "+chosennode+ ": " );
int na = sc.nextInt();
System.out.println("Enter the adjacent node and delay to that node from
"+chosennode+ ": ");
String[] adjnod = new String[na];
int[] delay = new int[na];
int[] newdelay = new int[n];
String[] newroute = new String[n];
for(int i=0;i<na;i++)
{
adjnod[i] = br.readLine();
delay[i] = sc.nextInt();
}

G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 9
COMPUTER NETWORKS LAB

System.out.println("Enter the routing table of adjacent nodes: ");
int[][] dist = new int[na][n];
for(int i=0;i<na;i++)
{
System.out.println("for "+adjnod[i]);
for(int j=0;j<n;j++)
{
System.out.print(adjnod[i]+","+name[j]+" = ");
dist[i][j]=sc.nextInt();
}
}
for(int i=0;i<n;i++)
{
min = 1000;
for(int j=0;j<na;j++)
{
temp = delay[j]+dist[j][i];
if(temp<min)
{
min = temp;
minnode=adjnod[j];
}
}
if(name[i].equals(chosennode))
{ newdelay[i]=0;
newroute[i]="-"; }
else
{ newdelay[i]=min;
newroute[i]=minnode; }
System.out.print("Path is: ");
System.out.println(name[i]+" ==> "+newdelay[i]+"-"+newroute[i]);
}
}
}

G NATA RAJU M.Tech (CNIS) I Sem Roll No: 11031D6427
Program No: Page No: 10
COMPUTER NETWORKS LAB

OUTPUT:

Enter no of nodes: 5
Enter the name of each node:
a
b
c
d
e
Choose one of the node: a
Enter the no. of adjacent nodes to a: 2
Enter the adjacent node and delay to that node from a:
b
1
c
2
Enter the routing table of adjacent nodes:
for b
b,a = 1
b,b = 0
b,c = 1000
b,d = 5
b,e = 3
for c
c,a = 2
c,b = 1000
c,c = 0
c,d = 3
c,e = 6
Path is: a ==> 0--
Path is: b ==> 1-b
Path is: c ==> 2-c
Path is: d ==> 5-c
Path is: e ==> 4-b

You might also like