CNLabCompleteDocument (1)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

COMPUTER NETWORKS LAB

LIST OF EXPERIMENTS
S.NO TITLE
1 Study of basic network commands and network configuration commands. i) ping (peer to
peer ) ii) ipconfig (switch)
2 Create a network topology and configure a network topology with four PCs, two
switches, and two routers.
3 Compose a Mail for a Client i) POP Client: Gives the server name, user name and
password retrieve the mails and allow manipulation of mail box using POP commands.
ii) SMTP Client: Gives the server name, send email to the recipient using SMTP
commands.Using cisco packet tracer
4 Implement the following data link layer framing methods. i) Character count ii)
Character stuffing iii) Bit stuffing
5 Implement the frame sorting technique used in buffers
6 Implement Dijkstra‘s algorithm to compute the shortest path for the given graph
7 Develop a program to obtain routing table for each node using Distance Vector Routing
Algorithm by considering the given subnet with weights indicating delay between
Nodes.
8 Design and develop a program to compute checksum for the given frame 1101011111
using CRC 12 and CRC 16.
9 Congestion Control using Leaky Bucket Algorithm
10 Using TCP/IP Sockets establish Client Server Communication
Experiment 1
AIM: To establish a peer-peer Communication.

PROCEDURE:

step-1: Take two computers into workspace.

step-2: Since they are of same devices we have to use "Ethernet Cross Over Cable."(------)

step-3: On clicking on the PC we are able to see two ports (1)RS 332 (2)Fast Ethernet0

we have to connect both PC's with 'FastEthernet0'.

step-4: We need to assign IP Address for PC's.

click 'DESKTOP' option --> IP Configuration --> Assume an IP Address


(ex:10.10.10.1)

For second PC (IP Address- 10.10.10.2)

step-5: Now click on 1'st PC

Device --> Command Prompt --> Give Command " ipconfig ".

step-6: To know physical address or MAC address

Give the command in command prompt as "ipconfig /all"

Similar to the both PC's.

To Know whether they are reachable or not.

step-7: 1'st PC Device -->cmdprmpt -->Command as " ping 10.10.10.2"


If connectivity is built, it gives four acknowledgements since 4 packets have been sent.

SWITCHES:

Aim: To Create a network topology and configure a network topology with five PCs and one
switch.

Procedure:

STEP-1: Choose switch (2960) and select end-devices like pc's (5)

STEP-2: Click on switch to see the structure of switch we are able to see 24 ports .We are able
to see separately two parts they are "Gigabitethernet port". Remaining one is "Fastethernetport".

STEP-3: we need straight through cable to connect pc's and switch.

STEP-4: Initially we will be able to see amber colour neat switch because it is reading MAC
address after some time it turns to green colour.

STEP-5: We have to assign IP address for every PC .

STEP-6: Take a packet and choose source and destination in simulation mode.

STEP-7: To see MAC address table


Click switch->CLI port ->press enter ->Type "EN" -> Type "show MAC-address-table".
Experiment 2
ROUTER:

Aim: To Create a network topology and configure a network topology with six PCs, two
switches, and one router.

Procedure:

STEP-1: Create two different LAN's, i.e create two switches with 3 PC's each.

STEP-2: Give IP address to every PC. For each LAN the IP address should be different.

STEP-3: Label each LAN as LAN1(IP address as 10.0.0.1- 10.0.0.3) and LAN2 (192.168.1.1-
192.168.1.3).

STEP-4: Check whether they are correctly connected or not.(Using straight cable)

STEP-5: click on router (2911) and place it.

STEP-6: Connect LAN1 ( GigabitEthernet 0/1 port ) to router(GigabitEthernet 0/0 port).

And connect LAN2 (GigabitEthernet 0/1 port ) to router(GigabitEthernet 0/1 port).

STEP-7: By default port in the routers are turned off, we have to turn it on.

STEP-8: We have to assign IP address to interface i.e for LAN1and LAN2, for that click on
router , for LAN1 in config click on GigabitEthernet 0/0 and assign IP address(IP address
10.0.0.4) . Similarly , for LAN2 in config click on GigabitEthernet 0/1 and assign IP address(IP
address 192.168.1.4). Check port status and turn on for both LANs.

STEP-9: Now click on every individual PC and go to Desktop then to IP configuration and say
default gateway is their respective LAN IP address (i.e for LAN1 devices default gateway is
10.0.0.4 and for LAN 2 devices default gateway is 192.168.1.4).

STEP-10: Now try to ping from one PC of LAN1 to PC of LAN2. (i.e for example, go to
command prompt of 10.0.0.2 and type “ping 192.168.1.2” ).

STEP-11: Take a packet and choose source and destination in simulation mode.
Experiment 3
Experiment 4
Aim: to implement the data link layer framing method using character count
Procedure:
1. Character count is one of the framing methods.
2. This method uses a field in the header to specify the number of characters in the frame.
3. When the data link layer at the destination sees the character count, it knows how many
characters follow and hence where the end of the frame is.

4. The trouble with this algorithm is that the count can be garbled by a transmission error.
5. If the character count of 5 in the second frame of becomes a 7, the destination will get out
of synchronization and will be unable to locate the start of the next frame.
6. Even if the checksum is incorrect so the destination knows that the frame is bad, it still
has no way of telling where the next frame starts.
Algorithm:
1. Start
2. Enter the number of digits
3. Read n
4. Read array elements in a[]
5. For i0 to n step increases by 1
5.1. ka[i]
5.2. ii+k-1
5.3. sumsum+k
6. If n not equal to sum
6.1. Write there is an error
7. For i0 to n step increases by 1
7.1. ka[i]
7.2. display frame, p
7.3. For ji to k+1 step increases by 1
7.3.1. Read a[j]
7.4. ii+k
7.5. increment p
8. End
Program:
importjava.util.*;
classChcount
{
public static void main(String args[])
{
inti,j,x,n,sum=0;
int a[]=new int[20];
int k=0,p=1;
Scanner s=new Scanner(System.in);
System.out.println("enter the no.of numbers");
n=s.nextInt();
System.out.println("enter the numbers");
for(i=0;i<n;i++)
a[i]=s.nextInt();
for(i=0;i<n;i++)
{
k=a[i];
i=i+k-1;
sum=sum+k;
}
if(n!=sum)
{
System.out.println("error");
System.exit(0);
}
for(i=0;i<n;i++)
{
k=a[i];
System.out.println("frame:"+p);
for(j=i;j<k+i;j++)
System.out.println(a[j]);
i=i+k-1;
p++;
}
}
}

OUTPUT:

D:\CN\CN - 2020 - 2021\CN Lab\CN - Week 1>javac Chcount.java

D:\CN\CN - 2020 - 2021\CN Lab\CN - Week 1>java


D:\CN\CN - 2020 - 2021\CN Lab\CN - Week 1>java Chcount
enter the no.of numbers
15
enter the numbers
4
2
1
3
2
7
2
6
3
5
8
4
2
7
9
frame:1
4
2
1
3
frame:2
2
7
frame:3
2
6
frame:4
3
5
8
frame:5
4
2
7
9
CHARACTER STUFFING
Aim: to implement the data link layer framing method using character stuffing.
Procedure:
1. The starting and ending bytes were different, but in recent years most protocols have used
the same byte, called a flag byte, as both the starting and ending delimiter.
2. In this way, if the receiver ever loses synchronization, it can just search for the flag byte
to find the end of the current frame.
3. When binary data, such as object programs or floating-point numbers are being
transmitted, it may easily happen that the flag byte's bit pattern occurs in the data.
4. One way to solve this problem is to have the sender's data link layer insert a special
escape byte (ESC) just before each flag byte in the data.
5. The data link layer on the receiving end removes the escape byte before the data are
given to the network layer.
6. Any single escape byte is part of an escape sequence, whereas a doubled one indicates
that a single escape occurred naturally in the data.

Algorithm:
1. Start
2. Read the character in a
3. nlength of a
4. if n is greater than 8
4.1. write INVALID
5. else
5.1. for i0 to n step increased by 1
5.2. if the character at ‘i’ in a is '*’
5.2.1. display b+a.charAt(i)
5.2.2. display b+a.charAt(i)
5.3. else
5.3.1. display b+a.charAt(i)
6. end

Program:
importjava.util.*;
classChstuff
{
public static void main(String args[])
{
int i=0,n,k;
String a,b="";
Scanner s=new Scanner(System.in);
System.out.println("enter the message");
a=s.next();
n=a.length();
if(n>8)
System.out.println("invalid");
else
{
System.out.println("at transmission medium");
for(i=0;i<n;i++)
{
if(a.charAt(i)=='*')
{
System.out.println(a.charAt(i));
b=b+a.charAt(i);
System.out.println(a.charAt(i));
b=b+a.charAt(i);
}
else
{
System.out.println(a.charAt(i));
b=b+a.charAt(i);
}
}
}
System.out.println("at reciever side");
k=b.length();
for(i=0;i<k;i++)
{
if(b.charAt(i)=='*')
{
System.out.println(b.charAt(i));
i++;
}
else
System.out.println(b.charAt(i));
}
}
}

OUTPUT:
D:\CN\CN - 2020 - 2021\CN Lab\CN - Week 1>javac Chstuff.java
D:\CN\CN - 2020 - 2021\CN Lab\CN - Week 1>java Chstuff
enter the message
a*b+c*d
at transmission medium
a
*
*
b
+
c
*
*
d
atreciever side
a
*
b
+
c
*
D

BIT STUFFING
Aim: to implement the data link layer framing method using bit stuffing.
Procedure:
1. This technique allows data frames to contain an arbitrary number of bits and allows
character codes with an arbitrary number of bits per character.
2. Each frame begins and ends with a special bit pattern, 01111110 (in fact, a flag byte).
3. Whenever the sender's data link layer encounters five consecutive 1s in the data, it
automatically stuffs a 0 bit into the outgoing bit stream.
4. When the receiver sees five consecutive incoming 1 bits, followed by a 0 bit, it
automatically destuffs (i.e., deletes) the 0 bit.

Algorithm:
1. Start
2. Read n number of elements
3. Display enter message
4. For i0 to n step increased by 1
4.1. Read array elements in a[i]
5. k=j=0
6. For i0 to n step increased by 1
6.1. If a[i]1
6.1.1. k++
6.2. Else
6.2.1. k0
6.3. b[j]a[i]
6.4. if k5
6.4.1. b[++j]0
6.4.2. k0
6.5. j++
7. Display in transmission medium
8. For k0 to j step increased by 1
8.1. Display b[k]
9. For i0 to j step increased by 1
9.1. If b[i] is equal to 1
9.1.1. Increment k
9.2. Else
9.2.1. k0
9.3. c[s]b[i]
9.4. if k is equal to 5
9.4.1. increment i
9.4.2. k0
9.5. increment s
10. Display in receiver
11. For k0 to s step increased by 1
11.1. Display c[k]
12. End

Program:

importjava.util.*;
classBstuff
{
public static void main(String args[])
{
inti,n,k,j,s=0;
int a[]=new int[10];
int b[]=new int[20];
int c[]=new int[10];
System.out.println("enter the no.of elements");
Scanner x=new Scanner(System.in);
n=x.nextInt();
System.out.println("enter the message");
for(i=0;i<n;i++)
a[i]=x.nextInt();
k=j=0;
for(i=0;i<n;i++)
{
if(a[i]==1)
k++;
else
k=0;
b[j]=a[i];
if(k==5)
{
b[++j]=0;
k=0;
}
j++;
}
System.out.println("at transmission medium");
for(k=0;k<j;k++)
{
System.out.println(b[k]);
}
k=0;
for(i=0;i<j;i++)
{
if(b[i]==1)
k++;
else
k=0;
c[s]=b[i];
if(k==5)
{
i++;
k=0;
}
s++;
}
System.out.println("at transmission medium");
for(k=0;k<s;k++)
{
System.out.println(c[k]);
}
}
}

OUTPUT:

D:\CN\CN - 2020 - 2021\CN Lab\CN - Week 1>javac Bstuff.java


D:\CN\CN - 2020 - 2021\CN Lab\CN - Week 1>java Bstuff
enter the no.of elements
10
enter the message
1
1
1
1
1
1
0
1
1
1
at transmission medium
1
1
1
1
1
0
1
0
1
1
1
at transmission medium
1
1
1
1
1
1
0
1
1
Experiment 5
Aim:To Implement the frame sorting technique used in buffers
Theory:
The data link layer divides the stream of bits received from the network layer into manageable
data units called frames. If frames are to be distributed to different systems on the network, the
Data link layer adds a header to the frame to define the sender and/or receiver of the frame. Each
Data link layer has its own frame format. One of the fields defined in the format is the maximum size
of the data field. In other words, when datagram is encapsulated in a frame, the total size of the
datagram must be less than this maximum size, which is defined by restriction imposed by the
hardware and software used in the network. IP Datagram Header MTU Trailer The value of MTU
differs from one physical network to another In order to make IP protocol portable/independent of
the physical network, the packagers decided to make the maximum length of the IP datagram equal
to the largest Maximum Transfer Unit (MTU) defined so far. However for other physical networks
we must divide the datagrams to make it possible to pass through these networks. This is called
fragmentation. When a datagram is fragmented, each fragmented has its own header. A fragmented
datagram may itself be fragmented if it encounters a network with an even smaller MTU. In another
words, a datagram may be fragmented several times before it reached the final destination and
also, the datagrams referred to as (frames in Data link layer) may arrives out of order at destination.
Hence sorting of frames need to be done at the destination to recover the original data.
Program:
import java.io.*;
importjava.util.*;
classFramesort
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter the number of frames:");
int n=s.nextInt();
intseq[]=new int[n];
String msg[]=new String[n];
for(inti=0;i<n;i++)
{
System.out.println("enter the sequence no.of frame"+(i+1)+":");
seq[i]=s.nextInt();
s.nextLine();
System.out.println("enter the msg in frmae"+(i+1)+":");
msg[i]=s.nextLine();
}
for(inti=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(seq[j]>seq[i])
{
int temp=seq[i];
seq[i]=seq[j];
seq[j]=temp;
String tem=msg[i];
msg[i]=msg[j];
msg[j]=tem;
}
}
}
for(inti=0;i<n;i++)
{
System.out.println("frame sequence no ->"+seq[i]);
System.out.println("frame message ->"+msg[i]);
}
}
}
/*output:
C:\Users\IT\Desktop\CN LAB>java Framesort
enter the number of frames:
3
enter the sequence no.of frame1:
2
enter the msg in frmae1:
NETWORKS
enter the sequence no.of frame2:
1
enter the msg in frmae2:
COMPUTER
enter the sequence no.of frame3:
3
enter the msg in frmae3:
LABORATORY
frame sequence no ->1
frame message -> COMPUTER
frame sequence no ->2
frame message -> NETWORKS
frame sequence no ->3
frame message -> LABORATORY
Experiment 6
AIM: To write a JAVA program to implement all shortest path routing algorithm
throughDijikstra’s Algorithm.
Procedure:To find the shortest path from A to D.

CODE:
Import java.util.*;
Class allshortestpath
{
public static void main(String args[])
{ Ints,t,n,k,i,j,cost=0;
int pre[],lengths[],status[],dist[][],path[];
finalint INFINITY=100;
System.out.println("the distance must be less than 100");
System.out.println("enter the number of routers");
Scanner x=new Scanner(System.in);
n=x.nextInt();
path= new int[n];
dist=new int[n][n];
pre= new int[n];
lengths=new int[n];
status=new int[n];
System.out.println("ENTER COST MATRIX");
1
for(i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
dist[i][j]=x.nextInt();
}
System.out.println("");
}
System.out.println("the graph is");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i!=j)&&(dist[i][j]==0))
{
dist[i][j]=INFINITY;
}
}
}f
or(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(dist[i][j]+" ");
}
System.out.println("");
}
System.out.println("enter the source node");
s=x.nextInt();
for(int p=1;p<n;p++)
{
t=p;
for(i=0;i<n;i++)
{
pre[i]=-1;lengths[i]=INFINITY;status[i]=0;
}l
engths[t]=0;
status[t]=1;
k=t;
do
{
for(i=0;i<n;i++)
{
if(dist[k][i]!=0&&status[i]==0)
{
if(lengths[k]+dist[k][i]<lengths[i])
{
pre[i]=k;
lengths[i]=lengths[k]+dist[k][i];
}
}
2
}k
=0;
int min=INFINITY;
for(i=0;i<n;i++)
{
if(status[i]==0&&(lengths[i]<min))
{
min=lengths[i];
k=i;
}
}s
tatus[k]=1;
}while(k!=s);
i=0;
k=s;
do
{
path[i]=k;
if(i!=0)
cost=cost+dist[path[i-1]][path[i]];
k=pre[k];
i++;
}while(k>=0);
System.out.println("\n\nthe shortest path from source "+s+ "to
destination "+p+" :");
for(k=0;k<i;k++)
{
System.out.print(path[k]+"-->");
}
System.out.print("cost"+cost);
cost=0;
}
}
}/
*
OUTPUT:
the distance must be less than 100
enter the number of routers:8
ENTER COST MATRIX
ABCDEFGH
A 0 2 100 100 100 100 6 100
B 2 0 7 100 2 100 100 100
C 100 7 0 3 100 3 100 100
D 100 100 3 0 100 100 100 2
E 100 2 100 100 0 2 1 100
F 100 100 3 100 2 0 100 2
G 6 100 100 100 1 100 0 4
H 100 100 100 2 100 2 4 0
the graph is
0 2 100 100 100 100 6 100
2 0 7 100 2 100 100 100
100 7 0 3 100 3 100 100
100 100 3 0 100 100 100 2
100 2 100 100 0 2 1 100
100 100 3 100 2 0 100 2
6 100 100 100 1 100 0 4
100 100 100 2 100 2 4 0
enter the source node : 0
the shortest path from source 0to destination 1 :
0-->1-->cost2
the shortest path from source 0to destination 2 :
0-->1-->2-->cost9
the shortest path from source 0to destination 3 :
0-->1-->4-->5-->7-->3-->cost10
the shortest path from source 0to destination 4 :
0-->1-->4-->cost4
the shortest path from source 0to destination 5 :
0-->1-->4-->5-->cost6
the shortest path from source 0to destination 6 :
0-->1-->4-->6-->cost5
the shortest path from source 0to destination 7 :
0-->1-->4-->5-->7-->cost8
Experiment 7
Aim: To write a JAVA program to implement Distance Vector Routing Algorithm.

Distance Vector Routing: Distance vector routing algorithm operates by having each router
maintain a table (i.e., a vector) giving the best known distance to each destination and which link
to use to get there.
✓ The distance vector routing algorithm is sometimes called by other names, most
commonly the distributed Bellman-Ford routing algorithm.

Procedure:

• The first four columns of part (b) show the delay vectors received from the neighbors of
router J. A claims to have a 12-msec delay to B, a 25-msec delay to C, a 40-msec delay to
D, etc.
• Suppose that J has measured or estimated its delay to its neighbors, A, I, H, and K as 8,
10, 12, and 6 msec, respectively.
• Consider how J computes its new route to router G. It knows that it can get to Ain 8
msec, and Aclaims to be able to get to G in 18 msec, so J knows it can count on a delay of
26 msec to G if it forwards packets bound for G to A.
• Similarly, it computes the delay to G via I, H, and K as 41 (31 + 10), 18 (6 + 12), and 37
(31 + 6) msec, respectively. The best of these values is 18, so it makes an entry in its
routing table that the delay to G is 18 msec and that the route to use is via H. The same
calculation is performed for all the other destinations, with the new routing table shown
in the last column of the figure.
Program:

import java.io.*;
import java.util.*;
import java.lang.*;
classDVector
{
public static void main(String args[ ])
{
Inti,j,n,s,min=100,p;
int subnet[ ][ ],path[ ][ ],delay[][],edelay[ ],ndelay[ ],line[ ];
System.out.println("Enter no. of routers : ");
Scanner x= new Scanner(System.in);
n=x.nextInt();
subnet=new int[n][n];
delay=new int[n][n];
edelay=new int[n];
ndelay=new int[n];
line=new int[n];
System.out.println ("Enter subnet :");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
subnet[i][j]=x.nextInt();
}
}
System.out.println("The Subnet is :");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.println(subnet[i][j]+"\t");
}
System.out.println();
}
System.out.println("Enter Source node: ");
s=x.nextInt();
for(i=0;i<n;i++)
{
if(subnet[s][i]==1)
{
System.out.println("Enter Delay vectors for neighbour router-"+i);
for(j=0;j<n;j++)
{
delay[i][j]=x.nextInt();

}
System.out.println("Enter Stimulated delay from source-"+s+"toneighbour-"+i+":");
edelay[i]=x.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(subnet[s][j]==1)
{
if(i!=s)
{
p=edelay[j]+delay[j][i];
if(p<min)
{
min=p;
line[i]=j;
ndelay[i ]=min;
}
}
else
{
ndelay[i]=0;
line[i]=s;
}
}
}
min=100;
}
System.out.println("The new delay vector from source "+s+"is: ");
System.out.println("DELAY LINE");
System.out.println("----- ----");
for(i=0;i<n;i++)
{
System.out.println(ndelay[i]+" "+line[i]);
}
}
}

Result
C:\Users\SVEC\Desktop\cn>java DVector
Enter no. of routers :
12
Enter subnet :
010010000100
101000100000
010110000000
001000010000
101001001000
000010100000
010001010000
000100100101
000010000100
100000011010
000000000101
000000010010
The Subnet is :
0
1
0
0
1
0
0
0
0
1
0
0

1
0
1
0
0
0
1
0
0
0
0
0

0
1
0
1
1
0
0
0
0
0
0
0

0
0
1
0
0
0
0
1
0
0
0
0

1
0
1
0
0
1
0
0
1
0
0
0

0
0
0
0
1
0
1
0
0
0
0
0

0
1
0
0
0
1
0
1
0
0
0
0

0
0
0
1
0
0
1
0
0
1
0
1

0
0
0
0
1
0
0
0
0
1
0
0

1
0
0
0
0
0
0
1
1
0
1
0
0
0
0
0
0
0
0
0
0
1
0
1

0
0
0
0
0
0
0
1
0
0
1
0
Enter Source node:
9
Enter Delay vectors for neighbour router-0
0
12
25
40
14
23
18
17
21
9
24
29
Enter Stimulated delay from source-9to neighbour-0 :
8
Enter Delay vectors for neighbour router-7
30
31
19
8
30
19
6
0
14
7
22
9
Enter Stimulated delay from source-9to neighbour-7 :
12
Enter Delay vectors for neighbour router-8
24
36
18
27
7
20
31
20
0
11
22
33
Enter Stimulated delay from source-9to neighbour-8 :
10
Enter Delay vectors for neighbour router-10
21
28
36
24
22
40
31
19
22
10
0
9
Enter Stimulated delay from source-9to neighbour-10 :
6
The new delay vector from source 9is:
DELAY LINE
8 0
20 0
28 8
20 7
17 8
30 8
18 7
12 7
10 8
0 9
6 10
15 10
Experiment 8
Program:
import java.io.*;
class CRC1
{
public static void main(String args[]) throws IOException
{
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
intdata_bits, divisor_bits, tot_length;

System.out.println("Enter number of data bits M(x): ");


data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in G(x) : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];

System.out.println("Enter G(x) : ");


for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
tot_length=data_bits+divisor_bits-1;

div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];

for(int i=0;i<data.length;i++)
div[i]=data[i];

System.out.print("Dividend (after appending 0's) are : ");


for(int i=0; i<div.length; i++)
System.out.print(div[i]);
System.out.println();

for(int j=0; j<div.length; j++){


rem[j] = div[j];
}

rem=divide(div, divisor, rem);


for(int i=0;i<div.length;i++)
{
crc[i]=(div[i]^rem[i]);
}

System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);

System.out.println();
System.out.println("Enter CRC code of : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());

for(int j=0; j<crc.length; j++){


rem[j] = crc[j];
}

rem=divide(crc, divisor, rem);

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


{
if(rem[i]!=0)
{
System.out.println("Error in transmission,plx resend the data");
break;
}
if(i==rem.length-1)
System.out.println("No Error");
}

staticint[] divide(int div[],int divisor[], int rem[])


{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);
while(rem[cur]==0 && cur!=rem.length-1)
cur++;

if((rem.length-cur)<divisor.length)
break;
}
return rem;
}
}

Output:
CRC 4 Output
C:\Users\IT\Desktop\CN LAB>java CRC1
Enter number of data bits M(x):
10
Enter data bits :
1
1
0
1
0
1
1
1
1
1
Enter number of bits in G(x) :
5
Enter G(x) :
1
0
0
1
1
Dividend (after appending 0's) are : 11010111110000
CRC code :
11010111110010
Enter CRC code of :
1
1
0
1
0
1
1
1
1
1
0
0
1
0
No Error

CRC 12 Output

C:\Users\IT\Desktop\CN LAB>
C:\Users\IT\Desktop\CN LAB>java CRC1

Enter number of data bits M(x):

10

Enter data bits :

Enter number of bits in G(x) :

13

Enter G(x) :

1
1

Dividend (after appending 0's) are : 1101011111000000000000

CRC code :

1101011111010001001001

Enter CRC code of :


1

1
1

CRC 16 Output
C:\Users\IT\Desktop\CN LAB>
C:\Users\IT\Desktop\CN LAB>java CRC1
Enter number of data bits M(x):
10
Enter data bits :
1
1
0
1
0
1
1
1
1
1
Enter number of bits in G(x) :
17
Enter G(x) :
1
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
Dividend (after appending 0's) are : 11010111110000000000000000
CRC code :
11010111110000101111000010
Enter CRC code of :
1
1
0
1
0
1
1
1
1
1
0
0
0
0
1
0
1
1
1
1
0
0
0
0
1
0
No Error
EXPERIMENT 9
/* Aim: Write a program for congestion control using leaky bucket algorithm */

Procedure:
Congestion: A state occurring in network layer when the message traffic is so heavy that
it slows down network response time.
Effects of Congestion:
• As delay increases, performance decreases.
• If delay increases, retransmission occurs, making situation worse.
Congestion control algorithms
• Leaky Bucket Algorithm
Let us consider an example to understand
Imagine a bucket with a small hole in the bottom.No matter at what rate water enters the
bucket, the outflow is at constant rate.When the bucket is full with water additional water
entering spills over the sides and is lost.

Similarly, each network interface contains a leaky bucket and the following steps are
involved in leaky bucket algorithm:
1. When host wants to send packet, packet is thrown into the bucket.
2. The bucket leaks at a constant rate, meaning the network interface transmits packets
at a constant rate.
3. Bursty traffic is converted to a uniform traffic by the leaky bucket.
4. In practice the bucket is a finite queue that outputs at a finite rate.
• Token bucket Algorithm
Need of token bucket Algorithm:-
The leaky bucket algorithm enforces output pattern at the average rate, no matter how
bursty the traffic is. So in order to deal with the bursty traffic we need a flexible algorithm
so that the data is not lost. One such algorithm is token bucket algorithm.
Steps of this algorithm can be described as follows:
1. In regular intervals tokens are thrown into the bucket. ƒ
2. The bucket has a maximum capacity. ƒ
3. If there is a ready packet, a token is removed from the bucket, and the packet is sent.
4. If there is no token in the bucket, the packet cannot be sent.
Let’s understand with an example,
In figure (A) we see a bucket holding three tokens, with five packets waiting to be
transmitted. For a packet to be transmitted, it must capture and destroy one token. In
figure (B) We see that three of the five packets have gotten through, but the other two
are stuck waiting for more tokens to be generated.
Ways in which token bucket is superior to leaky bucket:
The leaky bucket algorithm controls the rate at which the packets are introduced in the
network, but it is very conservative in nature. Some flexibility is introduced in the token
bucket algorithm. In the token bucket, algorithm tokens are generated at each tick (up to
a certain limit). For an incoming packet to be transmitted, it must capture a token and the
transmission takes place at the same rate. Hence some of the busty packets are
transmitted at the same rate if tokens are available and thus introduces some amount of
flexibility in the system.
Formula: M * s = C + ρ * s
where S – is time taken
M – Maximum output rate
ρ – Token arrival rate
C – Capacity of the token bucket in byte
Let’s understand with an example,

Program:

/*
variables used in leaky bucket
n=no of queries,
sto=storage,
ops=output packet size,
ips=input packet size,
bs=bucket size,
sl=size left.
*/

importjava.util.*;
import java.io.*;
class Leaky
{
public static void main (String args[]) {
intn,sto,bs,ips,ops,sl;
Scanner sc = new Scanner(System.in);
//Initial total number of packets that are passing into the bucket
System.out.println("Enter total number of quries entering\n");
n=sc.nextInt();
//first estimate total packets in the bucket
System.out.println("Enter the total number of packets in the bucket \n");
sto=sc.nextInt();
//the total number of packets that can be filled in the bucket
System.out.println("total number of packets that can be filled in the bucket \n");
bs=sc.nextInt();
//number of input packets at a particular time
System.out.println("total number of packets that are inputed into the bucket \n");
ips=sc.nextInt();
//no. of packets that quit the bucket
System.out.println("total number of packets that are comming out the bucket \n");
ops=sc.nextInt();
for(int i=0;i<n;i++)
{
System.out.println("At time:\n"+(i+1));
// total size left
sl=bs-sto;
System.out.println("size left "+sl);
if(ips<=(sl))
{
sto+=ips;
System.out.println("Packets received= "+sto+ " out of bucket size= "+bs);
System.out.println("Packet loss is 0 ");
}
else
{
System.out.println("Packet loss = "+(ips-(sl)));
//if the bucket is at filled fully
sto=bs;
System.out.println("Buffer size= "+sto+ " out of bucket size= "+bs);
}
System.out.println("output sent "+ops);
sto-=ops;
System.out.println("After output sent storage is "+sto);
System.out.println("------------------------------------------------------------------");
}
}
}

/*OUTPUT
C:\Users\Jyoshna\Desktop>javac Leaky.java

C:\Users\Jyoshna\Desktop>java Leaky
Enter total number of quries entering

5
Enter the total number of packets in the bucket

42
total number of packets that can be filled in the bucket
4
total number of packets that are inputed into the bucket

3
total number of packets that are comming out the bucket

2
At time:
1
size left -38
Packet loss = 41
Buffer size= 4 out of bucket size= 4
output sent 2
After output sent storage is 2
------------------------------------------------------------------
At time:
2
size left 2
Packet loss = 1
Buffer size= 4 out of bucket size= 4
output sent 2
After output sent storage is 2
------------------------------------------------------------------
At time:
3
size left 2
Packet loss = 1
Buffer size= 4 out of bucket size= 4
output sent 2
After output sent storage is 2
------------------------------------------------------------------
At time:
4
size left 2
Packet loss = 1
Buffer size= 4 out of bucket size= 4
output sent 2
After output sent storage is 2
------------------------------------------------------------------
At time:
5
size left 2
Packet loss = 1
Buffer size= 4 out of bucket size= 4
output sent 2
After output sent storage is 2
------------------------------------------------------------------
*/
EXPERIMENT 10
/*Aim:Using TCP/IP sockets, write a java to establish client-server communication. */

Procedure:

One-way Client and Server setup where a Client connects, sends messages to the server
and the server shows them using a socket connection. There’s a lot of low-level stuff that
needs to happen for these things to work but the Java API networking package (java.net)
takes care of all of that, making network programming very easy for programmers.
Client-Side Programming

Establish a Socket Connection


To connect to another machine we need a socket connection. A socket connection means
the two machines have information about each other’s network location (IP Address) and
TCP port. The java.net.Socket class represents a Socket. To open a socket:
Socket socket = new Socket(“127.0.0.1”, 5000)
• The first argument – IP address of Server. ( 127.0.0.1 is the IP address of
localhost, where code will run on the single stand-alone machine).
• The second argument – TCP Port. (Just a number representing which application to
run on a server. For example, HTTP runs on port 80. Port number can be from 0 to
65535)
Communication
To communicate over a socket connection, streams are used to both input and output the
data.
Closing the connection
The socket connection is closed explicitly once the message to the server is sent.
In the program, the Client keeps reading input from a user and sends it to the server until
“Over” is typed.

Server Programming
Establish a Socket Connection
To write a server application two sockets are needed.
• A ServerSocket which waits for the client requests (when a client makes a new
Socket())
• A plain old Socket socket to use for communication with the client.
Communication
getOutputStream() method is used to send the output through the socket.
Close the Connection
After finishing, it is important to close the connection by closing the socket as well as
input/output streams

Program:
Save as Client.java

// A Java program for a Client


importjava.net.*;
importjava.io.*;

publicclassClient
{
// initialize socket and input output streams
privateSocket socket = null;
privateDataInputStream input = null;
privateDataOutputStream out = null;

// constructor to put ip address and port


publicClient(String address, intport)
{
// establish a connection
try
{
socket = newSocket(address, port);
System.out.println("Connected");

// takes input from terminal


input = newDataInputStream(System.in);

// sends output to the socket


out = newDataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}

// string to read message from input


String line = "";

// keep reading until "Over" is input


while(!line.equals("Over"))
{
try
{
line = input.readLine();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println(i);
}
}

publicstaticvoidmain(String args[])
{
Client client = newClient("127.0.0.1", 5000);
}
}

Save as Server.java

// A Java program for a Server


importjava.net.*;
importjava.io.*;

publicclassServer
{
//initialize socket and input stream
privateSocket socket = null;
privateServerSocket server = null;
privateDataInputStream in = null;

// constructor with port


publicServer(intport)
{
// starts server and waits for a connection
try
{
server = newServerSocket(port);
System.out.println("Server started");

System.out.println("Waiting for a client ...");

socket = server.accept();
System.out.println("Client accepted");

// takes input from the client socket


in = newDataInputStream(
newBufferedInputStream(socket.getInputStream()));

String line = "";


// reads message from client until "Over" is sent
while(!line.equals("Over"))
{
try
{
line = in.readUTF();
System.out.println(line);

}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");

// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}

publicstaticvoidmain(String args[])
{
Server server = newServer(5000);
}
}

Important Points

• Server application makes a ServerSocket on a specific port which is 5000. This starts
our Server listening for client requests coming in for port 5000.
• Then Server makes a new Socket to communicate with the client.
socket = server.accept()
• The accept() method blocks(just sits there) until a client connects to the server.
• Then we take input from the socket using getInputStream() method. Our Server keeps
receiving messages until the Client sends “Over”.
• After we’re done we close the connection by closing the socket and the input stream.
• To run the Client and Server application on your machine, compile both of them. Then
first run the server application and then run the Client application.

Execution procedure:

To run on Terminal or Command Prompt


Open two windows one for Server and another for Client
1. First run the Server application as,
$ java Server
Server started
Waiting for a client …
2. Then run the Client application on another terminal as,
$ java Client
It will show – Connected and the server accepts the client and shows,
Client accepted
3. Then you can start typing messages in the Client window. Here is a sample input to the
Client
Hello
I made my first socket connection
Over
Which the Server simultaneously receives and shows,
Hello
I made my first socket connection
Over
Closing connection
Notice that sending “Over” closes the connection between the Client and the Server just
like said before.

You might also like