1st Cycle Experiments CL LAB II 3
1st Cycle Experiments CL LAB II 3
1st Cycle Experiments CL LAB II 3
Computer Network
OSI model
OSI (Open Systems Interconnection) model was created by the International Organization for
Standardization (ISO), an international standard-setting body. It was designed to be a
reference model for describing the functions of a communication system. It has seven layers,
with each layer describing a different function of data traveling through a network.
Application (Layer 7)
OSI Model, Layer 7, supports application and end-user processes. Communication partners
are identified, quality of service is identified, user authentication and privacy are considered,
and any constraints on datasyntax are identified. Everything at this layer is application-
specific. This layer provides application services for file transfers, e-mail, and
other network software services. Telnet and FTP are applications that exist entirely in the
application level. Tiered application architectures are part of this layer.
Layer 7 Application examples include WWW browsers, NFS, SNMP, Telnet, HTTP, FTP
Presentation (Layer 6)
This layer provides independence from differences in data representation (e.g., encryption) by
translating from application to network format, and vice versa. The presentation layer works
to transform data into the form that the application layer can accept. This layer formats and
encrypts data to be sent across a network, providing freedom from compatibility problems. It
is sometimes called the syntax layer.
Layer 6 Presentation examples include encryption, ASCII, EBCDIC, TIFF, GIF, PICT,
JPEG, MPEG, MIDI.
Session (Layer 5)
This layer establishes, manages and terminates connections between applications. The session
layer sets up, coordinates, and terminates conversations, exchanges, and dialogues between
the applications at each end. It deals with session and connection coordination.
Layer 5 Session examples include NFS, NetBios names, RPC, SQL.
Transport (Layer 4)
OSI Model, Layer 4, provides transparent transfer of data between end systems, or hosts, and
is responsible for end-to-end error recovery and flow control. It ensures complete data
transfer.
Layer 4 Transport examples include SPX, TCP, UDP.
Network (Layer 3)
Layer 3 provides switching and routing technologies, creating logical paths, known as virtual
circuits, for transmitting data from node to node. Routing and forwarding are functions of this
layer, as well as addressing,internetworking, error handling, congestion control and packet
sequencing.
Layer 3 Network examples include AppleTalk DDP, IP, IPX.
Physical (Layer 1)
OSI Model, Layer 1 conveys the bit stream - electrical impulse, light or radio signal —
through the network at the electrical and mechanical level. It provides the hardware means of
sending and receiving data on a carrier, including defining cables, cards and physical
aspects. Fast Ethernet, RS232, and ATM are protocols with physical layer components.
Network monitors traffic flows continuously to ensure they meet their traffic
contract. The process of monitoring and enforcing the traffic flow is called policing.
When a packet violates the contract, network can discard or tag the packed giving it
lower priority. If congestion occurs, tagged packets are discarded first.
01. Bucket has specified leak rate for average contracted rate
02. Bucket has specified depth to accommodate variation in arrival rate
03. Arriving packet is conforming if it does not result in overflow Leaky bucket algorithm
can be used to police arrival rate of a packet
Program:
#include<stdio.h>
#include<unistd.h>
int main()
{
int incoming,outgoing,bucksize, initialval,count,ans,store=0;
printf("enter bucket size, outgoing rate and counter initial size:");
scanf("%d %d %d",&bucksize, &outgoing, &initialval);
count=initialval;
while(count!=0)
{
printf("enter the incoming packet size:");
scanf("%d", &incoming);
printf("incoming packet size %d \n", incoming);
if(incoming<=(bucksize-store))
{
store+=incoming;
printf("bucket contains %d out of %d\n", store,bucksize);
}
else
{
printf("dropped %d no of packets\n", incoming -(bucksize-store));
store=bucksize;
printf("bucket now contains %d out of %d\n", store,bucksize);
}
store=store-outgoing;
sleep(5);
printf("after outgoing %d pkts left in bucket \n",store);
if (count<=incoming)
{
printf("do you want to continue? type 1 to continue");
scanf("%d", &ans);
if(ans!=1)
break;
count=initialval;
}
count=count-incoming;
printf("count=%d\n", count);
}
}
Result:
Experiment 2
Write a Program for Simulation of Sliding Window Protocol.
Theory:
In the Protocol (and the next), the sliding window is an abstract concept that defines the range
of sequence numbers that is the concern of the sender and receiver. In other words, the sender
and receiver need to deal with only part of the possible sequence numbers.
The range which is the concern of the sender is called the send sliding window; the range that
is the concern of the receiver is called the receive sliding window. We discuss both here. The
send window is an imaginary box covering the sequence numbers of the data frames which
can be transit.
In each window position, some of these sequence numbers define the frames that have been
sent;others define those that can be sent. The maximum size of the window is 2m -1 for
reasons that we discuss that we discuss later. In this chapter, we let the size be fixed and set to
the maximum value, but we will see in future chapters that some protocols may have a
variable window size.
Program
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int i=0,j=0,numframes,x,y,count=0;
char a[100];
printf("\n enter no of frames,: \n");
scanf("%d",&numframes);
for (i=0; i<numframes;i++)
{
printf("\n enter data %d: \n", i+1);
scanf("%c%*c",&a[i]);
x=rand()%10;
y=rand()%10;
printf(“\n rondom numbers x and y are %d and %d “, x , y);
if(i==5)
{
printf("\n NAK & resending frame %d \n", i+1);
}
if((y!=x)&&(y>x)&&(count<=1))
{
count++;
sleep(1);
printf("\n Ack %d\n",i+1);
count=0;
}
}
sleep(3);
printf("\n End of sliding window protocol\n");
}
Result:
Experiment 3
Write a Program to obtain CRC Code for the given Data, using CRC-CCITT (CRC 16)
Polynomial. Verify the Program for the Cases.
a. Without Error b. With Error
Theory: The polynomial code (also known as a cyclic redundancy code or CRC code) is
widely used. Polynomial codes are based upon treating bit strings as representations of
polynomials with coefficients of 0 and 1 only. A k-bit frame is regarded as the coefficient list
for a polynomial with k terms, ranging from xk-1 to x0. Such a polynomial is said to be of a
degree k-1. The high-order (left most) bit is the coefficient of xk-1; the next bit is the
coefficient of xk-2, and so on. For example, 110001 has 6 bits and thus represents a six-term
polynomial with coefficients 1,1,0,0,0 and 1 : x5 + x4 + x0.
When the polynomial code method is employed, the sender and receiver must agree upon a
generator polynomial, G(x), in advance. Both the high and low-order bits of the generator
must be 1. To compute the checksum for some frame with m bits, corresponding to the
polynomial M(x), the frame must be longer than the generator polynomial. The idea is to
append a checksum to the end of the frame in such a way that the polynomial represented by
the check summed frame is divisible by G (x). When the receiver gets the check summed
frame, it tries dividing it by G(x). If there is a remainder, there has been a transmission error.
Error Detection:
The receiver divides the message (including the calculated CRC), by the same polynomial the
transmitter used. If the result of this division is zero, then the transmission was successful.
However, if the result is not equal to zero, an error occurred during the transmission.
Assume for the previous example if the received frame is: 111001100110
The received frame is divided by the same generator polynomial as used by the transmitter
i.e., 11001 (x4 + x3 + 1), we get remainder 0000, indicating, “No Transmission Error”. Else
if the remainder is not 0000 indicates “Transmission error”
/* C Program to compute polynomial code checksum */
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define N strlen(g)
char t[100]="0",cs[100]="0",g[]="10001000000100001";
int a=0,e=0,c=0;
void xor()
{
for(c=0;c<N;c++)
cs[c]=((cs[c]==g[c])?'0':'1');
}
void crc()
{
for(e=0;e<N;e++)
cs[e]=t[e];
do
{
if(cs[0]=='1')
xor();
for(c=0;c<N-1;c++)
cs[c]=cs[c+1];
cs[c]=t[e++];
}
while(e<=(a+N-1));
}
int main()
{
clrscr();
printf("enter the data\n");
scanf("%s",&t);
printf("\n generator polynomial:%s",g);
a=strlen(t);
for(e=a;e<a+N-1;e++)
t[e]='0';
printf("\n modified data is %s",t);
crc();
printf("\n check sum is %s",cs);
for(e=a;e<a+N-1;e++)
t[e]=cs[e-a];
printf("\n final code word is %s",t);
printf("\n test error detection 0(yes) 1(no)?");
scanf("%d",&e);
if (e==0)
{
do
{
printf("\n enter the position where error is to be inserted");
scanf("%d",&e);
}
while(e==0||e>a+N-1);
t[e-1]=((t[e-1]=='0')?'1':'0');
printf("\n errourns data:%s\n",t);
}
crc();
for(e=0;(e<N-1)&&(cs[e]!='1');e++);
if(e<N-1)
printf("\n error detected \n");
else
printf("\n no error detected \n");
getch();
return 0;
}
Output:
FEATURES OF NS2
NSG2
It takes a lot of time to write TCL scripts for ns2, NSG2 is a tool for creating TCL scripts
automatically. It is a free software.
NS2 Scenarios Generator 2(NSG2) is a JAVA based ns2 scenarios generator. Since NSG2 is
written by JAVA language, you can run NSG on any platform. NSG2 is capable of generating
both wired and wireless TCL scripts for ns2. Some major functions of NSG2 are listed below:
1. Creating wired and wireless nodes
2. Creating connection between nodes
3. Creating links (Duplex-Link and Simplex-Link)
4. Creating agents (TCP and UDP)
5. Creating applications (CBR and FTP)
6. Node movement
.Analyzing the trace file
Event Meaning
+ Added to queue
- Removed from queue
r Received at node
d Dropped
1. Action s – sent
r – Rxd
d - dropped
2. Time : when the action happened
3. From node : The node where action happened
4. Layer
AGT -> Application layer ( Application)
RTR – Network Layer (Routing)
LL – Link layer ( ARP is done here)
IFQ – Outgoing packet queue(between link & MAC layer)
MAC –
Phy – Physical
5. Flags :
6. Seq no : the sequence number of the packet
7. Type : Packet type
Cbr -> CBR data stream pkt
DSR -> DSR routing pkt
RTS -> RTS pkt generated by MAC 802.11
APR -> Link layer ARP Packet
8. Size : The size of pkt at current layer, when pkt goes dwon, size
[ a b c d] : a -> pkt duration in MAC layer header
b -> The MAC address of destination
c -> The MAC address of source
d -> The MAC type of the pkt body
9. Flags :
Procedure :
01. Create the required network and save the TCL file in your folder
02. Open a terminal and type cd (your folder name)
03. run the tcl file type ns (tcl file name )
04. Type gedit (filename.awk) to create awk file for your experiment
05. Type the awk code and save it
06. run the awk script
07. Note down the packets sent, received and dropped
08. Repeat the experiment by changing the buffer size in the tcl file.
TCL File :
#===================================
# Simulation parameters setup
#===================================
set val(stop) 10.0 ;# time of simulation end
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $n0 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n0 $n2 50
$ns duplex-link $n1 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n1 $n2 50
$ns duplex-link $n3 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n3 $n2 50
$ns duplex-link $n4 $n2 100.0Mb 10ms DropTail
$ns queue-limit $n4 $n2 50 # change the que limit to 1 or 1.5 and resimulate
#===================================
# Applications Definition
#===================================
#Setup a FTP Application over TCP connection
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ns at 1.0 "$ftp0 start"
$ns at 2.0 "$ftp0 stop"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam out.nam &
exit 0
}
$ns at $val(stop) "$ns nam-end-wireless $val(stop)"
$ns at $val(stop) "finish"
$ns at $val(stop) "puts \"done\" ; $ns halt"
$ns run
NAM File :
AWK script:
BEGIN{
recd=0
drop=0
}
{
if($1=="r"&&$4=="4"&&$5=="tcp"&&$6=="1540")
{
recd++;
}
if($1=="d"&&$3=="
3"&&$5=="tcp"&&$6=="1540")
{
drop++;
}
}
END{
printf("\n total number of data packets received at Node 3: %d\n", recd++);
printf("\n total number of packets dropped at Node 3: %d\n", drop++);
}
Results: