Escolar Documentos
Profissional Documentos
Cultura Documentos
PART - A
SIMULATION EXERCISES
Introduction
The network simulator can design and simulate a network with network protocols
used for research.
Network emulation
refers to actual network traffic passing through some software which might do
some analysis or modify the traffic.
Types of simulators
NIST
INSANE
REAL
NS
OPNET
NUTUns ( National chiao Tung Univercity network simulator) : is open
source , high quality and support many types of networks.
PART A
Page 1
The following experiments shall be conducted using either NS / NCTUns or any other
suitable simulator.
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 as follows:
n0 n2, n1 n2 and n2 n3. Apply TCP agent between n0-n3 and UDP between n1n3. Apply relevant applications over TCP and UDP agents changing the parameter and
determine the number of packets sent by TCP / UDP.
3.
4. Simulate an Ethernet LAN using n nodes (6-10), change error rate and data rate
and compare throughput.
5. Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination.
6. Simulate simple ESS and with transmitting nodes in wire-less LAN by simulation
and determine the performance with respect to transmission of packets.
Page 2
PART A Programs
The following experiments shall be conducted using NS2 or NCTUns-6.0 or other
suitable simulator
Then :
1> add nodes and create the network
2> configure the network devices
3> start the network and analyze the network behavior.
Page 3
Page 4
Page 5
Page 6
Receiver:rtg u w log1
For TCP Connection:
Sender
stcp -p 7000 -l 1024 1.0.1.2
Receiver:
rtcp -p 7000 -l 1024
Parameters:Throughput of incoming and outgoing Packets
Page 7
Page 8
4. 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 hub
and 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
TCP Connection:
Sender
(Stcp p portno l buffer size IP address)
stcp -p 7000 -l 1024 1.0.1.2
Receiver:
rtcp -p 7000 -l 1024
3. Setup the error rate , data rate in the physical layer, i/p and o/p throughput in
the 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.00, run and check
Input Throughput and Output Throughput
Topology:-
TCP Connection:
Sender
stcp -p 7000 -i 1024 1.0.1.2
Receiver:
rtcp -p 7000 -l 1024
Double click on receiver link and change BER to 1.00, Run Again, and compare
Parameters:Throughput of outgoing Packets
Page 9
5. 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
hub using 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 layer
4. Plot the congestion window , go to menu->tools->plotgraph->file->open filename
results-> filename .coll.log
5. Give the results in filename .results
Topology:-
Page 10
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.
Subnet ID:
Port number of router (2)
Page 11
Page 12
PART-B
Error 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. If
errors 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
Page 13
10001000000100001)
1111111111111111010000010000000000000000
10001000000100001
redbitsareinitialvalue
11101111110111111
boldbitsaremessage
10001000000100001
bluebitsareaugmentation
Page 14
11001111100111100
10001000000100001
10001111000111010
10001000000100001
00001110000110110
00000000000000000
00011100001101100
00000000000000000
00111000011011000
00000000000000000
01110000110110001
00000000000000000
11100001101100010
10001000000100001
11010011010000110
10001000000100001
10110110101001110
Page 15
Program 1:
Write a program for error detecting code using CRC-CCITT (16-bits).
#include<stdio.h>
#include<string.h>
#define N strlen(g)
void xor(){
for(c=1;c<N;c++)
cs[c] = ((cs[c] == g[c])?'0':'1');
}
void crc(){
for(e=0;e<N;e++) cs[e]=t[e];
do {
Page 16
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);
}
void main()
{
//
clrscr();
printf("\n Enter the data in binary : ");
scanf("%s",t);
printf("Generating poynomial is : %s", g);
a = strlen(t);
for(e=a;e<a+N-1;e++) t[e]='0';
printf("\n Modified t[u] is : %s",t);
crc();
printf("\nChecksum is : %s", cs);
for(e=a;e<a+N-1;e++) t[e]=cs[e-a];
printf("\nFinal CodeWord is : %s\n",t);
Page 17
if(e==0){
printf("\nEnter position where error is to inserted :");
scanf("%d",&e);
t[e]=(t[e]=='0')?'1':'0';
printf("\nErrorneous data : %s\n",t);
}
crc();
for(e=0;(e<N-1)&&(cs[e]!='1');e++);
if(e<N-1) printf("\nError detected\n");
else printf("\nNo error Detected\n");
//
getch();
Output:
Page 18
y 2 0 1
z
from
cost to
x y z
y 2 0 1
z 7 1 0
from
cost to
x y z
y
z 71 0
from
x 0 2 7
x 0 2 3
Department
y 2 0Of 1Information Science
y 2 and
0 Engineering
1
z 3 1 0
z 3 1 0
from
from
Page 19
Program 3:
Write a program for distance vector algorithm to find suitable path for
transmission.
#include<stdio.h>
#include<string.h>
struct node
{
int dist[20]; // cost
int from[20]; // nexthop
}rt[10];
Page 20
int main()
{
int dmat[20][20],i,j,k;
int n=i=j=k=0,count=0;
printf("Enter The Number of Nodes in the Network\n");
scanf("%d",&n);
printf("Enter The Cost Matrix( 999 if there is no direct link)\n");
for(i=0 ;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
Page 21
for(j=0;j<n;j++)
for(k=0;k<n; k++)
if(rt[i].dist[j] > dmat[i][k]+rt[k].dist[j]) // finding new route
with less cost
{
rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j];// new route with
less cost
rt[i].from[j]=k;
// via node k
count++;
}
}while (count!=0);
for(i=0;i<n;i++)
{
printf("\n\nRouting Table Of Router %c Is\n",65+i);
printf("
Destination
NextHop
Distance \n");
for(j=0;j<n;j++)
printf("\t%c\t\t%c\t\t%d\n",65+j, 65+rt[i].from[j], rt[i].dist[j]);
}
}
Page 22
Output:
Page 23
Page 24
The steps involved in establishing a socket on the client side are as follows1.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
follows1.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
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.
Page 25
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 order to/from
a dotted decimal string)
The header files used in order are:1.<sys/types.h> -prerequisite typedefs.
2.<errno.h> -names for erno values (error numbers)
3.<sys/socket.> - struct sock addr ;system prototypes and constants .
4.<netdb.h.h> - network info lookup prototypes and structures.
5.<netinet/in.h> - struct sockaddr_in; byte ordering macros.
6.<arpa/inet.h> - 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.
Client program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
Page 26
//for sockaddr
#include<netinet/in.h>
//for sockaddr_in
#include<sys/fcntl.h>
//for O_RDONLY
#include<netdb.h>
#define SERVER_PORT 2234
#define BUF_SIZE 4096
int main()
{
int c,s,bytes;
char buf[BUF_SIZE],fname[255];
struct hostent *h;
struct sockaddr_in channel;
h=gethostbyname("localhost");
if(!h){
printf("gethostbyname failed");
exit(0);
}
memset(&channel,0,sizeof(channel));
'channel'
channel.sin_family=AF_INET;
memcpy(&channel.sin_addr.s_addr,h->h_addr,h->h_length);
channel.sin_port=htons(SERVER_PORT);
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
Page 27
exit(0);
while(1)
{
channel
bytes=read(s,buf,BUF_SIZE);
if (bytes<=0) exit(0);
write(1,buf,bytes);
}
}
Server program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
//for sockaddr
#include<netinet/in.h>
//for sockaddr_in
#include<sys/fcntl.h>
//for O_RDONLY
#include<netdb.h>
#define SERVER_PORT 2234
#define BUF_SIZE 4096
Department Of Information Science and Engineering
Page 28
int main()
{
int s,b,l,fd,sa,bytes,on=1;
char buf[BUF_SIZE],fname[255];
struct sockaddr_in channel;
s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(s<0) printf("socket failed"), exit(0);
//
setsockopt(s,SOL_SOCKET,SO_REUSEADDR,
(char*)&on,sizeof(on)); // optional
//assign values
channel.sin_addr.s_addr=htonl(INADDR_ANY);
channel.sin_port=htons(SERVER_PORT);
b=bind(s,(struct sockaddr*)&channel,sizeof(channel));
if(b<0) printf("bind failed"),
listen(s,5);
exit(0);
Page 29
while(1)
{
memset(fname,0,sizeof(fname));
read(sa,fname,BUF_SIZE);
channel
printf("requested filename: %s",fname);
fd=open(fname,O_RDONLY);
//open file to
READ
if(fd<0)
{
}
else {
while(1)
{
bytes = read(fd,buf,BUF_SIZE);
in buf
if(bytes<=0) break;
write(sa,buf,bytes);
Page 30
}
close(fd);
//close connection
}
close(sa);
}
}
Output:
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 <programname>.c
-o <output filename> and then the command ./<output filename> is
entered if no errors are encountered(else correct the errors and then perform
the above).
1.Compile and execute the Server side program. Then in the other terminal
compile and execute the Client side program.
Page 31
Page 32
Well-known
FIFO
Client-specific
FIFO
Client
Client-specific
FIFO
Client
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(<fifo name>, 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
Page 33
Program 5
Implement the above program using as message queues of FIFOs as IPC
channel.
server program:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#define FIFO1_NAME "fifo1"
#define FIFO2_NAME "fifo2"
int main()
{
char p[100],f[100],c[3000];
Page 34
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");
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))<0){
printf("Server %s not found\n",p);return(0);exit(1);
}
else
{
printf("Server %s found \n transferring the contents\n",p);stdin =
fdopen(f1,"r");
if(fgets(c,3000,stdin)!=NULL){
fd2 = open(FIFO2_NAME,O_WRONLY);
if(num2 = write(fd2,c,strlen(c))== -1)
perror("Transfer error\n");
else
printf("Server:Transfer Completed size=%d\n",strlen(c) );
}
else
perror("read Error\n");
exit(1);
}
}
}
}
Client program:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
Page 35
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#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
Page 36
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 request
Server /ns/nwlab/ns2.33/file found
Transferring the contents
Server:transfer completed size=33
EXECUTION INSTRUCTIONS:
Page 37
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
Page 38
Page 39
Program 6:
Write a program for simple RSA algorithm to encrypt and decrypt the
data.
#include<stdio.h>
#include<stdlib.h>
int gcd(int a,int b)
{
int c;
while(a!=b)
{
if(a<b)
{
c=a;a=b;b=c;
}
a-=b;
}
return a;
}
int mod(int m,int e,int n)
{
int a=1;
while(e)
{
a=(a*m)%n;
e--;
}
return a;
Page 40
}
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<n;e++)
{
if(gcd(e,z)==1)
{
en[j]=e;
printf(" %d",en[j++]);
}
}
printf("\nchoose e\n");
scanf("%d",&e);
if(gcd(e,z)!=1)
{
printf("\nthe value not from list\n");
exit(0);
}
printf("enter the character to be encrypted:\n");
scanf("%d",&m);
printf("before encryption:%d\n",m);
c=mod(m,e,n);
printf("after encryption:%d\n",c);
for(d=0;d<n;d++)
{
if((d*e)%z==1)
{
de[j]=d;
printf(" %d",de[j++]);
}
}
Page 41
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: 7
Enter 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 42
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
001
010
011
100
101
110
111
0
1
1
0
1
0
0
1
0+0+0+0
0+0+1+1
0+1+0+1
0+1+1+0
1+0+0+1
1+0+1+0
1+0+1+0
1+1+1+1
=
=
=
=
=
=
=
=
0
0
0
0
0
0
0
0
Page 43
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 even
parity.
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. They
will 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|
Page 44
|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|
Page 45
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]
|0111000|
G= |1010100|
|1100010|
|1110001|
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|
|0111000 |
|1|
|1010 |
|1010100 |
=|1|
|1100010 |
|0|
|1110001 |
|1|
| (1 1) + (0 1) + (1 0) + (0 1) |
=
| (1 1) + (0 0) + (1 0) + (0 0) |
| (1 0) + (0 1) + (1 0) + (0 0) |
| (1 0) + (0 0) + (1 1) + (0 0) |
| (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.
Page 46
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 code
word 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 1s
in 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:
H=
|1000111|
|0101011|
|0011101|
Page 47
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.
|1000111|
|0|
|0101011|
= |0|
|0011101|
|0|
|1|
|0|
|1|
|(11)+(00)+(01)+(01)+(10)+(11)+(10) |
| 1 | = |(00)+(10)+(01)+(11)+(00)+(11)+(10) |
|0|
|(01)+(00)+(11)+(11)+(10)+(01)+(10) |
|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
Page 48
Program 7:
Write a program for Hamming Code generation for error detection and
correction.
#include<stdio.h>
#include<unistd.h>
#include<math.h>
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 ");
Page 49
Page 50
for(i=0;i<7;i++)
for(j=0;j<4;j++)
encode[i]=encode[i] + ( data[j] * gmatrix[j][i]);
printf("\n the encoded data is \n");
for(i=0;i<7;i++)
{
encode[i]=encode[i] % 2;
}
for(i=0;i<7;i++)
printf("%d",encode[i]);
break;
case 2:
printf("\n enter the encoded data \n");
for(i=0;i<7;i++)
scanf("%d",&edata[i]);
for(i=0;i<3;i++)
for(j=0;j<7;j++)
syn[i]=syn[i] + ( hmatrix[i][j] * edata[j] );
for(i=0;i<3;i++)
syn[i]=syn[i] % 2;
errdig = 4 * syn[0] + 2 * syn[1] + 1 * syn[2];
if (errdig == 0)
printf("\n Error free data \n");
else
{
printf("\n error in the bit number %d\n",errdig);
errdig--;
if(edata[errdig] == 1)
edata[errdig]=0;
else
edata[errdig]=1;
}
printf("\n the actaual data error free data : ");
for(i=3;i<7;i++)
printf("%d",edata[i]);
break;
default : exit(0);
}
}
Page 51
}
Output:
cc prgm8.c
./a.out
1.Encode
2.Decode
3.Exit
Enter your choice : 1
Enter the 4 bits : 1101
The generator matrix is
0
1
1
1
1
0
1
0
1
1
0
0
1
1
1
0
0
1
0
0
0
0
1
0
0
0
0
1
Page 52
Page 53
Program 8:
Write a program for Congestion control using the leaky bucket
algorithm.
#include<stdio.h>
#define min(x,y) ((x) < (y) ? (x) : (y))
Page 54
#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{
",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 || (i<nsec); i++)
{
printf(" (%d) :",i+1);
printf(" (%d)\t: ",/*nsec?*/inp[i]/*:0*/);
printf(" (%d)\t: ",min(cont+inp[i],oprt));
if((x=cont+inp[i]-oprt)>0)
if(x>cap)
{
cont=cap;
dr=x-cap;
}
else
{
cont=x;
Page 55
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 :
(1)
(6)
(2)
(2)
(0)
(2)
(3)
(0)
(1)
In bucket :
(3)
(1)
(0)
Dropped
(1)
(0)
(0)
Viva Questions
Page 56
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
Page 57
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
Page 58
1.
2.
3.
4.
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/local change 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 restart
your computer. You will find a new entry in GRUB Menu "NCTUns kernel login". Boot
into Linux using this entry.
9.
10.
Page 59
11.
In the NCTUns window Settings Dispatcher. Provide the username and password of the
user account u created in step 11. Then Click OK.
Page 60