CNLabCompleteDocument (1)
CNLabCompleteDocument (1)
CNLabCompleteDocument (1)
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-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
Device --> Command Prompt --> Give Command " ipconfig ".
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-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-6: Take a packet and choose source and destination in simulation mode.
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-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 i0 to n step increases by 1
5.1. ka[i]
5.2. ii+k-1
5.3. sumsum+k
6. If n not equal to sum
6.1. Write there is an error
7. For i0 to n step increases by 1
7.1. ka[i]
7.2. display frame, p
7.3. For ji to k+1 step increases by 1
7.3.1. Read a[j]
7.4. ii+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:
Algorithm:
1. Start
2. Read the character in a
3. nlength of a
4. if n is greater than 8
4.1. write INVALID
5. else
5.1. for i0 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 i0 to n step increased by 1
4.1. Read array elements in a[i]
5. k=j=0
6. For i0 to n step increased by 1
6.1. If a[i]1
6.1.1. k++
6.2. Else
6.2.1. k0
6.3. b[j]a[i]
6.4. if k5
6.4.1. b[++j]0
6.4.2. k0
6.5. j++
7. Display in transmission medium
8. For k0 to j step increased by 1
8.1. Display b[k]
9. For i0 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. k0
9.3. c[s]b[i]
9.4. if k is equal to 5
9.4.1. increment i
9.4.2. k0
9.5. increment s
10. Display in receiver
11. For k0 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:
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;
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.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());
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
10
13
Enter G(x) :
1
1
CRC code :
1101011111010001001001
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
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
publicclassClient
{
// initialize socket and input output streams
privateSocket socket = null;
privateDataInputStream input = null;
privateDataOutputStream out = null;
publicstaticvoidmain(String args[])
{
Client client = newClient("127.0.0.1", 5000);
}
}
Save as Server.java
publicclassServer
{
//initialize socket and input stream
privateSocket socket = null;
privateServerSocket server = null;
privateDataInputStream in = null;
socket = server.accept();
System.out.println("Client accepted");
}
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: