You are on page 1of 20

INTRODUCTION

Computer Network

A computer network, or data network, is a digital telecommunications network which


allows nodes to share resources. In computer networks, computing devices exchange data with
each other using connections (data links) between nodes. These data links are established
over cable media such as wires or optic cables, or wireless media such as WiFi.

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.

Here is the graphical representation of these layers:

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.

Data Link (Layer 2)


At OSI Model, Layer 2, data packets are encoded and decoded into bits. It
furnishes transmission protocolknowledge and management and handles errors in the physical
layer, flow control and frame synchronization. The data link layer is divided into two sub
layers: The Media Access Control (MAC) layer and the Logical Link Control (LLC) layer.
The MAC sub layer controls how a computer on the network gains access to the data and
permission to transmit it. The LLC layer controls frame synchronization, flow control and
error checking.
Layer 2 Data Link examples include PPP, FDDI, ATM, IEEE 802.5/ 802.2, IEEE
802.3/802.2, HDLC, Frame Relay.

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.

TCP/IP model v/s OSI Model


Experiment 1

Write a Program for Congestion Control using Leaky Bucket Algorithm


Aim: To simulate Leaky Bucket Algorithm.
Theory:

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.

Leaky bucket algorithm is the most commonly used policing mechanism

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.

Aim: To simulate 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.

The algorithm for computing checksum is as follows:


1. Let r be the degree of G(x). Append r zero bits to the low-order end of the frame, so it
now contains m+ r bits and corresponds to the polynomial xr M(x).
2. Divide the bit string corresponding to G(X) into the bit string corresponding to xr M(x)
using modulo-2 division.
3. Subtract the remainder (which is always r or fewer bits) from the bit string
corresponding to xr 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 x16 + x12 + x5 + 1


Example to calculate CRC:
Data Frame to be transmitted: 11100110
Generator Polynomial: 11001 (x4 + x3 + 1)
Message after appending four zeroes (as degree of polynomial is 4): 111001100000
Transmitted Frame= Message frame+ CRC checksum (Remainder)
= 111001100110.

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:

Enter the message code in binary: 1101


CCITT-CRC code is:
1 1011101000110101101
Enter received code in binary:
1 1011101000110101101
Received code is error free: 1101

Enter the message code in binary: 1101


CCITT-CRC code is:
1 1011101000110101101
Enter received code in binary:
1 1011101000110101100
Received code is erroneous.
NETWORK SIMULATOR NS2

NS (Network Simulator) is a name for a series of discrete event network simulators,


specifically NS-1, NS-2, NS-3 and NS-4. All are discrete-event computer network simulators,
primarily used in research and teaching.

NS2 stands for Network Simulator Version 2. It is an open-source event-driven simulator


designed specifically for research in computer communication networks.

FEATURES OF NS2

1. It is a discrete event simulator for networking research.


2. It provides substantial support to simulate bunch of protocols like TCP, FTP, UDP, https
and DSR.
3. It simulates wired and wireless network.
4. It is primarily UNIX based.
5. Uses TCL as its scripting language.
6. OTCL : Object oriented support
7. TCLCL : C++ and otcl linkage
8. Discrete event scheduler

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

•Trace file format for wired scenario


Event Time Where To Pkt Pkt Flags Flow Src Dest Seq Pkt id
from node type size id addr addr no
node

Trace file Event

Event Meaning
+ Added to queue
- Removed from queue
r Received at node
d Dropped

•Trace file format for wireless scenario

Action Time Where Layer Flags Seq Type Size flags


from no
node

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 :

Source node Port no of Port no ip ip of next hop


I/P Dest node i/p Header ttl (0 means node 0 or
(-1 Means broadcast) broadcast)
Experiment 4
Configure a simple network (Bus/star) topology using simulation software

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]

#Open the NS trace file


set tracefile [open out.tr w]
$ns trace-all $tracefile

#Open the NAM trace file


set namfile [open out.nam w]
$ns namtrace-all $namfile
#===================================
# Nodes Definition
#===================================
#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

#===================================
# 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

#Give node position (for NAM)


$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient left-down
$ns duplex-link-op $n3 $n2 orient right-up
$ns duplex-link-op $n4 $n2 orient left-up
#===================================
# Agents Definition
#===================================
#Setup a TCP connection
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set sink4 [new Agent/TCPSink]
$ns attach-agent $n4 $sink4
$ns connect $tcp0 $sink4
$tcp0 set packetSize_ 1500

#Setup a TCP connection


set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set sink3 [new Agent/TCPSink]
$ns attach-agent $n4 $sink3
$ns connect $tcp1 $sink3
$tcp1 set packetSize_ 1500

#Setup a TCP connection


set tcp2 [new Agent/TCP]
$ns attach-agent $n3 $tcp2
set sink5 [new Agent/TCPSink]
$ns attach-agent $n4 $sink5
$ns connect $tcp2 $sink5
$tcp2 set packetSize_ 1500

#===================================
# 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"

#Setup a FTP Application over TCP connection


set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
$ns at 1.0 "$ftp1 start"
$ns at 2.0 "$ftp1 stop"

#Setup a FTP Application over TCP connection


set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ns at 1.0 "$ftp2 start"
$ns at 2.0 "$ftp2 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:

Buffer Packets Packets


Size Received dropped
50
5
2

You might also like