Upload
ashwin-gowda
View
229
Download
0
Embed Size (px)
Citation preview
8/2/2019 Network Lab Manual(a & B)
1/62
Computer Networks Lab Manual
COMPUTER NETWORKS
LAB MANUAL
For
VII SEMESTER(ISE)
by:
Sreenath M,
Sr. Lecturer, Dept. of ISE,
Note: Student is required to solve one problem from PART-A and one problem from
PART-B. The questions are allotted based on lots. Both questions carry equal marks.
PART - A
SIMULATION EXERCISESThe following experiments shall be conducted using either NS / NCTUns or any other
suitable simulator.
Department Of Information Science and Engineering Page 1
8/2/2019 Network Lab Manual(a & B)
2/62
Computer Networks Lab Manual
1. Simulate a three nodes point to point network with duplex links
between them. Set the queue size and vary the bandwidth and find the
number of packets dropped.
2. Simulate a four node point-to-point network with the links connected asfollows:n0 n2, n1 n2 and n2 n3. Apply TCP agent between n0-n3
and UDP between n1-n3. Apply relevant applications over TCP and UDP
agents changing the parameter and determine the number of packets
sent by TCP / UDP.
3. Simulate the different types of Internet traffic such as FTP and TELNET
over a network and analyze the throughput.
4. Simulate the transmission of ping messages over a network topologyconsisting of 6 nodes and find the number of packets dropped due to
congestion.
5. Simulate an Ethernet LAN using n nodes (6-10), change error rate and
data rate and compare throughput.
6. Simulate an Ethernet LAN using n nodes and set multiple traffic nodes
and determine collision across different nodes.
7. Simulate an Ethernet LAN using n nodes and set multiple traffic nodes
and plot congestion window for different source / destination.
8. Simulate simple ESS and with transmitting nodes in wire-less LAN by
simulation and determine the performance with respect to transmission
of packets.
PART - B
IMPLEMENT THE FOLLOWING BY C/C++
1. Write a program for error detecting code using CRC-CCITT (16- bits).
2. Write a program for frame sorting technique used in buffers.
Department Of Information Science and Engineering Page 2
8/2/2019 Network Lab Manual(a & B)
3/62
Computer Networks Lab Manual
3. Write a program for distance vector algorithm to find suitable path for
transmission.
4. Using TCP/IP sockets, write a client server program to make the client send
the file name and to make the server send back the contents of the requestedfile if present.
5. Implement the above program using as message queues or FIFOs as IPC
channels.
6. Write a program for simple RSA algorithm to encrypt and decrypt the data.
7. Write a program for Hamming code generation for error detection and
correction.
8. Write a program for congestion control using leaky bucket algorithm.
Department Of Information Science and Engineering Page 3
8/2/2019 Network Lab Manual(a & B)
4/62
Computer Networks Lab Manual
PART A Programs
The following experiments shall be conducted using NS2 or NCTUns-6.0 or other suitablesimulator
Steps to execute Part-A:
1.After grub loading select NCTUns kernel
2.others3.username:root
Password:root123
STEP1-open terminal1
[root@lab3~]#cd /usr/local/nctuns/bin
[root@lab3~bin]# ./dispatcher
STEP2-open terminal2
[root@lab3~]#cd /usr/local/nctuns/bin[root@lab3~bin]# ./coordinator
STEP3-open terminal3[root@lab3~]#cd /usr/local/nctuns/bin
[root@lab3~bin]# ./nctunsclient
Department Of Information Science and Engineering Page 4
8/2/2019 Network Lab Manual(a & B)
5/62
Computer Networks Lab Manual
1. Simulate a three-node point-to-point network with a duplex link between them.
Set the queue size and vary the bandwidth and find the number of packets
dropped.
Procedure:
1. Draw the n/w topology as specified in the draw topology mode of the simulatorDraw toplogy mode-
Select the hub icon and drag it on the working window
Select the host icon on the toolbar and drag it on the working window
Repeat for another host
Select the link icon from the toolbar & make a connection b/w host1 and hub, host2 and
hub. Here the hub acts as node 3 in the point to point n/w topology
2. Go to edit mode and save the topology as a .tpl file
3. Double click on host1,click on the node editor & select MAC layer and then select half
duplex for hubs & in log statistics , select no of drop packets, no of collisions, &
throuput of outgoing packets & press ok. Select FIFO , set the queue size to 50 & pressok . click on command box & type the following command Shown after diagram
4. Double click on the link b/w host1 and hub to set the b/w to some initial value ,say10mbps
5. Save the changes made to the topology .now click on the R button to run the simulation
6. Now go to menu -> simulation -> run . execution
7. Play8. Go to g-settings , plot the graph -> fileopen ->.results
Topology:-
Sender:-
stg u 1024 100 1.0.1.2
Receiver:-
rtg u w log1
Parameters:-
Drop Packets and Collision Packets
2. Simulate a four-node point-to-point network and connect the link as follows: Apply
a TCP agent between n0 to n3 and apply a UDP agent between n1 and n3. Apply
Department Of Information Science and Engineering Page 5
8/2/2019 Network Lab Manual(a & B)
6/62
Computer Networks Lab Manual
relevant applications over TCP and UDP agents changing the parameters and
determine the number of packets sent by two agents.
Procedure:
1. Create the toplogy of three nodes connect these nodes to a hub
2. Goto edit mode & save the topology3. Setup the tcp connection b/w n0-n3 and udp connection b/w n1-n3 as fallows using
the commands
TCP & UDP
Sender-Stcp p 7000 l 1024 1.0.1.3
Stg u 7000 l 100 1.0.1.3
Receiver-rtcp p 7000 l 1024
rtg u w log1
4. Set the i/p throuput for the receiver & the o/p throuput for the sender in the log file
statistics5. Back to edit mode
6. Run the simulation7. Play
8. For results of graph, -> g-settings
->.plot graph
->select parameters9. make a comparisons
Topology:-
Sender:-
Stcp p 7000 l 1024 1.0.1.3
Department Of Information Science and Engineering Page 6
8/2/2019 Network Lab Manual(a & B)
7/62
Computer Networks Lab Manual
Stg u 7000 l 100 1.0.1.3
Receiver:-
rtcp p 7000 l 1024
rtg u w log1
Parameters:-
Throughput of incoming and outgoing Packets
Department Of Information Science and Engineering Page 7
8/2/2019 Network Lab Manual(a & B)
8/62
Computer Networks Lab Manual
3. Simulate the different types of Internet traffic such as FTP, TELNET over a
network and analyze the throughput.
Procedure:
Steps to FTP connection
1. Create the topology of three nodesConnect these nodes to a hub
2. Go to edit mode and save the toplogy
3. Setup FTP connection b/w 1 and 3 using the following commands
Stcp p 21 l 1024 1.0.1.34. Set the i/p and o/p throughput logfile as described in the previous experiments
5. To view results, go to filename results folder
To setup TELNET connection
1. Create the topology of three nodes; connect these nodes to the hub
2. Go to edit mode and save the topology
3. Setup TELNET connection b/w node2 and node3Using the following command
Stcp p 23 l 1024 1.0.1.3Rtcp p 23 l 1024
4. Set the input and output throughput log file as described in the previous
experiments
5. To view the results, go filename results folder
Topology:-
Sender:-For FTP
stcp p 21 l 1024 1.0.1.3
For Telnetstcp p 23 l 1024 1.0.1.3
Department Of Information Science and Engineering Page 8
8/2/2019 Network Lab Manual(a & B)
9/62
Computer Networks Lab Manual
Receiver:-
For FTP
rtcp p 21 l 1024For Telnet
rtcp p 23 l 1024
Parameters:-
Throughput of incoming and outgoing Packets
Department Of Information Science and Engineering Page 9
8/2/2019 Network Lab Manual(a & B)
10/62
Computer Networks Lab Manual
4. Simulate the transmission of ping messages over a network topology consisting
of 6 nodes and find the number of packets dropped due to congestion.
Procedure:
1.click on the hub icon on the toolbar and drag it on the working window
2. select the 6 nodes and a suitable radius b/w the host and the hub3. a single host is made to act as a receiver and other as sender
4.run the simulation
5.in the run mode from sending node the receiving node is pinged using ping
1.0.1.6Note:the no of dropped packets are obtained only when the traffic is more is the
n/w
Command console:go to toolsSimulation time and change
Simulation time to 100
During run mode, doulble click host2 and then click command console and execute
Ping 1.0.1.6
Topology:-
Department Of Information Science and Engineering Page 10
8/2/2019 Network Lab Manual(a & B)
11/62
Computer Networks Lab Manual
Sender:-
stcp p 2000 l 1024 1.0.1.4
Receiver:-rtcp p 2000 l 1024
Command Console:-
Goto tools-> simulation time and change Simulation time to 100. During run mode,
double click host 2 and then click command console. And execute the following
command.
ping 1.0.1.4
Parameters:-
Drop Packets and Collision Packets.
Department Of Information Science and Engineering Page 11
8/2/2019 Network Lab Manual(a & B)
12/62
Computer Networks Lab Manual
5. Simulate an Ethernet LAN using N nodes (6-10), change error rate and data
rate and compare throughput.
Procedure:
1. Connect one set of host with a hub and another set of host also through a huband connect these two hubs with a switch , this forms Ethernet LAN
2. To setup a tcp connection b/w a host or one hub & host or another hub using
following commands
Stcp p portno l buffer size IP addressStcp p 2000 l 1024 1.0.1.4
rtcp p 2000 l 1024
3. Setup the error route , data route in the physical layer, i/p and o/p throughput inthe MAC layer
4. Change error route and data route and compare throughput
5. View the results in file name results
Note:double click on reciver link and change BER to 0.000001, run again
Topology:-
Sender:-
stcp p 2000 l 1024 1.0.1.4
Receiver:-
rtcp p 2000 l 1024
Double click on receiver link and change BER to 0.000001, Run Again.
Parameters:-Throughput of outgoing Packets
Department Of Information Science and Engineering Page 12
8/2/2019 Network Lab Manual(a & B)
13/62
Computer Networks Lab Manual
6. Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and
determine collisions across different nodes.
Procedure:
1. Connect one set of host with a hub and another set of host also through a hub and
connect these two hubs through a switch, these forms Ethernet LAN2. Setup multiple traffic connection b/w the hosts on one hub & hosts on another
hub using the following command
Stcp p 2000 l 1024 1.0.1.4
Rtcp p 2000 l 10243. Setup the collision log at the destination host in the MAC layer
4. Give the results in filename .results
Topology:-
Sender:-stcp p 2000 l 1024 1.0.1.4
Receiver:-
rtcp p 2000 l 1024
Parameters:-
Collision Packets and Drop Packets (Optional)
Department Of Information Science and Engineering Page 13
8/2/2019 Network Lab Manual(a & B)
14/62
Computer Networks Lab Manual
7. Simulate an Ethernet LAN using N nodes and set multiple traffic nodes and
plot congestion window for different source/destination.
Procedure:1. Connect one set of host with a hub and another set of hosts also through a hub and
connect these two hubs through a switch this forms an Ethernet LAN
2. Setup multiple traffic connection b/w the hosts on one hub and host on another hubusing the following
Stcp p 2000 l 1024 1.0.1.4
Rtcp p 2000 l 1024
3. Setup the collision log at the destination host in the MAC layer4. Plot the congestion window , go to menu->tools->plotgraph->file->open filename
results-> filename .coll.log
5. Give the results in filename .results
Topology:-
Sender:-
stcp p 2000 l 1024 1.0.1.4
Receiver:-
rtcp p 2000 l 1024
Parameters:-
Receiver side Collision Packets and Drop Packets
Department Of Information Science and Engineering Page 14
8/2/2019 Network Lab Manual(a & B)
15/62
Computer Networks Lab Manual
8. Simulate simple BSS and with transmitting nodes in wireless LAN by
simulation and determine the performance with respect to transmission of
packets.
Procedure:
1. Draw the topology consisting of a : route b:host c:WLAN d:WLAN accesspoint (two)
2. Go to tools menu, select the option wireless LAN -> mobile nodes
3. Generate infrastructure mobile nodes IP
4. A dialog box appear, entries to be done areSubnet ID 8 gateway IP
Subnetid=2 & gateway Ip= 1.0.1.0
5. Here mobile nodes are the senders & the host is the receiver6. For each mobile node, add a command
ttcp t -u s p 7000
ttcp t u s p 7001 for another node
7. For the receiver, add the following commandTtcp r u s p 7000
8. Run the simulation9. To see the results of the graph , go to g-settings ->plot graph ->file open-
>results, select the parameters required
Topology:-
Click on access point. Goto wireless interface and tick on show transmission range
and then click OK.
Double click on Router -> Node Editor and then
Left stack -> throughput of Incoming packets
Right stack -> throughput of Outgoing packets
Select mobile hosts and access points then click on.
Tools -> WLAN mobile nodes-> WLAN Generate infrastructure.
Department Of Information Science and Engineering Page 15
8/2/2019 Network Lab Manual(a & B)
16/62
Computer Networks Lab Manual
Subnet ID: Port number of router (2)
Gateway ID: IP address of router
Mobile Host 1
ttcp t u s p 3000 1.0.1.1
Mobile Host 1
ttcp t u s p 3001 1.0.1.1
Host(Receiver)ttcp r u s p 3000
ttcp r u s p 3001
Run and then play to plot the graph.
Department Of Information Science and Engineering Page 16
8/2/2019 Network Lab Manual(a & B)
17/62
Computer Networks Lab Manual
PART-BError detecting code using CRC-CCITT
Overview:The accurate implementations (long-hand and programmed) of the 16-bit
CRC-CCITT specification, is as follows:
Width = 16 bits
Truncated polynomial = 0x1021
Initial value = 0xFFFF
Input data is NOT reflected
Output CRC is NOT reflected
No XOR is performed on the output CRC
Theoretical Concepts:Important features of a standard CRC are that it:
Can be used to validate data
Is reproducible by others
The first feature above is easy to realize in a closed system if corruption
of data is infrequent (but substantial when it occurs). The term "closed
system" refers to a situation where the CRC need not be communicated
to others. A correct implementation of a 16-bit CRC will detect a
change in a single bit in a message of over 8000 bytes. An erroneous
CRC implementation may not be able to detect such subtle errors. Iferrors are usually both rare and large (affecting several bits), then a
faulty 16-bit CRC implementation may still be adequate in a closed
system. The second feature above -- that the CRC is reproducible by
others -- is crucial in an open system; that is, when the CRC must be
communicated to others. If the integrity of data passed between two
applications is to be verified using a CRC defined by a particular
standard, then the implementation of that standard must produce the
same result in both applications -- otherwise, valid data will be reported
as corrupt.
Reproducibility may be satisfied by even a botched implementation of a
standard CRC in most cases -- if everyone uses the same erroneous
implementation of the standard. But this approach:
Modifies the standard in ways that are both unofficial and
undocumented.
Department Of Information Science and Engineering Page 17
8/2/2019 Network Lab Manual(a & B)
18/62
Computer Networks Lab Manual
Creates confusion when communicating with others who have not
adopted the botched implementation as the implied standard.
The CRC value for the 9-byte reference string, "123456789" is 0xE5CC.
The need to focus on the 16-bit CRC-CCITT (polynomial 0x1021) and notCRC16 (polynomial 0x8005),are as follows :
Is a straightforward 16-bit CRC implementation in that it doesn't
involve:
reflection of data
reflection of the final CRC value
Starts with a non-zero initial value -- leading zero bits can't affect the
CRC16 used by LHA, ARC, etc., because its initial value is zero.
It requires no additional XOR operation after everything else is done.
The CRC32 implementation used by Ethernet, Pkzip, etc., requires
this operation; less common 16-bit CRCs may require it as well.
The need to use a 16-bit CRC instead of a 32-bit CRC is as follows :
Can be calculated faster than a 32-bit CRC.
Requires less space than a 32-bit CRC for storage, display or printing.
Is usually long enough if the data being safeguarded is fewer than
several thousand bytes in length, e.g., individual records in a database.
Example:-
Calculation of the 16-bit CRC-CCITT for a one-byte
message consisting of the letter "A":
Quotient= 111100001110111101011001
poly=
------------------------------------------
10001000000100001)
1111111111111111010000010000000000000000
10001000000100001
-------------------------------------------------------
---
red bits are initial value
11101111110111111
bold bits are message
10001000000100001
blue bits are augmentation
Department Of Information Science and Engineering Page 18
8/2/2019 Network Lab Manual(a & B)
19/62
Computer Networks Lab Manual
-------------------------------------------------------
----
11001111100111100
10001000000100001
-------------------------
-----
10001111000111010
10001000000100001
---------------------------------------
----
00001110000110110
00000000000000000
---------------------------
-----
00011100001101100
00000000000000000
---------------------------
-----
00111000011011000
00000000000000000
----------------------------
-----
01110000110110001 00000000000000000
----------------------------
-----
11100001101100010
10001000000100001
----------------------------
-----
11010011010000110
10001000000100001----------------------------
----
10110110101001110
Department Of Information Science and Engineering Page 19
8/2/2019 Network Lab Manual(a & B)
20/62
Computer Networks Lab Manual
Program 1:
Write a program for error detecting code using CRC-CCITT (16-bits).
#include
#include
char t[128],cs[128],g[ ]="10001000000100001";
#define N strlen(g)
int a,e,c;
void xor()
{
for(c=1;c
8/2/2019 Network Lab Manual(a & B)
21/62
Computer Networks Lab Manual
for(e=a;e
8/2/2019 Network Lab Manual(a & B)
22/62
Computer Networks Lab Manual
FRAME SORTING TECHNIQUE USED IN BUFFERSOverview & Theoretical Concepts:
We need the frame sorter when the frame source is using TCP/IP so we can
run packet re-assemblers on other computers in an effort to balance the
workload.
If the frames are coming in over ISIS IP multicasts, we don't need a sorter:
Each re-assembler can receive all frames and discard those they don't need.
This way we get each frame on the network one time only, and the work in
each re-assembler to input and reject frames on the wrong virtual channel is
very little. If ISIS is not using true IP multicasting, then, once again, the
sorter is needed to reduce the network traffic.
There are side benefits to such a design. In cases like I&T where the frame
source is using TCP/IP, we need to sort the transfer frames by virtual
channel so we can run some packet re-assemblers on other workstations to
balance the compute load. But even so, we may re-assemble housekeeping
packets on the workstation doing the frame sorting. Why do it in a separate
process? This is especially true on Solaris where we have control over thread
binding, and therefore, thread concurrency.
In a multi-threaded environment, the frame sorter and packet re-assembler
both can be constructed from a collection of objects: input tasks, outputtasks, packet assembly tasks, pool objects, sorter objects, and queue objects.
The packet re-assembler might looks like this:
input --> queue --> pkt assy --> sorter --> queue --> output
Transfer frames enter an input. Input enqueues and possibly archives them.
The packet assembly task reads frames out of the queue and constructs
packets. It hands completed packets to a sorter which enqueues them for
output tasks and possibly archives them. Each output task reads packets
fromits queue and sends packets out.
Program 2:
Write a program for frame sorting technique used in the buffers.
#include
struct frame{
int fslno;
Department Of Information Science and Engineering Page 22
8/2/2019 Network Lab Manual(a & B)
23/62
Computer Networks Lab Manual
char finfo[20];
};
struct frame arr[10];
int n;
void sort()
{
int i,j,ex;
struct frame temp;
for(i=0;i
8/2/2019 Network Lab Manual(a & B)
24/62
Computer Networks Lab Manual
printf("-----------------------------------------------------------\n");
}
return 0;
}
Output :
Enter the number of frames: 3
Enter the frame sequence and frame content:
5 ink
3 pot
8 pen
The frame in sequence
3 pot
---------------------------------5 ink
---------------------------------
8 pen
---------------------------------
DISTANCE VECTOR ALGORITHM
Background: One of the most popular & dynamic routing algorithms, the
distance vector routing algorithms operate by using the concept of each
router having to maintain a table( ie., a vector ), that lets the router know the
best or shortest distance to each destination and the next hop to get to there.
Also know as Bellman Ford(1957) and Ford Fulkerson (1962). It was the
original ARPANET algorithm.
Algorithm Overview:Each router maintains a table containing entries for
and indexed by each other router in the subnet. Table contains two parts :
1.A preferred outing line.
2.Estimate of time or distance to that destination.
The metric used here is the transmission delay to each destination. Thismetric maybe number of hops, queue length, etc.
The router is assumed to know the distance metric to each of its
neighbours.Across the network the delay is calculated by sending echo
packets to each of neighbours.
Example:
Department Of Information Science and Engineering Page 24
x y z
x
y
z
0 2 7
from
cost to
from
from
x y z
x
y
z
0 2 3
from
cost tox y z
x
y
z
0 2 3
from
cost to
x y zx
y
z
cost to
x y zx
y
z
0 2 7
from
cost to
x y z
x
y
z
0 2 3
from
cost to
x y z
x
y
z
0 2 3
from
cost to
x y z
x
y
z
0 2 7
from
cost to
x y z
x
y
z 7 1 0
cost to
2 0 1
2 0 1
7 1 0
2 0 1
7 1 0
2 0 1
3 1 0
2 0 1
3 1 0
2 0 1
3 1 0
2 0 1
3 1 0
x z
12
7
y
Dx(y) = min{c(x,y) + D
y(y), c(x,z) + D
z(y)}
= min{2+0 , 7+1} = 2
8/2/2019 Network Lab Manual(a & B)
25/62
Computer Networks Lab Manual
Program 3:
Write a program for distance vector algorithm to find suitable path for
transmission.
#include
#include
int p,a[10][10];
int i,j;
void path(int n)
{
printf("If no path exist,enter a value > 999 \n");
printf("enter the source node:");
scanf("%d",&p);
for(i=1;i
8/2/2019 Network Lab Manual(a & B)
26/62
Computer Networks Lab Manual
{
int sma=9999,pos;
for(i=1;ia[1][i])
{sma==a[1][i];
pos=i;
}
for(j=i;j1)
{
int sma=minv(a,s);
s=s-1;
for(i=p;ia[p][sma]+a[sma][i])a[p][i]=a[p][sma]+a[sma][i];
}
}
}
void disp(int n)
{
for(i=1;i
8/2/2019 Network Lab Manual(a & B)
27/62
Computer Networks Lab Manual
scanf("%d",&n);
path(n);
fsp(n);
disp(n);
}
Output:
Enter the number of node:3
If no path exists,enter a value >999
Enter the source node: 1
Enter the value of 1:2:::path
200
Enter the value of 1:3:::path255
Enter the value of 2:1:::path
256
Enter the value of 2:3:::path
643
Enter the value of 3:1:::path
235
Enter the value of 3:2:::path
2345The path from 1 to 1:0
The path from 1 to 2:200
The path from 1 to 3:255
CLIENT SERVER PROGRAM USING SOCKET PROGRAMMING
OVERVIEW:Unix sockets is just like two way FIFO's. All data
communication will take place through the socket's interface, instead ofthrough the file interface. Although unix socket's are a special file in the file
system(just like FIFO's), there's usage of socket(), bind(), recv(),etc and not
open(), read().
When programming with socket's, usually there's creation of server and
client programs. The server will sit listening for incoming connections from
Department Of Information Science and Engineering Page 27
8/2/2019 Network Lab Manual(a & B)
28/62
Computer Networks Lab Manual
clients and handling. This is similar to the situation that exists with internet
sockets but with fine differences.
For instance, when describing which unix socket that has to be used(i.e the
path to the special file that is the socket). The structure struct sockaddr_un
has the following fields:struct sockaddr_un{
unsigned short sa_family; // Address family,AF_XXXX
char sa_data; // 14 bytes of protocol address
};
This is the structure you will be passing to the bind() function, which
associates a socket descriptor(a file descriptor) with a certain file(the name
for which is in the sun_path field).
The structure struct sockaddr_in is used when we need IP address and Port
number to be binded to the Sockets. It has following fields:
struct sockaddr_in {
short int sin_family; // Address family
unsigned short int sin_port; // Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8] // Same size as struct
sockaddr
};
// Internet adress
struct in_addr {
unsigned long s_addr; // 32 bits or 4 bytes long};
BACKGROUND REQUIRED:
1. UNIX File I/O system calls
2. UNIX IPC system calls
3. UNIX socket programming
THEORETICAL CONCEPTS:
Most interprocess communication uses the client server model. These terms
refer to the two processes which will be communication with each other.
One of the two processes , the client , connects to the other process, the
server, typiceally to make a request for information. A good analogy is a
person who makes a phone call to another person.
Notice that the client needs to know of the existence of and the address of
the server, but the server does not need to know the adresss of(or even the
existence of) the client prior to the connection being established. Notice also
Department Of Information Science and Engineering Page 28
8/2/2019 Network Lab Manual(a & B)
29/62
Computer Networks Lab Manual
that once a connection is established, both sides can send and receive
information.
The system calls for establishing a connection are somewhat different for the
client and the server, but both involve the basic construct of a socket. A
socket is one end of an interprocess communication channel. The twoprocesses each establish their own socket.
The steps involved in establishing a socket on the client side are as follows-
1. Create a socket with the socket() system call .
2. Connect the socket to the address of the server using the connect()
system call.
3. Send and receive data.There are a number of ways to do this, but the
simplest is to use the read() and write() systen calls.
The stepd involved in establishing a socket on the server side are as
follows-
1. Create a socket with the socket() system call.
2. Bind the socket to an address using the bind() system call. For a
server socket on the internet,an address consists of a port number on
the host machine.
3. Listen for connections with the listen() system call.
4. Acept a connection with the accept() system call. This call typically
blocks until a client connects with the server.
5. Send and receive the data.
Socket Types:When a socket is created, the program has to specify the address domain and
the socket type. Two processes can communicate with each other only if
their sockets are of the same type and in the same domain, in which two
processes running on any two hosts on the Internet communicate. Each of
these has it's own adress format.
The address of a socket in the Unix domain is a character string which is
basically an entry in the file system.
The address of a socket in the Internet domain consists of the Internet
address of the host machine (every computer on the Internet has a unique 32
bit address, often reffered to as it's IP address). In addition , each socket
needs a port number on that host. Port numbers are 16 bit unsigned integers.
The lower numbers are reserved in Unix for standard services. For eg, the
port number for the FTP server is 21.There are two widely used socket
types, stream sockets , and datgram sockets. Stream sockets treat
communications as a continuous stream of characters, while datagram
Department Of Information Science and Engineering Page 29
8/2/2019 Network Lab Manual(a & B)
30/62
Computer Networks Lab Manual
sockets have to read entire messages at once. Each uses it's own
communications protocol. Stream sockets use TCP , which is a reliable ,
stream oriented protocol, and datagram sockets use UDP, which is unreliable
and message oriented.
The primary socket calls are as follows:-
1. socket() - Create a new socket and return it's descriptor.
2. bind() - Associate a socket with a port and address .
3. Listen() -Establish a queue for connection requests.
4. Accept()- Accepts a connection request.
5. Connect()- Initiate a connection to a remote host.
6. Recv() - Receive data from a socket descriptor.
7. Send() - Send data to a socket descriptor.
8. Close() - one-way close of a socket descriptor,
The other system calls used are as follows:-
1. gethostbyname- given a hostname , returns a structure which specifies
it's DNS name(s) and IP address(es).
2. getservbyname given service name and protocol , returns a structure
which specifies its name(s) and its port address.
The socket utility functions are as follows:-
1. htons/ntohl- convert short/long from host byte order to network byte
order.
2. inet_ntoa/inet_addr- converts 32 bit IP address (network byte orderto/from a dotted decimal string)
The header files used in order are:-
1. -prerequisite typedefs.
2. -names for erno values (error numbers)
3. - struct sock addr ;system prototypes and constants .
4. - network info lookup prototypes and structures.
5. - struct sockaddr_in; byte ordering macros.
6. - utility function prototypes.
Program 4
Using TCP/IP sockets, write a client-server program to make client
sending the file name and the server to send back the contents of the
requested file if present.
Department Of Information Science and Engineering Page 30
8/2/2019 Network Lab Manual(a & B)
31/62
Computer Networks Lab Manual
Client program:
#include
#include
#include
#include#include
#include
#include
int main(int argc,char *argv[])
{
int sockfd,portno,n;
char buffer[256],c[20000];
struct sockaddr_in serv_addr;
if(argc
8/2/2019 Network Lab Manual(a & B)
32/62
Computer Networks Lab Manual
}
Server program:
#include#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,clilen,n;
char buffer[256],c[2000],cc[20000];struct sockaddr_in serv_addr,cli_addr;
FILE *fd;
if(argc
8/2/2019 Network Lab Manual(a & B)
33/62
Computer Networks Lab Manual
strcat(cc,c);
write(newsockfd,cc,strlen(cc));
}else{
printf("server:file not found\n");
n=write(newsockfd,"file not found\n",15);}
return 0;
}
Output:
cc server.c
./a.out 8888
Server waiting for client:
(in another terminal)
cc client.c
./a.out 8888
Enter the path:
(in that folder jst create a new document and type some text inside the
document)
Take the path of that folder and type in the client terminal.
Enter the path : /nwlab/ns/ns 2.33/a
Client : displaying contents of /nwlab/ns/ns 2.33/a
EXECUTION INSTRUCTIONS:
There are two programs(Server-side & Client-side) to be typed on an editor
(eg vi or kdevelop), exit the editor after saving the progarm, complile the
programs on different terminals using the command cc .c
-o and then the command ./ is
Department Of Information Science and Engineering Page 33
8/2/2019 Network Lab Manual(a & B)
34/62
Computer Networks Lab Manual
entered if no errors are encountered(else correct the errors and then perform
the above).
1. Compile and execute the Server side program specifying an unsed port
no. after the ouput filename, then in the other terminal compile and
execute the Client sideprogram with IP address of Server system andcommon port no.
2. Enter the pathname of any file on the Server system as a request to the
Server and wait for reply.
3. Now switch to Server terminal and check if the file requested is present or
not and the status of Server. If the file is found then switch to the Client
terminal and watch the contents of file displayed on the terminal. Else it
displays that file is not found with pathname.
MESSAGE QUEUES / FIFOs AS IPC CHANNELS
Client Server communication using FIFO as IPC
OVERVIEW:
FIFO's is used to send data between a Clientand Server. If we have a server
that is contacted by numerous clients, each client can write its request to a
well-known(i.e, the pathname is known by all clients) FIFO that the server
creates. The figure below shows this arrangement. Since there are multiple
writers for the FIFO, the requests sent by the clients to the server need to be
Department Of Information Science and Engineering Page 34
8/2/2019 Network Lab Manual(a & B)
35/62
Computer Networks Lab Manual
less than PIPE_BUF bytes in size. This prevents any interleaving of the
client writes.
The problem in using FIFO's for this type of client-server communication is
how to send replies back from the server to each client. A single FIFO can't
be used, as the clients would never know when to read their response, versusresponses for other clients. One solution is for each client to send its process
ID with the request. The server then creates a unique FIFO for each client,
using a pathname based on the client's process ID.
BACKGROUND REQUIRED:
1. UNIX File I/O system calls
2. UNIX IPC system calls
ALGORITHM: There are two programs- Server side & Client side.Server side:
1. Create a well-known FIFO using mknod command.
Ex: mknod(, S_IFIFO | 0666 , 0)
2. Open the above FIFO in readonly mode to accept requests from
the clients.
3. When the client opens the other end of FIFO in writeonly mode
then read the contents and store the request(pathname of file) in
a buffer.
4. Now create another FIFO(client-specific) in writeonly mode to
send the reply(contents of the file requested)
5. Open the file requested by client and write the contents into the
client-speicfic FIFO and terminate the connection.
Client side:
1. Open the well-known Server FIFO in write mode.
Department Of Information Science and Engineering Page 35
Well-known
FIFO
Client Client
Server
Client-specific
FIFOClient-specific
FIFO
8/2/2019 Network Lab Manual(a & B)
36/62
Computer Networks Lab Manual
2. Write the pathname of file into this FIFO and send as a request.
3. Now open the Client-specific FIFO in read mode and wait for
reply from server.
4. When the contents of file are available on this FIFO display it
on the terminal.
Program 5
Implement the above program using as message queues of FIFOs as
IPC channel.
server program:
#include
#include
#include
#include#include
#include
#include
#include
#define FIFO1_NAME "fifo1"
#define FIFO2_NAME "fifo2"
int main()
{
char p[100],f[100],c[3000];
int num,num2,f1,fd,fd2,in;
mknod(FIFO1_NAME,S_IFIFO | 0666,0);
mknod(FIFO2_NAME,S_IFIFO | 0666,0);
printf("SERVER ONLINE\n");
Department Of Information Science and Engineering Page 36
8/2/2019 Network Lab Manual(a & B)
37/62
Computer Networks Lab Manual
fd = open(FIFO1_NAME,O_RDONLY);
printf("Client online \n waiting for request \n");
while(1){
if((num = read(fd, p, 100))==-1)
perror("Read Error\n");else{
p[num] ='\0';
if((f1=open(p,O_RDONLY))
8/2/2019 Network Lab Manual(a & B)
38/62
Computer Networks Lab Manual
#define FIFO1_NAME "fifo1"
#define FIFO2_NAME "fifo2"
int main()
{char p[1000],f[1000],c[3000];
int num,num2,f1,fd,fd2;
mknod(FIFO1_NAME,S_IFIFO | 0666,0);
mknod(FIFO2_NAME,S_IFIFO | 0666,0);
printf("Waiting for server\n");
fd=open(FIFO1_NAME,O_WRONLY);
printf("server online\n Client:Enter the Path\n");
while(gets(p)!=feof(stdin)){
if((num= write(fd,p,strlen(p)))==-1)
perror("write Error\n");
else
{
printf("waiting for reply\n");
fd2=open(FIFO2_NAME,O_RDONLY);
if((num2=read(fd2,c,300))==-1)
perror("Transfer error\n");
else{
printf("File Recevied \n displaying the contents \n");
if(fputs(c,stdout)==EOF)perror("Print error\n");
exit(1);
}
}
}
}
Output:
clientterminal Server terminal
Department Of Information Science and Engineering Page 38
8/2/2019 Network Lab Manual(a & B)
39/62
Computer Networks Lab Manual
cc client.c
./a.out
Waiting for server
Server online
Client:enter the path:/ns/nwlab/ns2.33/file
Waiting for reply
File received
Displaying the contents:
Bangalore college of engineering
cc server.c
./a.out
Server online
Client online, waiting for the request
Waiting for requestServer /ns/nwlab/ns2.33/file found
Transferring the contents
Server:transfer completed size=33
EXECUTION INSTRUCTIONS:
There are two programs(Server-side & Client-side) to be typed on an editor,
exit the editor after saving the program, compile the programs on different
terminals using the command cc .c and then the
command ./a.out is entered if no errors are encountered(else correct theerrors and then perform the above).
1. Compile and execute the Server side program first then in the other
terminal compile and execute the client side program.
2. Enter the pathname of any file on the system as a request to the Server
and wait for reply.
3. Now switch to Server terminal and check if the file requested is present or
not and the status of Server. If the file is found then switch to the Client
terminal and watch the contents of file displayed on the terminal. Else it
displays that file is not found with pathname.
Department Of Information Science and Engineering Page 39
8/2/2019 Network Lab Manual(a & B)
40/62
Computer Networks Lab Manual
RSA ALGORITHM
Overview:
The RSA Public key algorithm was invented in 1977 by Ron Rivest, Adi
Shamir and Leonard Adelman (RSA). The algorithm supports encryption
and digital signatures. It is the most widely used public key algorithm. RSA
gets its security from the integer factorization problem. The algorithm is
relatively easy to understand and implement. It has been patent free since
2000.RSA is used in security protocols such as IPSEC/IKE -IP data security,
TLS/SSL -transport data security (web), PGP -email security, SSH -terminal
connection security, SILC -conferencing service security.
Theoretical Concepts:
RSA gets its security from the factorization problem. The difficulty of
factoring large numbers is the basis of security of RSA.
The Integer factorization problem (finding a number's prime factors):
For a positive integer n, find its prime factors:
n = p1 p2 ... pi where pi is positive distinct prime number
Example: 257603 = 41 * 61 * 103
Factorization algorithms can be used to factor faster than brute forcing.
Some of them are Trial division, Pollard's rho, Pollard's p-1, Quadratic sieve,
elliptic curve factorization, Random square factoring, Number field sieve,
etc. A Prime number is a positive integer and is divisible only by itself and
1. Prime numbers are found
With primality testing; an algorithm which tests a probable prime for
primality. If primality testing returns false prime numbers the cryptographic
algorithm may be insecure (or will not function correctly). RSA depends on
prime numbers in key generation. It also uses strong primes, numbers
Department Of Information Science and Engineering Page 40
8/2/2019 Network Lab Manual(a & B)
41/62
Computer Networks Lab Manual
whose factors of the prime are also primes.
The RSA algorithm:
Key generation:
1)Select random prime numbers p and q, and check that p != q2)Compute modulus n = pq
3)Compute phi = (p - 1)(q - 1)
4)Select public exponent e, 1 < e < phi such that gcd(e,phi) = 1
5)Compute private exponent d = e^-1 mod phi
6)Public key is {n, e}, private key is d
Encryption: c = m*e mod n, decryption: m = c*d mod n
The selected public exponent e, which is used as public key with n. It is used
to encrypt messages and to verify digital signatures. The e is stored for later
with n. The e is usually small number but it can be 1 < e < phi . The e must
be relatively prime to phi , hence gcd(e, phi ) = 1.
(gcd = greatest common divisor, using the Euclidean algorithm)
The private exponent d, is the actual RSA private key. The d must not be
disclosed at any time or the security of the RSA is compromised. The d is
found by computing the multiplicative inverse d = e^-1 mod phi . The
extended Euclidean algorithm is commonly used to compute inverses. The d
exponent is used to decrypt messages and to compute digital signatures.Implementations try to
find as small d as possible to make decryption faster. This is fine as long as
it is assured that d is about the same size as n. If it is only one quarter of size
it is not considered safe to be used. It is possible to find a smaller d by using
lcm(p-1,q-1) instead of phi (lcm = least common multiple, lcm(p-1,q-1) =
phi /gcd(p-1,q-1) ).
Example of RSA with small numbers:
p = 47, q = 71, compute n = p*q = 3337
Compute phi = 46 * 70 = 3220
Let e be 79, compute d = 79-1 mod 3220 = 1019
Department Of Information Science and Engineering Page 41
8/2/2019 Network Lab Manual(a & B)
42/62
Computer Networks Lab Manual
Public key is n and e, private key d, discard p and q.
Encrypt message m = 688, 68879 mod 3337 = 1570 = c.
Decrypt message c = 1570, 15701019 mod 3337 = 688 = m.
Program 6:
Write a program for simple RSA algorithm to encrypt and decrypt the
data.
#include
#include
int gcd(int a,int b)
{
int c;
while(a!=b)
{
if(a
8/2/2019 Network Lab Manual(a & B)
43/62
Computer Networks Lab Manual
}
int main()
{
int p,q,n,e,m,c,d,x,z;int en[100],de[100],j=0;
printf("\nenter the value of p & q\n");
scanf("%d%d",&p,&q);
n=p*q;
z=(p-1)*(q-1);
for(e=1;e
8/2/2019 Network Lab Manual(a & B)
44/62
Computer Networks Lab Manual
printf("\nchoose D\n");
scanf("%d",&d);
x=mod(c,d,n);
printf("after decryption=%d\n",x);
return 0;}
Output:
Enter the value of p& q: 3 5
1 3 5 7
Choose e: 7Enter the character to be encrypted: 3
Before encryption : 3
After encryption: 12
7
Choose d: 7
After decryption : 3
HAMMING CODES
Overview:
Developed by 1947 Richard. W. Hamming for detecting and correcting
single bit errors in transmitted data. This technique requires that three parity
bits (or check bits) be transmitted with every four data bits. The algorithm is
Department Of Information Science and Engineering Page 44
8/2/2019 Network Lab Manual(a & B)
45/62
Computer Networks Lab Manual
called a (7, 4) code, because it requires seven bits to encoded four bits of
data.
Theoretical Concepts :
Background Concepts Required:1. matrix multiplication
2. modulo 2 arithmetic
3. parity.
Parity:A parity bit is an extra bit that forces a binary string to have a specific
parity.
Two types:
1.Even(Even number of 1's i.e., the modulo 2 sum of the bits is 0)
2.Odd (Odd number of 1's i.e., the modulo 2 sum of the bits is 1)
The table below lists all possible three bit values and value of a parity bit
required to create a four bit sequence with even parity.
3 Bit String Parity Bit Verification
000 0 0 + 0 + 0 + 0 = 0
001 1 0 + 0 + 1 + 1 = 0
010 1 0 + 1 + 0 + 1 = 0
011 0 0 + 1 + 1 + 0 = 0100 1 1 + 0 + 0 + 1 = 0
101 0 1 + 0 + 1 + 0 = 0
110 0 1 + 0 + 1 + 0 = 0
111 1 1 + 1 + 1 + 1 = 0
Table 1. Even Parity
It is very common for communication protocols to specify that a block of
bits will be transmitted with a specific parity. If a block of data arrives at its
intended destination with a parity other than the specified parity, it must be
the case that at least one of the bits has been corrupted. A single parity bit is
not sufficient to identify an even number of bits with errors, nor is it
sufficient to allow the receiver to correct an error. Hamming codes use
multiple parity bits to allow for error correction.
Department Of Information Science and Engineering Page 45
8/2/2019 Network Lab Manual(a & B)
46/62
Computer Networks Lab Manual
Encoding
Traditional Hamming codes are (7, 4) codes, encoding four bits of data into
seven bit blocks (a Hamming code word). The extra three bits are parity bits.
Each of the three parity bits are parity for three of the four data bits, and no
two parity bits are for the same three data bits. All of the parity bits are evenparity.
Example:
Given: data bits d1, d2, d3, and d4
A (7, 4) Hamming code may define parity bits p1, p2, and p3 as
p1 = d2 + d3 + d4
p2 = d1 + d3 + d4
p3 = d1 + d2 + d4
There's a fourth equation for a parity bit that may be used in Hamming
codes:
p4 = d1 + d2 + d3
Valid Hamming codes may use any three of the above four parity bit
definitions.
Valid Hamming codes may also place the parity bits in any location within
the block of 7 data and parity bits. Two Hamming codes with different parity
bits or parity bits in a different bit position are considered equivalent. Theywill produce different results, but they are still Hamming codes.
One method for transforming four bits of data into a seven bit Hamming
code word is to use a 47 generator matrix [G].
Define d to be the 14 vector [d1 d2 d3 d4]
It's possible to create a 47 generator matrix [G] such that the product
modulo 2 of d and [G] (d[G]) is the desired 17 Hamming code word. Here's
how it's done:
Step 1: Represent each data bit with a column vector as follows:
| 1 |
d1 = | 0 |
| 0 |
| 0 |
Department Of Information Science and Engineering Page 46
8/2/2019 Network Lab Manual(a & B)
47/62
Computer Networks Lab Manual
| 0 |
d2 = | 1 || 0 |
| 0 |
| 0 |
d3 = | 0 |
| 1 |
| 0 |
| 0 |
d4 = | 0 |
| 0 |
| 1 |
Step 2:Represent each parity bit with a column vector containing a 1 in the
row corresponding to each data bit included in the computation and a zero in
all other rows. Using the parity bit definitions from the example above:
| 0 |p1 = | 1 |
| 1 |
| 1 |
| 1 |
p2 = | 0 |
| 1 |
| 1 |
| 1 |
p3 = | 1 |
| 0 |
| 1 |
Department Of Information Science and Engineering Page 47
8/2/2019 Network Lab Manual(a & B)
48/62
Computer Networks Lab Manual
Step 3:Arrange the column vectors from the previous steps into a 47 matrix
such that the columns are ordered to match their corresponding bits in a code
word.
Using the vectors from the previous steps, the following will produce code
words of the form [p1 p2 p3 d1 d2 d3 d4]
| 0 1 1 1 0 0 0 |
G = | 1 0 1 0 1 0 0 |
| 1 1 0 0 0 1 0 |
| 1 1 1 0 0 0 1 |
Arranging the columns in any other order will just change the positions of
bits in the code word.
Example:
Encode the data value 1010 using the Hamming code defined by the matrix
G (above).
| (1 0) + (0 1) + (1 1) + (0 1) |
| 1 |
| (1 1) + (0 0) + (1 1) + (0 1) || 0 |
| 0 1 1 1 0 0 0 | | (1 1) + (0 1) + (1 0) + (0 1) |
| 1 |
|1010 | | 1 0 1 0 1 0 0 | = | (1 1) + (0 0) + (1 0) + (0 0) |
= | 1 |
| 1 1 0 0 0 1 0 | | (1 0) + (0 1) + (1 0) + (0 0) |
| 0 |
| 1 1 1 0 0 0 1 | | (1 0) + (0 0) + (1 1) + (0 0) |
| 1 |
| (1 0) + (0 0) + (1 0) + (0 1) |
| 0 |
So 1010 encodes to 1011010. Equivalent Hamming codes represented by
different generator matrices will produce different results.
Department Of Information Science and Engineering Page 48
8/2/2019 Network Lab Manual(a & B)
49/62
Computer Networks Lab Manual
Decoding
In a world without errors decoding a Hamming code word would be very
easy. Just throw out the parity bits. The encoding example produced a 7 bit
code word. Its parity bits are 101 and its data bits are 1010. If you receive a
1011010, just decode it as 1010. But what happens if you receive a codeword with an error and one or more of the parity bits are wrong.
Suppose the Hamming code defined by the matrix G in the example above is
being used and the code word 1011011 is received. How is that word
decoded? The first step is to check the parity bits to determine if there is an
error.
Arithmetically, parity may be checked as follows:
p1 = d2 + d3 + d4 = 0 + 1 + 1 = 0
p2 = d1 + d3 + d4 = 1 + 1 + 1 = 1
p3 = d1 + d2 + d4 = 1 + 0 + 1 = 0
In this case every parity bit is wrong. p1, p2, and p3 should have been 010,
but we received 101.
Parity may also be validated using matrix operations. A 37 parity check
matrix [H] may be constructed such that row 1 contains 1s in the position of
the first parity bit and all of the data bits that are included in its parity
calculation. Row 2 contains 1s in the position of the second parity bit and all
of the data bits that are included in its parity calculation. Row 3 contains 1sin the position of the third parity bit and all of the data bits that are included
in its parity calculation.
Example:
Using the code from example above, the matrix H may be defined as
follows:
| 1 0 0 0 1 1 1 |
H = | 0 1 0 1 0 1 1 |
| 0 0 1 1 1 0 1 |
Multiplying the 37 matrix [H] by a 71 matrix representing the encoded
data produces a 31 matrix called the "syndrome". There are two useful
proprieties of the syndrome. If the syndrome is all zeros, the encoded data is
Department Of Information Science and Engineering Page 49
8/2/2019 Network Lab Manual(a & B)
50/62
Computer Networks Lab Manual
error free. If the syndrome has a non-zero value, flipping the encoded bit that
is in the position of the column matching the syndrome will result in a valid
code word.
Example:Using the parity check matrix from the example above we can correct and
verify the code word 1011011.
| 1 |
| 0 |
| 1000111 | | 1 | |(11)+(00)+(01)+(01)+(10)+(11) +(11) | |
1 |
| 0101011 | | 1 | = |(00)+(10)+(01)+(11)+(00)+(11)+(11) | = |
1 |
| 0011101 | | 0 | |(01)+(00)+(11)+(11)+(10)+(01)+(11) | |
1 |
| 1 |
| 1 |
A column of all 1s is not the column of all 0s, so there is a parity error.
Looking back at the matrix [H], you will see that the seventh column is all
1s, so the seventh bit is the bit that has an error. Changing the seventh bit
produces the code word 1011010.
| 1 |
| 0 |
|1000111| | 1 | |(11)+(00)+(01)+(01)+(10)+(11)+(10) |
| 0 |
|0101011| | 1 | = |(00)+(10)+(01)+(11)+(00)+(11)+(10) |
= | 0 |
|0011101| | 0 | |(01)+(00)+(11)+(11)+(10)+(01)+(10) |
| 0 |
| 1 |
| 0 |
Sure enough 1011010 is a valid code word. As I stated at the top of this
section remove the parity bits to get the encoded value. In this case 1011011
Department Of Information Science and Engineering Page 50
8/2/2019 Network Lab Manual(a & B)
51/62
Computer Networks Lab Manual
was likely transmitted as 1011010, which encodes 1010.
Program 7:
Write a program for Hamming Code generation for error detection
and correction.
#include
#include
#include
int main()
{
int i,j;
char data[4];
int d1[5],d2[5],d3[5],d4[5];int p1[5],p2[5],p3[5];
int gmatrix[4][8];
int encode[7]={0,0,0,0,0,0,0};
int edata[7]={0,0,0,0,0,0,0};
int errdig;
int syn[3]= {0,0,0};
int hmatrix[3][7] = { 1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1
};
int choice;
system("clear");
for(;;)
{
printf("\n 1.ENCODE ");
Department Of Information Science and Engineering Page 51
8/2/2019 Network Lab Manual(a & B)
52/62
Computer Networks Lab Manual
printf("\n 2.DECODE ");
printf("\n 3.EXIT ");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("\n Enter 4 data bits\n");
scanf("%s",data);
for(i=0;i
8/2/2019 Network Lab Manual(a & B)
53/62
Computer Networks Lab Manual
for(i=0;i
8/2/2019 Network Lab Manual(a & B)
54/62
Computer Networks Lab Manual
}
Output:
cc prgm8.c
./a.out1.Encode
2.Decode
3.Exit
Enter your choice : 1
Enter the 4 bits : 1101
The generator matrix is
0 1 1 1 0 0 0
1 0 1 0 1 0 0
1 1 0 0 0 1 0
1 1 1 0 0 0 1
The encoded data is:
0011101
1.Encode
2.Decode
3.Exit
Enter your choice : 2
Enter the encoded data :
0 0 1 1 1 0 1Error free data
The actual data error free data: 1101
1.Encode
2.Decode
3.Exit
Enter your choice : 3
LEAKY BUCKET ALGORITHM
The leaky-bucket implementation is used to control the rate at which trafficis sent to the network. A leaky bucket provides a mechanism by which
bursty traffic can be shaped to present a steady stream of traffic to the
network, as opposed to traffic with erratic bursts of low-volume and high-
volume flows.
Department Of Information Science and Engineering Page 54
8/2/2019 Network Lab Manual(a & B)
55/62
Computer Networks Lab Manual
Traffic analogy An appropriate analogy for the leaky bucket is a scenario in
which four lanes of automobile traffic converge into a single lane. A
regulated admission interval into the single lane of traffic flow helps the
traffic move. The benefit of this approach is that traffic flow into the major
arteries (the network) is predictable and controlled. The major liability is
that when the volume of traffic is vastly greater than the bucket size, in
conjunction with the drainage-time interval, traffic backs up in the bucket
beyond bucket capacity and is discarded.
The Leaky-bucket algorithm
The algorithm can be conceptually understood as follows:
Arriving packets (network layer PDUs) are placed in a bucket with a
hole in the bottom.
The bucket can queue at most b bytes. If a packet arrives when the
Department Of Information Science and Engineering Page 55
8/2/2019 Network Lab Manual(a & B)
56/62
Computer Networks Lab Manual
bucket is full, the packet is discarded.
Packets drain through the hole in the bucket, into the network, at a
constant rate ofrbytes per second, thus smoothing traffic bursts.
The size b of the bucket is limited by the available memory of the system.
Sometimes the leaky bucket and token algorithms are lumped together under
the same name.
Program 8:
Write a program for Congestion control using the leaky bucket
algorithm.
#include
#define min(x,y) ((x) < (y) ? (x) : (y))
Department Of Information Science and Engineering Page 56
8/2/2019 Network Lab Manual(a & B)
57/62
Computer Networks Lab Manual
#define MAX 25
int main()
{
int cap, oprt, nsec, cont, i=0,dr=0, inp[MAX]={0}, ch, x;printf("\n\nLEAKY BUCKET ALGORITHM\n");
printf("\nEnter bucket size : ");
scanf("%d",&cap);
printf("\nEnter output rate (no..of pkts/sec) : ");
scanf("%d",&oprt);
do{ printf("\nEnter the no..of packets entering at second %d :
",i+1);
scanf("%d",&inp[i++]);
printf("Enter 1 to insert packets or 0 to quit : ");
scanf("%d",&ch);
} while(ch);
nsec=i;
printf("\nSecond : Packets sent : Packets recvd : In bucket:
Dropped\n");
for(cont=i=0; cont || (i0)
if(x>cap)
{
cont=cap;
dr=x-cap;
}
else
{
cont=x;
Department Of Information Science and Engineering Page 57
8/2/2019 Network Lab Manual(a & B)
58/62
Computer Networks Lab Manual
dr=0;
}
else
cont=0;
printf(" %d\t:",cont); printf(" %d\n",dr);
}
return 0;
}
Output :
Leaky bucket algorithm:
Enter bucket size: 3
Enter the output rate(No. of packets/sec): 2
Enter the no. of packets sent at second 1: 6
Enter 1 to insert packets or 0 to quit:0
Second : packet sent : packet recvd : In bucket : Dropped
(1) (6) (2) (3) (1)
(2) (0) (2) (1) (0)
(3) (0) (1) (0) (0)
Viva Questions
Department Of Information Science and Engineering Page 58
8/2/2019 Network Lab Manual(a & B)
59/62
Computer Networks Lab Manual
1. What are functions of different layers in OSI And TCP/IP model?2. Differentiate between TCP/IP Layers and OSI Layers
3. Why header is required?
4. What is the use of adding header and trailer to frames?
5. What is encapsulation?
6. Why fragmentation requires?7. What is MTU?
8. Which layer imposes MTU?
9. Differentiate between flow control and congestion control.
10. Differentiate between Point-to-Point Connection and End-to-End connections.
11. Which are protocols running in different layers?12. What is Protocol Stack?
13. Differentiate between TCP and UDP.
14. Differentiate between Connectionless and connection oriented connection.
15. Why frame sorting is required?
16. What is meant by subnet?
17. What is meant by Gateway?
18. What is an IP address?19. What is MAC address?
20. Why IP address is required when we have MAC address?
21. What is meant by port?
22. What are ephemerical port number and well known port numbers?
23. What is a socket?24. What are the parameters of socket()?
25. Describe bind(), listen(), accept(),connect(), send() and recv().
26. What are system calls? Mention few of them.
27. What is IPC? Name three techniques.
28. Explain mkfifo(), open(), close() with parameters.
29. What is meant by file descriptor?
30. What is meant by traffic shaping?31. How do you classify congestion control algorithms?
32. Differentiate between Leaky bucket and Token bucket.
33. How do you implement Leaky bucket?
34. How do you generate busty traffic?
35. What is the polynomial used in CRC-CCITT?
36. What are the other error detection algorithms?
37. What is difference between CRC and Hamming code?38. Why Hamming code is called 7,4 code?
39. What is odd parity and even parity?
40. What is meant by syndrome?
41. What is generator matrix?42. What is spanning tree?
43. Where Pirms algorithm does finds its use in Networks?
44. Differentiate between Prims and Kruskals algorithm.
45. What are Routing algorithms?46. How do you classify routing algorithms? Give examples for each.
47. What are drawbacks in distance vector algorithm?
48. How routers update distances to each of its neighbor?
49. How do you overcome count to infinity problem?50. What is cryptography?
Department Of Information Science and Engineering Page 59
8/2/2019 Network Lab Manual(a & B)
60/62
Computer Networks Lab Manual
51. How do you classify cryptographic algorithms?
52. What is public key?
53. What is private key?
54. What are key, ciphertext and plaintext?
55. What is simulation?
56. What are advantages of simulation?57. Differentiate between Simulation and Emulation.
58. What is meant by router?
59. What is meant by bridge?
60. What is meant by switch?
61. What is meant by hub?
62. Differentiate between route, bridge, switch and hub.
63. What is ping and telnet?
64. What is FTP?
65. What is BER?
66. What is meant by congestion window?
67. What is BSS?
68. What is incoming throughput and outgoing throughput?
69. What is collision?
70. How do you generate multiple traffics across different sender-receiver pairs?
71. How do you setup Ethernet LAN?
72. What is meant by mobile host?
73. What is meant by NCTUns?74. What are dispatcher, coordinator and nctunsclient?
75. Name few other Network simulators
76. Differentiate between logical and physical address.
77. Which address gets affected if a system moves from one place to another place?
78. What is ICMP? What are uses of ICMP? Name few.
79. Which layer implements security for data?
Department Of Information Science and Engineering Page 60
8/2/2019 Network Lab Manual(a & B)
61/62
Computer Networks Lab Manual
Guidelines For installation of NCTUns Network Simulator
Follow the following steps carefully,
Please don't skip any steps that have been mentioned below
1. Install LinuxRecommended Fedora 12 with dvd(gcc compiler should be installed if it not installed
after fedora installation)
2. After installation Boot into Linux as root.
3. Copy the .tgz installation file of NCTUns that you got from college to the folder /bin/localPlease don't change any folder name in this folder that is created after unzipping the above
file. Dont even change the Case of the folder that is created
4. Now unzip the .tgz file by opening the terminal and changing the directory to /bin/localbythe command :-
[root@localhost ~] cd /bin/local
To unzip use the following command:-
[root@localhost local] tar xvzf [the file name].tar
(Note there is no '-' before xvzf)
This will create a folder called NCTUns in the directory /bin/local...
5. Now disable the Secure Linux option by running the following command :-
[root@localhost local]vi /etc/selinux/config
When the file opens, there is a line similar to -- SELINUX=enforcing
Change the "enforcing" to "disabled" (Note without quotes)
6. From the directory/bin/localchange the current working directory to NCTUns by following
command :-[root@localhost local]cd NCTUns
7. Now from here execute the installation shell script that will do the required compilations
and settings for you:-
[root@localhost local]./install.sh
During this part it will ask for installation of tunnel files. Please type yes and Enter to
continue
8. If the installation is successful, it will display the success message at the end. Now restartyour computer. You will find a new entry in GRUB Menu "NCTUns kernel login". Boot
into Linux using this entry.
9. before using simulator, please execute the following command
[root@localhost ~]iptables -F
10. Run the simulator using three commands where each command should be executed in
different window.
[root@localhost ~]dispatcher
[root@localhost ~]coordinator
Department Of Information Science and Engineering Page 61
8/2/2019 Network Lab Manual(a & B)
62/62
Computer Networks Lab Manual
[root@localhost ~]nctunsclient
11. In the NCTUns window Settings Dispatcher. Provide the username and password of the
user account u created in step 11. Then Click OK.