Professional Documents
Culture Documents
PRACTICAL FILE
ADVANCED COMPUTER
NETWORK
VINAYAK
M.TECH(IS)
1ST SEM
01310100814
Table Of Contents
S.
No.
Experiment
Date
Sign
EXPERIMENT
NO.
1
//hamming.c
#include<stdio.h>
char
data[4];
int
encoded[7],
edata[7],
syn[3],
i
=
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
gmatrix[4][7]
=
{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};
int
main()
{
int
i,
j;
printf("Enter
4
bit
data:
");
scanf("%s",
data);
printf("Encoded
Data:
");
for
(i
=
0;
i
<
7;
i++)
{
for
(j
=
0;
j
<
4;
j++)
encoded[i]
+=
((data[j])*(gmatrix[j][i]));
encoded[i]
=
encoded[i]
%
2;
printf("%d
",
encoded[i]);
}
printf("\nEnter
Encoded
bits
as
received
:
");
for
(i
=
0;
i
<
7;
i++)
scanf("%d",
&edata[i]);
for
(i
=
0;
i
<
3;
i++)
{
//Generating
Syndrome
matrix
for
(j
=
0;
j
<
7;
j++)
syn[i]
+=
(hmatrix[i][j]
*
edata[j]);
syn[i]
=
syn[i]
%
2;
}
for
(i
=
0;
i
<
7;
i++)
//finding
Error
Bit
if
(syn[0]
==
hmatrix[0][i]
&&
syn[1]
==
hmatrix[1][i]
&&
syn[2]
==
hmatrix[2][i])
break;
if
(i
==
7)
printf("Data
is
error
free!!\n");
else
{
printf("Error
in
data
at
bit
position:
%d\n",
i
+
1);
edata[i]
=
!edata[i];
//Complementing
the
bit
value
printf("The
Correct
data
Should
be
:
");
for
(i
=
0;
i
<
7;
i++)
printf("%d
",
edata[i]);
printf("\n");
}
return
0;
}
Output:
$
gcc
hamming.c
$
./a.out
[ENCODING]
Enter
4
bit
data:
1011
Encoded
Data:
0
1
0
1
0
1
1
[DECODING]Enter
Encoded
bits
as
received
:
0
1
0
1
0
1
0
Error
received
at
bit
number
7
of
the
data
The
Correct
data
Should
be
:
0
1
0
1
0
1
1
EXPERIMENT
NO.
2
/*PROGRAM
FOR
CALCULATION
OF
CRC
(CYCLIC
REDUNDANCY
CHECK)
IN
C*/
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
void
main()
{
int
i,j,n,g,a,arr[20],gen[20],b[20],q[20],s;
clrscr();
printf("Transmitter
side:");
printf("\nEnter
no.
of
data
bits:");
scanf("%d",&n);
printf("Enter
data:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Enter
size
of
generator:");
scanf("%d",&g);
do{
printf("Enter
generator:");
for(j=0;j<g;j++)
scanf("%d",&gen[j]);
}
while(gen[0]!=1);
printf("\n\tThe
generator
matrix:");
for(j=0;j<g;j++)
printf("%d",gen[j]);
a=n+(g-1);
printf("\n\tThe
appended
matrix
is:");
for(i=0;i<j;++i)
arr[n+i]=0;
for(i=0;i<a;++i)
printf("%d",arr[i]);
for(i=0;i<n;++i)
q[i]=
arr[i];
for(i=0;i<n;++i)
{
if(arr[i]==0)
{
for(j=i;j<g+i;++j)
arr[j]
=
arr[j]^0;
}
else
{
arr[i]
=
arr[i]^gen[0];
arr[i+1]=arr[i+1]^gen[1];
arr[i+2]=arr[i+2]^gen[2];
arr[i+3]=arr[i+3]^gen[3];
}
}
printf("\n\tThe
CRC
is
:");
for(i=n;i<a;++i)
printf("%d",arr[i]);
s=n+a;
for(i=n;i<s;i++)
q[i]=arr[i];
printf("\n");
for(i=0;i<a;i++)
printf("%d",q[i]);
getch();
}
Output
Transmitter
side:
Enter
no.
of
data
bits:8
Enter
data:
1
0
1
0
0
0
0
1
Enter
size
of
generator:
4
Enter
generator:
1
0
0
1
The
generator
matrix:
1001
The
appended
matrix
is:
10100001000
The
CRC
is:
111
10100001111
*/
EXPERIMENT
NO.
3
#include<stdio.h>
#include<string.h>
int
main()
{
int
i,count=0,parity;
char
data[20];
printf("Enter
data
:
");
scanf("%s",&data);
printf("Choose
parity\n0:Even\n1:Odd\n");
scanf("%d",&parity);
for(i=0;i<strlen(data);i++)
{
if(data[i]=='1')
count++;
}
if(parity==0
&&
count%2==0)
printf("No
Error
Detected");
else
if(parity==1
&&
count%2==1)
printf("No
Error
Detected");
else
printf("Error
Detected");
return
0;
}
OUTPUT
EXPERIMENT
NO
4
/*PROGRAM FOR IMPLEMENTATION OF CHECKSUM A) SENDER B)
RECIEVER*/
SENDER
SIDE
#include<stdio.h>
#include<conio.h>
#include<math.h>
void
main()
{
int
n,i,a[10],c[10],data[20],receiver[20],count=0,count2=0,
sum=0,dsum=0,wsum=0,b[20],r=0,j=0;
int
rc[20],rComplement[10],rdata[20],rSum=0,rdsum=0,rwsum=0,
rB[20];
clrscr();
printf("Enter
no.
of
bits
to
be
sent
to
the
receiver::\n");
scanf("%d",&n);
printf("Enter
numbers
in
decimal
format\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Sum
of
the
decimal
numbers
to
be
sent
::\t");
for(i=0;i<n;i++)
sum
+=a[i];
printf("%d\n\n",sum);
while(sum>0)
{
r
=
sum
%
2;
b[j]=r;
sum=sum/2;
j++;
count++;
}
printf("Binary
representation
of
total
sum\n");
for(i=count-1;i>=0;i--)
{
printf("%d\t",b[i]);
}
printf("\n4
bit
representation\n");
if(count>4)
{
printf(",after
X-ORing
extra
bit
with
the
initial
4
-
bit
od
the
sum\n");
for(i=0;i<=3;i++)
{
data[i]=b[i];
}
for(i=4,j=0;i<count;i++)
{
if(b[j]!=b[i])
{
data[j]=1;
j++;
}
else
{
data[j]=0;
j++;
}
}
}
for(i=3;i>=0;i--)
{
printf("%d\t",data[i]);
}
printf("\nWrapped
Sum::\t");
for(i=0;i<4;i++)
{
wsum
+=(data[i]*(pow(2,i)));
}
printf("%d\n",wsum);
printf("\nComplement
of
the
wrapped
sum\n");
for(i=0;i<4;i++)
{
if(data[i]==0)
c[i]=1;
else
c[i]=0;
}
printf("\n");
for(i=3;i>=0;i--)
printf("%d\t",c[i]);
printf("\n");
for(i=0;i<4;i++)
{
dsum
+=(c[i]*(pow(2,i)));
}
OUTPUT
RECIEVER
SIDE
#include<stdio.h>
#include<conio.h>
#include<math.h>
void
main()
{
int
n,i,a[10],receiver[20],count2=0,r=0,j=0;
int
rc[20],rComplement[10],rdata[20],rSum=0,rdsum=0,rwsum=0,
rB[20];
int
cksum;
clrscr();
printf("Enter
no.
of
decimal
digits
sent
from
the
sender::\t");
scanf("%d",&n);
printf("Data
received
from
the
sender\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
}
for(i=3;i>=0;i--)
{
printf("%d\t",rdata[i]);
}
printf("\nWrapped
Sum::\t");
for(i=0;i<4;i++)
{
rwsum
+=(rdata[i]*(pow(2,i)));
}
printf("%d\n",rwsum);
printf("\nComplement
of
the
wrapped
sum\n");
for(i=0;i<4;i++)
{
if(rdata[i]==0)
rc[i]=1;
else
rc[i]=0;
}
printf("\n");
for(i=3;i>=0;i--)
printf("%d\t",rc[i]);
printf("\n");
for(i=0;i<4;i++)
{
rdsum
+=(rc[i]*(pow(2,i)));
}
printf("
\nchecksum
::
%d\n",rdsum);
if(rdsum==0)
{
printf("\nData
received
correctly\n");
}
else
{
printf("\nData
contains
error");
}
getch();
}
OUTPUT
EXPERIMENT
NO.5
/*PROGRAM FOR IMPLEMENTATION OF SOCKETS DATE/TIME CLIENT
SERVER SOCKET USING TCP */
//tcpdateserver.java
import
java.net.*;
import
java.io.*;
import
java.util.*;
class
tcpdateserver
{
public
static
void
main(String
arg[])
{
ServerSocket
ss
=
null;
Socket
cs;
PrintStream
ps;
BufferedReader
dis;
String
inet;
try
{
ss
=
new
ServerSocket(4444);
System.out.println("Press
Ctrl+C
to
quit");
while(true)
{
cs
=
ss.accept();
ps
=
new
PrintStream(cs.getOutputStream());
Date
d
=
new
Date();
ps.println(d);
dis
=
new
BufferedReader(new
InputStreamReader(cs.getInputStream()));
inet
=
dis.readLine();
System.out.println("Client
System/IP
address
is
:"+
inet);
ps.close();
dis.close();
}
}
catch(IOException
e)
{
System.out.println("The
exception
is
:"
+
e);
}
}
}
//
tcpdateclient.java
import
java.net.*;
import
java.io.*;
class
tcpdateclient
{
public
static
void
main
(String
args[])
{
Socket
soc;
BufferedReader
dis;
String
sdate;
PrintStream
ps;
try
{
InetAddress
ia
=
InetAddress.getLocalHost();
if
(args.length
==
0)
soc
=
new
Socket(InetAddress.getLocalHost(),4444);
else
soc
=
new
Socket(InetAddress.getByName(args[0]),
4444);
dis
=
new
BufferedReader(new
InputStreamReader(soc.getInputStream()));
sdate=dis.readLine();
System.out.println("The
date/time
on
server
is
:
"
+sdate);
ps
=
new
PrintStream(soc.getOutputStream());
ps.println(ia);
ps.close();
}
catch(IOException
e)
{
System.out.println("THE
EXCEPTION
is
:"
+
e);
}
}
}
OUTPUT
Server:
$
javac
tcpdateserver.java
$
java
tcpdateserver
Press
Ctrl+C
to
quit
Client
System/IP
address
is
:
localhost.localdomain/127.0.0.1
Client
System/IP
address
is
:
localhost.localdomain/127.0.0.1
Client:
$
javac
tcpdateclient.java
$
java
tcpdateclient
The
date/time
on
server
is:
Wed
Jul
06
07:12:03
GMT
2011
RESULT
Thus
every
time
a
client
connects
to
the
server,
servers
date/time
will
be
returned
to
the
client
for
synchronization.
EXPERIMENT NO. 6
There
are
some
fundamental
differences
between
TCP
and
UDP
sockets.
UDP
is
a
connection-less,
unreliable,
datagram
protocol
(TCP
is
instead
connection-oriented,
reliable
and
stream
based).
There
are
some
instances
when
it
makes
to
use
UDP
instead
of
TCP.
Some
popular
applications
built
around
UDP
are
DNS,
NFS,
SNMP
and
for
example,
some
Skype
services
and
streaming
media.
Figure
shows
the
the
interaction
between
a
UDP
client
and
server.
First
of
all,
the
client
does
not
establish
a
connection
with
the
server.
Instead,
the
client
just
sends
a
datagram
to
the
server
using
the
sendto
function
which
requires
the
address
of
the
destination
as
a
parameter.
Similarly,
the
server
does
not
accept
a
connection
from
a
client.
Instead,
the
server
just
calls
the
recvfrom
function,
which
waits
until
data
arrives
from
some
client.
recvfrom
returns
the
IP
address
of
the
client,
along
with
the
datagram,
so
the
server
can
send
a
response
to
the
client.
As
shown
in
the
Figure,
the
steps
of
establishing
a
UDP
socket
communication
on
the
client
side
are
as
follows:
Create
a
socket
using
the
socket()
function;
Send
and
receive
data
by
means
of
the
recvfrom()
and
sendto()
functions.
The
steps
of
establishing
a
UDP
socket
communication
on
the
server
side
are
as
follows:
Create
a
socket
with
the
socket()
function;
Bind
the
socket
to
an
address
using
the
bind()
function;
Send
and
receive
data
by
means
of
recvfrom()
and
sendto().
Figure
:
UDP
client-server.
In
this
section,
we
will
describe
the
two
new
functions
recvfrom()
and
sendto().
The
recvfrom()
Function
This
function
is
similar
to
the
read()
function,
but
three
additional
arguments
are
required.
The
recvfrom()
function
is
defined
as
follows:
#include
<sys/socket.h>
ssize_t
recvfrom(int
sockfd,
void*
buff,
size_t
nbytes,
int
flags,
struct
sockaddr*
from,
socklen_t
*addrlen);
The
first
three
arguments
sockfd,
buff,
and
nbytes,
are
identical
to
the
first
three
arguments
of
read
and
write.
sockfd
is
the
socket
descriptor,
buff
is
the
pointer
to
read
into,
and
nbytes
is
number
of
bytes
to
read.
In
our
examples
we
will
set
all
the
values
of
the
flags
argument
to
0.
The
recvfrom
function
fills
in
the
socket
address
structure
pointed
to
by
from
with
the
protocol
address
of
who
sent
the
datagram.
The
number
of
bytes
stored
in
the
socket
address
structure
is
returned
in
the
integer
pointed
by
addrlen.
The
function
returns
the
number
of
bytes
read
if
it
succeeds,
-1
on
error.
The
sendto()
Function
This
function
is
similar
to
the
send()
function,
but
three
additional
arguments
are
required.
The
sendto()
function
is
defined
as
follows:
#include
<sys/socket.h>
ssize_t
sendto(int
sockfd,
const
void
*buff,
size_t
nbytes,
int
flags,
const
struct
sockaddr
*to,
socklen_t
addrlen);
The
first
three
arguments
sockfd,
buff,
and
nbytes,
are
identical
to
the
first
three
arguments
of
recv.
sockfd
is
the
socket
descriptor,
buff
is
the
pointer
to
write
from,
and
nbytes
is
number
of
bytes
to
write.
In
our
examples
we
will
set
all
the
values
of
the
flags
argument
to
0.
The
to
argument
is
a
socket
address
structure
containing
the
protocol
address
(e.g.,
IP
address
and
port
number)
of
where
the
data
is
sent.
addlen
specified
the
size
of
this
socket.
The
function
returns
the
number
of
bytes
written
if
it
succeeds,
-1
on
error.
JAVA
IMPLEMENTATION
:
TCP
guarantees
the
delivery
of
packets
and
preserves
their
order
on
destination.
Sometimes
these
features
are
not
required
and
since
they
do
not
come
without
performance
costs,
it
would
be
better
to
use
a
lighter
transport
protocol.
This
kind
of
service
is
accomplished
by
the
UDP
protocol
which
conveys
datagram
packets.
Datagram
packets
are
used
to
implement
a
connectionless
packet
delivery
service
supported
by
the
UDP
protocol.
Each
message
is
transferred
from
source
machine
to
destination
based
on
information
contained
within
that
packet.
That
means,
each
packet
needs
to
have
destination
address
and
each
packet
might
be
routed
differently,
and
might
arrive
in
any
order.
Packet
delivery
is
not
guaranteed.
The
format
of
datagram
packet
is:
|
Msg
|
length
|
Host
|
serverPort
|
Java
supports
datagram
communication
through
the
following
classes:
DatagramPacket
DatagramSocket
The
class
DatagramPacket
contains
several
constructors
that
can
be
used
for
creating
packet
object.
One
of
them
is:
DatagramPacket(byte[]
buf,
int
length,
InetAddress
address,
int
port);
This
constructor
is
used
for
creating
a
datagram
packet
for
sending
packets
of
length
length
to
the
specified
port
number
on
the
specified
host.
The
message
to
be
transmitted
is
indicated
in
the
first
argument.
The
key
methods
of
DatagramPacket
class
are:
byte[]
getData()
Returns
the
data
buffer.
int
getLength()
Returns
the
length
of
the
data
to
be
sent
or
the
length
of
the
data
received.
void
setData(byte[]
buf)
Sets
the
data
buffer
for
this
packet.
void
setLength(int
length)
Sets
the
length
for
this
packet.
The
class
DatagramSocket
supports
various
methods
that
can
be
used
for
transmitting
or
receiving
data
a
datagram
over
the
network.
The
two
key
methods
are:
void
send(DatagramPacket
p)
Sends
a
datagram
packet
from
this
socket.
void
receive(DatagramPacket
p)
Receives
a
datagram
packet
from
this
socket.
A
simple
UDP
server
program
that
waits
for
clients
requests
and
then
accepts
the
message
(datagram)
and
sends
back
the
same
message
is
given
below.
Of
course,
an
extended
server
program
can
manipulate
clients
messages/request
and
send
a
new
message
as
a
response.
Program
//
UDPServer.java:
A
simple
UDP
server
program.
import
java.net.*;
import
java.io.*;
public
class
UDPServer
{
public
static
void
main(String
args[]){
DatagramSocket
aSocket
=
null;
if
(args.length
<
1)
{
System.out.println(Usage:
java
UDPServer
<Port
Number>);
System.exit(1);
}
try
{
int
socket_no
=
Integer.valueOf(args[0]).intValue();
aSocket
=
new
DatagramSocket(socket_no);
byte[]
buffer
=
new
byte[1000];
while(true)
{
DatagramPacket
request
=
new
DatagramPacket(buffer,
buffer.length);
aSocket.receive(request);
DatagramPacket
reply
=
new
DatagramPacket(request.getData(),
request.getLength(),request.getAddress(),
request.getPort());
aSocket.send(reply);
}
}
catch
(SocketException
e)
{
System.out.println(Socket:
+
e.getMessage());
}
catch
(IOException
e)
{
System.out.println(IO:
+
e.getMessage());
}
finally
{
}
}
}
if
(aSocket
!=
null)
aSocket.close();
A
corresponding
client
program
for
creating
a
datagram
and
then
sending
it
to
the
above
server
and
then
accepting
a
response
is
listed
below.
Program
//
UDPClient.java:
A
simple
UDP
client
program.
import
java.net.*;
import
java.io.*;
public
class
UDPClient
{
public
static
void
main(String
args[]){
//
args
give
message
contents
and
server
hostname
DatagramSocket
aSocket
=
null;
if
(args.length
<
3)
{
System.out.println(
Usage:
java
UDPClient
<message>
<Host
name>
<Port
number>);
System.exit(1);
}
try
{
aSocket
=
new
DatagramSocket();
byte
[]
m
=
args[0].getBytes();
InetAddress
aHost
=
InetAddress.getByName(args[1]);
int
serverPort
=
Integer.valueOf(args[2]).intValue();
DatagramPacket
request
=
new
DatagramPacket(m,
args[0].length(),
aHost,
serverPort);
aSocket.send(request);
byte[]
buffer
=
new
byte[1000];
DatagramPacket
reply
=
new
DatagramPacket(buffer,
buffer.length);
aSocket.receive(reply);
System.out.println(Reply:
+
new
String(reply.getData()));
}
catch
(SocketException
e)
{
System.out.println(Socket:
+
e.getMessage());
}
catch
(IOException
e)
{
System.out.println(IO:
+
e.getMessage());
}
finally
{
if
(aSocket
!=
null)
aSocket.close();
}
}
}
EXPERIMENT
NO
7
/*PROGRAM
TO
IMPLEMENT
BIT
STUFFING*/
#include<stdio.h>
#include
<conio.h>
int
main()
{
int
i=0,count=0;
char
a[100];
printf("enter
the
bits
:
");
scanf("%s",a);
printf("\nAfter
bit
stuffing
\n");
printf("01111110");
for(i=0;a[i];
i++)
{
if(a[i]=='1')
count++;
else
count=0;
printf("%c",a[i]);
if(count==5)
{
printf("0");
count=0;
}
}
printf("01111110");
getch();
return
1;
}
OUTPUT
: