Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
COLLEGE
KILAMBI,
KANCHIPURAM-631551
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
Networks Laboratory
(CS 6411)
NAME
: _________________________________
REG. NUMBER
: _________________________________
SEMESTER
: _________________________________
YEAR
: ___________________________________
THIRUMALAI
ENGINEERING
COLLEGE
KILAMBI,
KANCHIPURAM631551
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
BONAFIDE CERTIFICATE
This is to certify that this is a
bonafide work done by
Mr./Miss.______________________________
Reg.No._____________________
of
B.E.,
Computer
Science
and
Internal Examiner
External Examiner
5.If your frames reach the server it will send ACK signal to client
otherwise
it
will
send
NACK
signal
to
client.
6.Stop the program
Program :
imp
ort
java.
net.*
;
imp
ort
java.
io.*;
imp
ort
java.
rmi.
*;
p
u
b
l
i
c
c
l
a
s
s
s
l
i
d
s
e
n
d
e
r
{
public
static
void
main(String
a[])throws
Exception
{
ServerSocket ser=new ServerSocket(10); Socket s=ser.accept();
DataInputStream
in=new
DataInputStream(System.in);
DataInputStream
in1=new
DataInputStream(s.getInputStream()); String
sbuff[]=new String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i; String ch;
do
{
p=new
PrintStream(s.getOutputStream());
System.out.print("Enter the
no. of frames : ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter
"+nf+"
Messages to be send\n"); for(i=1;i<=nf;i+
+)
{
s
b
u
f
f
[
s
p
t
r
]
=
i
n
.
r
e
a
d
L
i
n
e
(
)
;
p
.
p
r
i
n
t
l
n
(
s
b
u
f
f
[
s
p
t
r
]
)
;
sptr=++sptr%8;
}
sws-=nf;
System.out.print("Acknowledgment received");
ano=Integer.parseInt(in1.readLine());
{
System.out.println("The no. of
frames exceeds window size");
break;
}
System.out.print("\nDo you wants to send some more
frames
:
");
ch=in.readLine();
p.println(ch);
}
w
h
i
l
e
(
c
h
.
e
q
u
a
l
s
(
"
y
e
s
"
)
)
;
s
.
c
l
o
s
e
(
)
;
}
}
RECEIVER PROGRAM
import java.net.*; import java.io.*; class slidreceiver
{
public
static
void
main(String
a[])throws
Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10); DataInputStream
in=new
DataInputStream(s.getInputStream());
PrintStream
nf=Integer.pa
rseInt(in.read
Line());
if(nf<=rws1)
{
f
o
r
(
i
=
1
;
i
<
=
n
f
;
i
+
+
)
{
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("The
received Frame " +rptr+" is :
"+rbuf[rptr]);
}
rws-=nf;
Syst
em.
out.
prin
tln("
\nA
ckn
owl
edg
men
t
sent
\n");
p.pr
intl
n(rp
tr+1
);
rws
+=n
f; }
else
break;
c
h
=
i
n
.
r
e
a
d
L
i
n
e
(
)
;
}
w
h
i
l
e
(
c
h
.
e
q
u
a
l
s
(
"
y
e
s
"
)
)
;
}
}
OUTPUT:
//SENDER OUTPUT
E
n
t
e
r
t
h
e
n
o
.
o
f
f
r
a
m
e
s
:
4
E
n
t
e
r
4
M
e
s
s
a
g
e
s
t
o
b
e
s
e
n
d
hiii
h
o
w
r
u
i
a
m
f
i
n
e
how is evryone
Acknowledgment received for 4 frames
Do you wants to send some more frames : no
//RECEIVER OUTPUT
The received Frame 0 is : hiii
The received Frame 1 is : how r u
The received Frame 2 is : i am fine
The received Frame 3 is : how is everyone
5.If your frames reach the server it will send ACK signal to
client otherwise it will send NACK signal to client.
6.Stop the program
PROGRAM
/SENDER PROGRAM
import java.io.*;
import java.net.*;
p
u
b
l
i
c
c
l
a
s
s
S
e
n
d
e
r
{
S
o
c
k
e
t
s
e
n
d
e
r
;
O
b
j
e
c
t
O
u
t
p
u
t
S
t
r
e
a
m
o
u
t
;
O
b
j
e
c
t
I
n
p
u
t
S
t
r
e
a
m
i
n
;
S
t
r
i
n
g
p
a
c
k
e
t
,
a
c
k
,
s
t
r
,
m
s
g
;
i
n
t
n
,
i
=
0
,
s
e
q
u
e
n
c
e
=
0
;
S
e
n
d
e
r
(
)
{
}
p
u
b
l
i
c
v
o
i
d
r
u
n
(
)
{
t
r
y
{
BufferedReader
br=new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("Waiting for Connection
......................................................................
");
sen
der
=
new
Soc
ket("
local
host
",20
04);
sequ
ence
=0;
out=new
ObjectOutputStream(s
ender.getOutputStrea
m()); out.flush();
in=new
ObjectInputStrea
m(sender.getInput
Stream());
str=(String)in.rea
dObject();
System.out.println("reciver > "+str);
System.out.println("Enter the data to send. . .");
p
a
c
k
e
t
=
b
r
.
r
e
a
d
L
i
n
e
(
)
;
n
=
p
a
c
k
e
t
.
l
e
n
g
t
h
(
)
;
d
o
{
t
r
y
{
if(i<n){
msg=String.valueOf(sequence);
msg=
msg.c
oncat(
packe
t.subs
tring(i
,i+1));
}
else if(i==n){
msg
="en
d";o
ut.wr
iteO
bject
(msg
);bre
ak;
}
out.writeObject(msg);
s
e
q
u
e
n
c
e
=
(
s
e
q
u
e
n
c
e
=
=
0
)
?
1
:
0
;
o
u
t
.
f
l
u
s
h
(
)
;
Sy
ste
m.
out
.pr
int
ln(
"d
ata
se
nt
>"
+
ms
g);
ac
k=
(St
rin
g)i
n.r
ea
dO
bje
ct(
);
System.o
ut.println
("waiting
for ack
...............
\n\n");
if(ack.eq
uals(Stri
ng.value
Of(seque
nce))){
i++;
System.out.println("rec
eiver
>
"+"
packet
recieved\n\n");
}
else{
System.out.printl
n("Time
out
resending data
............................
\n\n");
sequence=(seque
nce==0)?1:0;
}
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
}
}
w
h
i
l
e
(
i
<
n
+
1
)
;
System.
out.print
ln("All
data
sent.
exiting."
);
}
catch(E
xception
e){}
finally{
try{
i
n
.
c
l
o
s
e
(
)
;
o
u
t
.
c
l
o
s
e
(
)
;
s
e
n
d
e
r
.
c
l
o
s
e
(
)
;
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
}
}
}
pu
bli
c
sta
tic
vo
id
m
ai
n(
St
rin
g
ar
gs
[])
{
Se
nd
er
s=
ne
w
Se
nd
er(
);
s.run();
}
}
//RECEIVER PROGRAM
import java.io.*;
import java.net.*;
public class Reciever{
ServerSocket reciever;
Socket connection=null;
ObjectOutputStream out;
ObjectInputStream in;
String packet,ack,data="";
int i=0,sequence=0;
Reciever(){}
p
u
b
l
i
c
v
o
i
d
r
u
n
(
)
{
t
r
y
{
BufferedReader
br=new
BufferedReader(new
InputStreamReader(System.in)); reciever =
new ServerSocket(2004,10);
System.out.
println("wa
iting
for
connection.
..");
connection
=reciever.a
ccept();
sequence=0;
System.out.println("Connection established :");
out=new
ObjectOutputStream(conn
ection.getOutputStream());
out.flush();
in=new
ObjectInputStream(co
nnection.getInputStrea
m());
out.writeObject("conn
ected .");
do{
try{
packet=(String)in.readObject();
if(Integer.valueOf(
packet.substring(0,
1))==sequence)
{ data+=packet.su
bstring(1);
sequence=(sequence==0)?1:0;
System.out.println
>"+packet);
("\n\nreceiver
}
else
{
>"+packet +" duplicate data");
System.out.println
("\n\nreceiver
}
if(i<3){
out.writeOb
ject(String.
valueOf(seq
uence));i+
+;
}
else{
out.writeObject
(String.valueOf
((sequence+1)
%2));
i=0;
}
}
catch(Exception e){}
}while(!packet.equals("end"));
Syste
m.out.
println
("Data
recive
d="+d
ata);
out.wr
iteObj
ect("c
onnect
ion
ended
.");
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
}
f
i
n
a
l
l
y
{
try{
i
n
.
c
l
o
s
e
(
)
;
o
u
t
.
c
l
o
s
e
(
)
;
r
e
c
i
e
v
e
r
.
c
l
o
s
e
(
)
;
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
}
}
}
pu
bli
c
sta
tic
vo
id
m
ai
n(
St
rin
g
ar
gs
[])
{
Re
ci
ev
er
s=
ne
w
Re
ci
ev
er(
);
while(true){
s.run();
}
}
}
OUTPUT:
//SENDER OUTPUT
W
a
i
t
i
n
g
f
o
r
C
o
n
n
e
c
t
i
o
n
r
e
c
i
v
e
r
>
c
o
n
n
e
c
t
e
d
.
E
n
t
e
r
t
h
e
d
a
t
a
t
o
s
e
n
d
m
y
n
a
m
e
data sent>0m
waiting for ack....
receiver
> packet recieved
data sent>1y
waiting for ack....
receiver
> packet recieved
data sent>0n
waiting for ack....
receiver
> packet recieved
data sent>1a
waiting for ack....
T
i
m
e
o
u
t
r
e
s
e
n
d
i
n
g
d
a
t
a
d
a
t
a
s
e
n
t
>
1
a
waiting for ack....
receiver
> packet recieved
data sent>0m
waiting for ack....
receiver
> packet recieved
data sent>1e
o
n
e
s
t
a
b
l
i
s
h
e
d
:
receiver
receiver
receiver
receiver
receiver
receiver
receiver
D
a
t
a
r
e
c
i
v
e
d
=
m
y
n
a
m
e
w
a
>0m
>1y
>0n
>1a
>1a duplicate data
>0m
>1e
i
t
i
n
g
f
o
r
c
o
n
n
e
c
t
i
o
n
.
.
.
n
t
.
j
a
v
a
:
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
c
l
a
s
s
d
a
t
e
c
l
i
e
n
t
{
p
u
bl
ic
st
at
ic
v
oi
d
m
ai
n
(
St
ri
n
g
ar
g
s[
])
{
Socket soc;
D
a
t
a
I
n
p
u
t
S
t
r
e
a
m
d
i
s
;
S
t
r
i
n
g
s
d
a
t
e
;
P
r
i
n
t
S
t
r
e
a
m
p
s
;
t
r
y
{
InetAdd
ress
ia=InetA
ddress.g
etLocal
Host();
soc=new
Socket(i
a,8020);
}
c
a
t
c
h
(
I
O
E
x
c
e
p
t
i
o
n
dis=new
DataInputStre
am(soc.getIn
putStream());
sdate=dis.rea
dLine();
System.out.println(
"THE date in the
server is:"+sdate);
ps=new
PrintStream(soc.ge
tOutputStream());
ps.println(ia);
e
)
{
System.out.println("THE EXCEPTION is :"+e);
}
}
}
DateServer.java:
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
c
l
a
s
s
d
a
t
e
s
e
r
v
e
r
{
p
u
b
li
c
s
t
a
ti
c
v
o
i
d
m
a
i
n
(
S
tr
i
n
g
a
r
g
s
[
]
)
{
S
e
r
v
e
r
S
o
c
k
e
t
s
s
;
S
o
c
k
e
t
s
;
PrintStream ps;
D
a
t
a
I
n
p
u
t
S
t
r
e
a
m
d
i
s
;
S
t
r
i
n
g
i
n
e
t
;
try
{
s
s
=
n
e
w
S
e
r
v
e
r
S
o
c
k
e
t
(
8
0
2
0
)
;
w
h
i
l
e
(
t
r
u
e
)
{
s=ss.accept();
ps=ne
w
PrintSt
ream(s.
getOut
putStre
am());
Date
d=new
Date();
ps.println(d);
dis=new
DataInputS
tream(s.get
InputStrea
m());
inet=dis.re
adLine();
System.out.println("THE
CLIENT SYSTEM ADDRESS
IS
:"+inet);
ps.close();
}
}
c
a
t
c
h
(
I
O
E
x
c
e
p
t
i
o
n
e
)
{
}
}
OUTPUT:
CLIENTSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac dateclient.java
Note: dateclient.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java dateclient
THE date in the server is:Sat
Mar
19
13:01:16
GMT+05:30
2008
C:\Program
Files\Java\jdk1.5.0\bin>
SERVERSIDE:
C:\Program Files\Java\jdk1.5.0\bin>javac dateserver.java
Note: dateserver.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
C:\Program Files\Java\jdk1.5.0\bin>java dateserver
THE CLIENT SYSTEM ADDRESS IS :com17/192.168.21.17
Aim:
To write a java program for simulating ARP protocols using TCP
ALGORITHM:
Client
1.
2.
3.
4.
5.
Server
1. Start the program
2. Accept the socket which is created by the client.
3. Server maintains the table in which IP and corresponding MAC
addresses are stored.
4. Read the IP address which is send by the client.
5. Map the IP address with its MAC address and return the MAC address to
client.
Program
Client:
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
class Clientarp
{
p
u
b
li
c
s
t
a
ti
c
v
o
i
d
m
a
i
n
(
S
tr
i
n
g
a
r
g
s
[
]
)
{
t
r
y
{
BufferedReader
in=new
BufferedReader(new
InputStreamReader(System.in));
Socket
clsct=new Socket("127.0.0.1",139);
DataInputStream din=new
DataInputStream(clsct.getInputStream());
DataOutputStream
dout=new
DataOutputStream(clsct.getOutputStr
eam()); System.out.println("Enter the
Logical address(IP):");
String str1=in.readLine();
d
o
u
t
.
w
r
i
t
e
B
y
t
e
s
(
s
t
r
1
+
'
\
n
'
)
;
S
t
r
i
n
g
s
t
r
=
d
i
n
.
r
e
a
d
L
i
n
e
(
)
;
System.out.print
ln("The
Physical
Address
is:
"+str);
clsct.close();
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
}
}
Server:
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
c
l
a
s
s
S
e
r
v
e
r
a
r
p
{
p
u
b
li
c
s
t
a
ti
c
v
o
i
d
m
a
i
n
(
S
tr
i
n
g
a
r
g
s
[
]
)
{
t
r
y
{
Serv
erSo
cket
obj=
new
Serv
erSo
cket(
139);
Sock
et
obj1
=obj.
acce
pt();
while(true)
{
DataInputStream din=new
DataInputStream(obj1.getInputStream());
DataOutputStream
dout=new
DataOutputStream(obj1.getOutputStre
am()); String str=din.readLine();
String ip[]={"165.165.80.80","165.165.79.1"};
String
mac[]={"6A:0
8:AA:C2","8A
:BC:E3:FA"};
for(int
i=0;i<ip.lengt
h;i++)
{
i
f
(
s
t
r
.
e
q
u
a
l
s
(
i
p
[
i
]
)
)
{
d
o
u
t
.
w
r
i
t
e
B
y
t
e
s
(
m
a
c
[
i
]
+
'
\
n
'
)
;
b
r
e
a
k
;
}
}
o
b
j
.
c
l
o
s
e
(
)
;
}
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
}
}
Output:
E
:
\
n
e
t
w
o
r
k
s
>
j
a
v
a
S
e
r
v
e
r
a
r
p
E
:
\
n
e
t
w
o
r
k
s
>
j
a
v
a
C
l
i
e
n
t
a
r
p
E
n
t
e
r
t
h
e
L
o
g
i
c
a
l
a
d
d
r
e
s
s
(
I
P
)
:
1
6
5
.
1
6
5
.
8
0
.
8
0
The Physical Address is: 6A:08:AA:C2
Client:
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
c
l
a
s
s
C
l
i
e
n
t
r
a
r
p
1
2
{
p
u
b
li
c
s
t
a
ti
c
v
o
i
d
m
a
i
n
(
S
tr
i
n
g
a
r
g
s
[
]
)
{
t
r
y
{
DatagramSocket client=new DatagramSocket();
InetAddress
addr=InetAddress.g
etByName("127.0.0
.1");
byte[]
sendbyte=new
byte[1024];
byte[] receivebyte=new byte[1024];
BufferedReader
in=new
BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the Physical
address (MAC):")
.length,addr,1309);
client.send(sender);
DatagramPacket receiver=new
DatagramPacket(receivebyte,receivebyte.length);
client.receive(receiver);
String s=new String(receiver.getData());
System.out.println("The
Logical Address is(IP):
"+s.trim());
client.close();
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
}
}
Server:
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
c
l
a
s
s
S
e
r
v
e
r
r
a
r
p
1
2
{
p
u
b
li
c
s
t
a
ti
c
v
o
i
d
m
a
i
n
(
S
tr
i
n
g
a
r
g
s
[
]
)
{
t
r
y
{
DatagramSocket
server=new
DatagramSocket(
1309);
while(true)
{
byte[] sendbyte=new byte[1024];
byte[] receivebyte=new byte[1024];
DatagramPacket receiver=new
DatagramPacket(receivebyte,receivebyte.len
gth);
server.receive(receiver);
String
str=ne
w
String
(recei
ver.ge
tData(
));
String
s=str.t
rim();
Ine
tA
ddr
ess
add
r=r
ece
ive
r.ge
tA
ddr
ess
();
int
por
t=r
ece
ive
r.ge
tPo
rt()
;
String
ip[]
={"
165.
165.
80.8
0","
165.
165.
79.1
"};
Stri
ng
mac[]=
{"6A:0
8:AA:C
2","8A:
BC:E3:
FA"};
for(int
i=0;i<ip
.length;
i++)
{
i
f
(
s
.
e
q
u
a
l
s
(
m
a
c
[
i
]
)
)
{
sendbyte=ip[i].getBytes();
DatagramPacket
sender=newDatagramPacket(sendbyte,sendbyt
e.length,addr,port);
server.send(sender);
break;
}
}
b
r
e
a
k
;
}
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
}
Output:
I
:
\
e
x
>
j
a
v
a
S
e
r
v
e
r
r
a
r
p
1
2
I
:
\
e
x
>
j
a
v
a
C
l
i
e
n
t
r
a
r
p
1
2
E
n
t
e
r
t
h
e
P
h
y
s
i
c
a
l
a
d
d
r
e
s
s
(
M
A
C
)
:
6
A
:
0
8
:
A
A
:
C
2
The Logical Address is(IP): 165.165.80.80
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
c
l
a
s
s
p
i
n
g
s
e
r
v
e
r
{
p
u
b
li
c
s
t
a
ti
c
v
o
i
d
m
a
i
n
(
S
tr
i
n
g
a
r
g
s
[
]
)
{
t
r
y
{
String str;
System.out.print("
Enter the IP
Address to be Ping
: ");
BufferedReader
buf1=new
BufferedReader(ne
w
InputStreamReader
(System.in));
String ip=buf1.readLine();
R
u
n
t
i
m
e
H
=
R
u
n
t
i
m
e
.
g
e
t
R
u
n
t
i
m
e
(
)
;
P
r
o
c
e
s
s
p
=
H
.
e
x
e
c
(
"
p
i
n
g
"
+
i
p
)
;
I
n
p
u
t
S
t
r
e
a
m
i
n
=
p
.
g
e
t
I
n
p
u
t
S
t
r
e
a
m
(
)
;
BufferedRea
der
buf2=new
BufferedRea
der(new
InputStream
Reader(in));
w
h
i
l
e
(
(
s
t
r
=
b
u
f
2
.
r
e
a
d
L
i
n
e
(
)
)
!
=
n
u
l
l
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
"
"
+
s
t
r
)
;
}
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
.
g
e
t
M
e
s
s
a
g
e
(
)
)
;
}
}
}
Output:
Enter the IP address to the ping:192.168.0.1
Pinging 192.168.0.1: with bytes of data =32
Reply
from
192.168.0.11:bytes=
32 time<1ms TTL
=128
Reply
from
192.168.0.11:bytes=
32 time<1ms TTL
=128
Reply
from
192.168.0.11:bytes=
32 time<1ms TTL
=128
Reply
from
192.168.0.11:bytes=
32 time<1ms TTL
=128
Ping statistics for 192.168.0.1
Packets:
sent=4,received=4,lost=0(0%
loss),approximate round trip time in milli seconds:
Minimum=1
ms,maximum=4ms,average=2ms
e
r
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
A
r
r
a
y
L
i
s
t
;
i
m
p
o
r
t
t
r
a
c
e
r
o
u
t
e
.
T
r
a
c
e
r
o
u
t
e
I
t
e
m
;
p
u
b
l
i
c
a
b
s
t
r
a
c
t
c
l
a
s
s
T
r
a
c
e
r
o
u
t
e
{
private Runtime run;
p
u
b
l
i
c
T
r
a
c
e
r
o
u
t
e
(
)
{
r
u
n
=
R
u
n
t
i
m
e
.
g
e
t
R
u
n
t
i
m
e
(
)
;
}
public
ArrayList<TracerouteItem>
traceroute(String
destination)
{
ArrayList<TracerouteItem> result =
new ArrayList<TracerouteItem>();
Process pr = null;
String cmd = getTracerouteCommand(destination);
try
{
p
r
=
r
u
n
.
e
x
e
c
(
c
m
d
)
;
}
c
a
t
c
h
(
I
O
E
x
c
e
p
t
i
o
n
e
)
{
}
e.printStackTrace();
t
r
y
{
w
h
il
e
((
li
n
e
=
b
u
f.
r
e
a
d
L
i
n
e
()
)
!
=
n
u
ll
)
{
T
r
a
c
e
r
o
u
t
e
I
t
e
m
i
t
e
m
=
p
a
r
s
e
(
l
i
n
e
)
;
r
e
s
u
l
t
.
a
d
d
(
i
t
e
m
)
;
}
}
catch(IOException e)
{
}
return null;
r
e
t
u
r
n
r
e
s
u
l
t
;
}
public abstract TracerouteItem parse(String line);
public
abstract
String
getTracerouteCommand(String
destination);
}
Output
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
a
w
t
.
i
m
a
g
e
.
*
;
i
m
p
o
r
t
j
a
v
a
x
.
i
m
a
g
e
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
imp
ort
java
.awt
.ima
ge.
Buf
fere
dIm
age;
imp
ort
java
.io.
Byt
eAr
ray
Out
put
Stre
am;
imp
ort
java
.io.
File
;
i
m
p
o
r
t
j
a
v
a
.
i
o
.
I
O
E
x
c
e
p
t
i
o
n
;
i
m
p
o
r
t
j
a
v
a
x
.
i
m
a
g
e
i
o
.
I
m
a
g
e
I
O
;
b
y
t
e
[
]
b
y
t
e
s
=
b
a
o
s
.
t
o
B
y
t
e
A
r
r
a
y
(
)
;
b
a
o
s
.
c
l
o
s
e
(
)
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
a
w
t
.
i
m
a
g
e
.
*
;
i
m
p
o
r
t
j
a
v
a
x
.
i
m
a
g
e
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
x
.
s
w
i
n
g
.
*
;
class Server {
public static void
main(String args[])
throws
Exception{ ServerS
ocket server=null;
Socket socket;
server=new ServerSocket(4000);
System.out.println("Server Waiting for image");
socket=server.accept();
System.out.println("Client
connected."); InputStream
in
=
socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
System.out.println("Image Size: " + len/1024 +
"KB"); byte[] data = new byte[len];
dis.readFully(data);
dis.close();
in.close();
InputStream
ian = new
ByteArrayInpu
tStream(data);
BufferedImage
bImage
=
ImageIO.read(i
an);
JFrame f = new JFrame("Server");
ImageI
con
icon =
new
ImageI
con(bI
mage);
JLabel l
= new
JLabel(
);
l.setIcon(icon);
f.add(l);
f.pack();
f
.
s
e
t
V
i
s
i
b
l
e
(
t
r
u
e
)
;
Output
When you run the client code, following output screen would appear on
client side.
v
a
.
r
m
i
.
*
;
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
r
m
i
.
s
e
r
v
e
r
.
*
;
p
u
b
l
i
c
c
l
a
s
s
c
l
i
e
n
t
r
p
c
{
p
u
b
l
i
c
s
t
a
t
i
c
v
o
i
d
m
a
i
n
(
S
t
r
i
n
g
a
r
g
[
]
)
{
t
r
y
{
String
serverur
l="rmi://
localhos
t/serverr
pc"; ucet
ob=(uce
t)
Naming.
lookup(s
erverurl)
; int
r=ob.fun
ction(10
,5);
System.out.pr
intln("the
answer
of(a+b)^2
is:"+r); int t
=ob.power(1
0,5);
System.out.
println("the
answer
of(a)^(b)
is:"+t);
double
d=ob.log(10
);
System.out.println("the log value
of the given number "+10+"
is
:"+d);
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
System.o
ut.println(
"error.."+
e.getMess
age());
}
}
Server:
import java.rmi.*;
i
m
p
o
r
t
j
a
v
a
.
r
m
i
.
s
e
r
v
e
r
.
*
;
p
u
b
l
i
c
c
l
a
s
s
s
e
r
v
e
r
r
p
c
{
p
u
b
l
i
c
s
t
a
t
i
c
v
o
i
d
m
a
i
n
(
S
t
r
i
n
g
a
r
g
[
]
)
{
t
r
y
{
rpc ob=new rpc();
N
a
m
i
n
g
.
r
e
b
i
n
d
(
"
s
e
r
v
e
r
r
p
c
"
,
o
b
)
;
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
}
}}
RPC:
import java.rmi.*;
i
m
p
o
r
t
j
a
v
a
.
l
a
n
g
.
M
a
t
h
.
*
;
i
m
p
o
r
t
j
a
v
a
.
r
m
i
.
s
e
r
v
e
r
.
*
;
public class rpc extends
UnicastRemoteObject
implements
ucet
{
p
u
b
l
i
c
r
p
c
(
)
t
h
r
o
w
s
E
x
c
e
p
t
i
o
n
{
}
public
int
function(int a,int
b)throws
RemoteException
{
int m;
m
=
(
a
*
a
)
+
(
b
*
b
)
+
(
2
*
a
*
b
)
;
r
e
t
u
r
n
m
;
}
public
int
power(int a,int
b)throws
RemoteExceptio
n
{
i
n
t
m
=
(
i
n
t
)
M
a
t
h
.
p
o
w
(
a
,
b
)
;
r
e
t
u
r
n
m
;
}
public
double
log(int
a)throws
RemoteExce
ption
{
r
e
t
u
r
n
(
M
a
t
h
.
l
o
g
(
a
)
)
;
}
}
Ucet:
import java.rmi.*;
p
u
b
li
c
i
n
t
e
r
f
a
c
e
u
c
e
t
e
x
t
e
n
d
s
R
e
m
o
t
e
{
public
int
function(int
a,int
b)throws
RemoteException;
public int power(int
a,int
b)throws
RemoteException;
public double
log(int
a)throws
RemoteExcep
tion;
}
Output:
J
a
v
a
c
u
c
e
t
.
j
a
v
a
S
t
a
r
t
r
m
i
r
e
g
i
s
t
r
y
J
a
v
a
c
r
p
c
.
j
a
v
a
r
m
i
r
p
c
j
a
v
a
c
c
l
i
e
n
t
r
p
c
.
j
a
v
a
j
a
v
a
c
s
e
r
v
e
r
r
p
c
.
j
a
v
a
j
a
v
a
c
r
p
c
.
j
a
v
a
javac ucet.java
j
a
v
a
c
s
e
r
v
e
r
r
p
c
.
j
a
v
a
j
a
v
a
s
e
r
v
e
r
r
p
c
j
a
v
a
c
c
l
i
e
n
t
r
p
c
.
j
a
v
a
j
a
v
a
c
l
i
e
n
t
r
p
c
the ans of (a+b)^2 is:225
the ans of (a)^(b) is :100000
the log value of the given number 10 is 2.30258
c
l
a
s
s
S
u
b
n
e
t
{
public static void main(String args[])
{
Scan
ner
sc =
new
Scan
ner(S
yste
m.in)
;
Syste
m.ou
t.prin
t(En
ter
the
ip
addr
ess)
;
Strin
g ip
=
sc.ne
xtLin
e();
String split_ip[]=ip.split(\\.);
//SPlit the string after every .
String split_bip[] = new String[4];
//split binary ip
String bip = ;
for(int i=0;i<4;i++){
split_bip[i] =
appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
// 18 => 18 => 10010=>00010010
bip += split_bip[i];
}
System.out.println(IP in binary is +bip);
System.out.print
(Enter
the
number
of
addresses: ); int
n = sc.nextInt();
//Calculation of mask
int bits = (int)Math.ceil(Math.log(n)/Math.log(2));
/*eg if address = 120, log 120/log 2 gives log to the
base 2 => 6.9068, ceil gives us upper
integer */
System.out.println(Number of bits required for
address = +bits); int mask = 32-bits;
System.out.println(The subnet mask is =
+mask);
//Calculation of first address and last
address int fbip[] = new int[32];
for(int i=0; i<32;i++) fbip[i] = (int)bip.charAt(i)-48;
//convert cahracter 0,1 to
integer
0,1
for(int
i=31;i>31-bits;i-)
//Get
first address by ANDing
last n bits with 0
fbip[i] &= 0;
S
t
r
i
n
g
f
i
p
[
]
=
{
}
;
f
o
r
(
i
n
t
i
=
0
;
i
<
3
2
;
i
+
+
)
fip
[i/
8]
=
ne
w
Str
ing
(fi
p[i
/8]
+f
bip
[i])
;
Sy
ste
m.
out
.pr
int
(
Fir
st
ad
dre
ss
is
=
);
for
(in
t
i=
0;i
<4
;i+
+)
{
System
.out.pri
nt(Inte
ger.par
seInt(fi
p[i],2))
; if(i!
=3)
System
.out.pri
nt(.);
}
System.out.println();
int lbip[] = new int[32];
for(int i=0; i<32;i++) lbip[i] = (int)bip.charAt(i)48; //convert cahracter 0,1 to integer 0,1
for(int i=31;i>31-bits;i-) //Get last address by ORing
last n bits with 1
lbip[i] |= 1;
S
t
r
i
n
g
l
i
p
[
]
=
{
}
;
f
o
r
(
i
n
t
i
=
0
;
i
<
3
2
;
i
+
+
)
l
i
p
[
i
/
8
]
=
n
e
w
S
t
r
i
n
g
(
l
i
p
[
i
/
8
]
+
l
b
i
p
[
i
]
)
;
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
(
a
s
t
a
d
d
r
e
s
s
i
s
=
)
;
f
o
r
(
i
n
t
i
=
0
;
i
<
4
;
i
+
+
)
{
Syste
m.out
.print
(Integ
er.par
seInt(
lip[i],
2));
if(i!
=3)
Syste
m.out
.print
(.);
}
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
)
;
}
static String appendZeros(String s){
St
ri
ng
te
m
p
=
ne
w
St
ri
ng
(
00
00
00
00
);
ret
ur
n
te
m
p.
su
bs
tri
ng
(s.
le
ng
th
())
+
s;
}
}
Output
Enter the ip address: 100.110.150.10
IP in binary is
011001000110111
010010110000010
10
Enter
the
number
of
addresses: 7
Nu
mb
er
of
bits
req
uire
d
for
add
ress
= 3
The
sub
net
mas
k is
=
29
F
i
r
s
t
a
d
d
r
e
s
s
i
s
=
1
0
0
.
1
1
0
.
1
5
0
.
8
L
a
s
t
a
d
d
r
e
s
s
i
s
=
1
0
0
.
1
1
0
.
1
5
0
.
1
5
EchoServer.java
import java.net.*;
import java.io.*;
p
u
b
l
i
c
c
l
a
s
s
E
S
e
r
v
e
r
{
p
u
b
li
c
s
t
a
ti
c
v
o
i
d
m
a
i
n
(
S
tr
i
n
g
a
r
g
s
[
]
)
{
S
e
r
v
e
r
S
o
c
k
e
t
s
=
n
u
l
l
;
S
t
r
i
n
g
l
i
n
e
;
D
a
t
a
I
n
p
u
t
S
t
r
e
a
m
i
s
;
P
r
i
n
t
S
t
r
e
a
m
p
s
;
S
o
c
k
e
t
c
=
n
u
l
l
;
t
r
y
{
s
=
n
e
w
S
e
r
v
e
r
S
o
c
k
e
t
(
9
0
0
0
)
;
}
c
a
t
c
h
(
I
O
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
t
r
y
{
c=s.accept();
is=new
DataInput
Stream(c.
getInputSt
ream());
ps=new
PrintStrea
m(c.getOu
tputStrea
m());
while(true
)
{
line=is.readLine();
p
s
.
p
r
i
n
t
l
n
(
l
i
n
e
)
;
}
}
c
a
t
c
h
(
I
O
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
}
}
EClient.java
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
p
u
b
l
i
c
c
l
a
s
s
E
C
l
i
e
n
t
{
p
u
b
l
i
c
s
t
a
t
i
c
v
o
i
d
m
a
i
n
(
S
t
r
i
n
g
a
r
g
[
]
)
{
S
o
c
k
e
t
c
=
n
u
l
l
;
S
t
r
i
n
g
l
i
n
e
;
D
a
t
a
I
n
p
u
t
S
t
r
e
a
m
i
s
,
i
s
1
;
P
r
i
n
t
S
t
r
e
a
m
o
s
;
try
{
}
c
a
t
c
h
(
I
O
E
x
c
e
p
t
i
o
InetAddre
ss ia =
InetAddre
ss.getLoc
alHost();
c=new
Socket(ia,
9000);
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
t
r
y
{
os=new PrintStream(c.getOutputStream());
is=new DataInputStream(System.in);
is1=new
DataInputS
tream(c.get
InputStrea
m());
while(true)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
"
C
l
i
e
n
t
:
"
)
;
l
i
n
e
=
i
s
.
r
e
a
d
L
i
n
e
(
)
;
os.println(line);
System.out
.println("Server:"
+ is1.readLine());
}}
c
a
t
c
h
(
I
O
E
x
c
e
p
t
i
o
n
e
)
{
System.out.println("Socket Closed!");
}
}
O
u
t
p
u
t
S
e
r
v
e
r
C:\Program
Files\Java\jdk1.5.0\
bin>javac
EServer.java Note:
EServer.java uses
or overrides a
deprecated API.
Note: Recompile
with -deprecation
for details.
C:\Program
Files\Java\jdk1.5.0\
bin>java EServer
C:\Program
Files\Java\jdk1.5.0\
bin>
Client
C:\Program
Files\Java\jdk1.5.0
\bin>javac
EClient.java
Note: EClient.java
uses or overrides a
deprecated API.
Note: Recompile
with -deprecation
for details.
C:\Program
Files\Java\jdk1.5.0
\bin>java EClient
Client:
Hai Server
S
e
r
v
e
r
:
H
a
i
S
e
r
v
e
r
C
l
i
e
n
t
:
Hello
S
e
r
v
e
r
:
H
e
l
l
o
C
l
i
e
n
t
:
end
S
e
r
v
e
r
:
e
n
d
C
l
i
e
n
t
:
ds
Socket Closed!
B.Chat
Aim
Write a Program client -server
application for chat using UDP
Sockets
Program
UDPserver.java
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
c
l
a
s
s
U
D
P
s
e
r
v
e
r
{
(),0,p.getLength
());
System.out.print
ln("Client:"
+
psx);
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
"
S
e
r
v
e
r
:
"
)
;
S
t
r
i
n
g
s
t
r
=
d
i
s
.
r
e
a
d
L
i
n
e
(
)
;
i
f
(
s
t
r
.
e
q
u
a
l
s
(
"
e
n
d
"
)
)
b
r
e
a
k
;
b
u
f
f
e
r
=
s
t
r
.
g
e
t
B
y
t
e
s
(
)
;
d
s
.
s
e
n
d
(
n
e
w
DatagramPack
et(buffer,str.le
ngth(),ia,serve
rport));
}
}
}
UDPclient.java
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
c
l
a
s
s
U
D
P
c
l
i
e
n
t
{
i
e
n
t
:
"
)
;
S
t
r
i
n
g
s
t
r
=
d
i
s
.
r
e
a
d
L
i
n
e
(
)
;
i
f
(
s
t
r
.
e
q
u
a
l
s
(
"
e
n
d
"
)
)
b
r
e
a
k
;
buffer=str.getBytes();
ds.send(new
DatagramPacket(buffer,str.l
ength(),ia,clientport));
DatagramPacket
p=new
DatagramPacket(buffer,buf
fer.length);
ds.receive(p);
String psx=new
String(p.getData
(),0,p.getLength
());
System.out.print
ln("Server:" +
psx);
}
}
}
O
U
T
P
U
T
:
S
e
r
v
e
r
C:\Program Files\Java\jdk1.5.0\bin>javac UDPserver.java
C:\Program
Files\Java\jdk1.
5.0\bin>java
UDPserver press
Server:
Hello Client
C
l
i
e
n
t
:
H
o
w
a
r
e
Y
o
u
S
e
r
v
e
r
:
I am Fine
Client
C:\Program Files\Java\jdk1.5.0\bin>javac UDPclient.java
C:\Program
Files\Java\jdk1.
5.0\bin>java
UDPclient
server waiting
Client:
Hai Server
S
e
r
v
e
r
:
H
e
l
l
o
C
l
i
e
C
l
i
e
n
t
:
How are You
S
e
r
v
e
r
:
I
a
m
F
i
n
e
C
l
i
e
n
t
:
end
C. File Transfer
Program
File Client
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
class Clientfile
{
{
T
r
y
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in)); Socket clsct=new
Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream
dout=new
DataOutputStream(clsct.getOutputStr
eam()); System.out.println("Enter the
file name:");
String str=in.readLine();
dout.writeBytes(str+'\n');
System.out.
println("En
ter the new
file
name:");
String
str2=in.rea
dLine();
String str1,ss;
FileWriter f=new
F
i
l
e
W
r
i
t
e
r
(
s
t
r
2
)
;
c
h
a
r
b
u
f
f
e
r
[
]
;
w
h
i
l
e
(
t
r
u
e
)
{ str1=din.readLine();
if(str1.equals("-1")) break;
System.out.println(str1);
buffer=new char[str1.length()];
str
1.
ge
tC
ha
rs(
0,s
tr1
.le
ng
th(
),b
uff
er,
0);
f.
wr
ite
(b
uff
er)
;
}
f.close();
c
l
s
c
t
.
c
l
o
s
e
(
)
;
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
}
}
Server
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
c
l
a
s
s
S
e
r
v
e
r
f
i
l
e
{p
ubl
ic
sta
tic
voi
d
ma
in(
Str
ing
arg
s[]
)
{
T
r
y
{
Server
Socket
obj=ne
w
Server
Socket
(139);
while(t
rue)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream
dout=new
DataOutputStream(obj1.getOutputStre
am()); String str=din.readLine();
FileReader f=new FileReader(str);
Buffere
dReade
r
b=new
Buffere
dReade
r(f);
String
s;
w
h
i
l
e
(
(
s
=
b
.
r
e
a
d
L
i
n
e
(
)
)
!
=
n
u
l
l
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
s
)
;
d
o
u
t
.
w
r
i
t
e
B
y
t
e
s
(
s
+
'
\
n
'
)
;
}
f.close();
dout.writeBytes("-1\n");
}
}
catch(Exception e)
{
System.out.println(e);}
}
Output
File content
C
o
m
p
u
t
e
r
n
e
t
w
o
r
k
s
j
h
f
c
g
s
a
u
f
jbsdava
j
b
v
u
e
s
a
g
v
c
l
i
e
n
t
E
n
t
e
r
t
h
e
f
i
l
e
n
a
m
e
:
s
a
m
p
l
e
.
t
x
t
server
C
o
m
p
u
t
e
r
n
e
t
w
o
r
k
s
j
h
f
c
g
s
a
u
f
jbsdava
jbvuesagv
client
E
n
t
e
r
t
h
e
n
e
w
f
i
l
e
n
a
m
e
:
n
e
t
.
t
x
t
C
o
m
p
u
t
e
r
n
e
t
w
o
r
k
s
j
h
f
c
g
s
a
u
f
jbsdava
jbvuesagv
Destination file
C
o
m
p
u
t
e
r
n
e
t
w
o
r
k
s
j
h
f
c
g
s
a
u
f
jbsdava
jbvuesagv
U
d
p
d
n
s
s
e
r
v
e
r
.
j
a
v
a
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
p
u
b
l
i
c
c
l
a
s
s
u
d
p
d
n
s
s
e
r
v
e
r
private static
int
indexOf(Strin
g[]
array,
String str)
{
str = str.trim();
f
o
r
(
i
n
t
i
=
0
;
i
<
a
r
r
a
y
.
l
e
n
g
t
h
;
i
+
+
)
{
i
f
(
a
r
r
a
y
[
i
]
.
e
q
u
a
l
s
(
s
t
r
)
)
r
e
t
u
r
n
i
;
}
r
e
t
u
r
n
1
;
}
public static void
main(String
arg[])throws
IOException
{
String[] hosts = {"yahoo.com", "gmail.com","cricinfo.com",
"facebook.com"};
String[]
ip
=
{"68.180.206.184",
"209.85.148.19","80.168.92.140",
"69.63.189.16"}; System.out.println("Press Ctrl
+ C to Quit");
while (true)
{
DatagramSocket
serversocket=new
DatagramSocket(1362);
byte[] senddata = new
byte[1021];
byte[] receivedata = new byte[1021];
DatagramPacket
recvpack
=
DatagramPacket(receivedata,
receivedata.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddre
ss
ipaddress
=
recvpack.g
etAddress(
); int port
=
recvpack.g
etPort();
String capsent;
System.o
ut.println(
"Request
for host "
+
sen);
if(indexOf
(hosts,
sen) != -1)
c
a
p
s
e
n
t
new
=
i
p
[
i
n
d
e
x
O
f
(
h
o
s
t
s
,
s
e
n
)
]
;
e
l
s
e
c
a
p
s
e
n
t
=
"
H
o
s
t
N
o
t
F
o
u
n
d
"
;
s
e
n
d
d
a
t
a
=
c
a
p
s
e
n
t
.
g
e
t
B
y
t
e
s
(
)
;
DatagramPacket pack = new DatagramPacket
(senddata,
senddata.length,ipaddress,port);
serversocket.send(pack);
s
e
r
v
e
r
s
o
c
k
e
t
.
c
l
o
s
e
(
)
;
}
}
}
/
/
U
D
P
D
N
S
C
l
i
e
n
t
U
d
p
d
n
s
c
l
i
e
n
t
.
j
a
v
a
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
p
u
b
l
i
c
c
l
a
s
s
u
d
p
d
n
s
c
l
i
e
n
t
{
public static void
main(String
args[])throws
IOException
{
BufferedReader
br
=
BufferedReader(new
InputStreamReader(System.in));
DatagramSocket clientsocket =
DatagramSocket();
new
new
InetAddress ipaddress;
i
f
(
a
r
g
s
.
l
e
n
g
t
h
=
=
0
)
i
p
a
d
d
r
e
s
s
=
InetAddress.getLocalHost(); else
ipaddress
=
InetAddre
ss.getByN
ame(args[
0]); byte[]
senddata
=
new
byte[1024
];
b
y
t
e
[
]
r
e
c
e
i
v
e
d
a
t
a
=
n
e
w
b
y
t
e
[
1
0
2
4
];
i
n
t
p
o
rt
a
d
d
r
=
1
3
6
2
;
Syste
m.out.
print("
Enter
the
hostna
me :
");
String
senten
ce =
br.rea
dLine(
);
Senddata = sentence.getBytes();
DatagramPacket
pack
=
DatagramPacket(senddata,senddata.length,
ipaddress,portaddr); clientsocket.send(pack);
DatagramPacket
recvpack
=new
DatagramPacket(receivedata,receivedata.lengt
h); clientsocket.receive(recvpack);
String
modified =
new
String(recvpac
k.getData());
System.out.pri
ntln("IP
Address: " +
modified);
clientsocket.cl
ose();
}
}
O
U
new
T
P
U
T
S
e
r
v
e
r
j
a
v
a
c
u
d
p
d
n
s
s
e
r
v
e
r
.
j
a
v
a
j
a
v
a
u
d
p
d
n
s
s
e
r
v
e
r
Press Ctrl + C
to
Quit
Request
for
host
yahoo.com
Request
for
host
cricinfo.com
Request for host youtube.com
Client
j
a
v
a
c
u
d
p
d
n
s
c
l
i
e
n
t
.
j
a
v
a
j
a
v
a
u
d
p
d
n
s
c
l
i
e
n
t
E
n
t
e
r
t
h
e
h
o
s
t
n
a
m
e
:
y
a
h
o
o
.
c
o
m
I
P
A
d
d
r
e
s
s
:
6
8
.
1
8
0
.
2
0
6
.
1
8
4
j
a
v
a
u
d
p
d
n
s
c
l
i
e
n
t
E
n
t
e
r
t
h
e
h
o
s
t
n
a
m
e
:
c
r
i
c
i
n
f
o
.
c
o
m
I
P
A
d
d
r
e
s
s
:
8
0
.
1
6
8
.
9
2
.
1
4
0
j
a
v
a
u
d
p
d
n
s
c
l
i
e
n
t
E
n
t
e
r
t
h
e
h
o
s
t
n
a
m
e
:
y
o
u
t
u
b
e
.
c
o
m
I
P
A
d
d
r
e
s
s
:
H
o
s
t
N
o
t
F
o
u
n
d
B.SNMP
Aim
To write a java program for SNMP application program
Algorithm
1.Start the program.
2.Get the frame size from the user
3.To create the frame based on the user request.
4.To send frames to server from the client side.
5.If your frames reach the server it will send
ACK signal to client otherwise it will send
NACK signal to client.
6.Stop the program
Program
import java.io.IOException;
im
po
rt
or
g.s
n
m
p4
j.
C
o
m
m
un
ity
Ta
rg
et;
im
po
rt
or
g.s
n
m
p4
j.P
D
U;
i
m
p
o
r
t
o
r
g
.
s
n
m
p
4
j
.
S
n
m
p
;
i
m
p
o
r
t
o
r
g
.
s
n
m
p
4
j
.
T
a
r
g
e
t
;
import org.snmp4j.TransportMapping;
impo
rt
org.s
nmp4
j.eve
nt.Re
spons
eEve
nt;
impo
rt
org.s
nmp4
j.mp.
Snmp
Const
ants;
impo
rt
org.s
nmp4
j.smi.
Addr
ess;
im
po
rt
or
g.s
n
m
p4
j.s
mi
.G
en
eri
cA
dd
res
s;
im
po
rt
or
g.s
n
m
p4
j.s
mi
.O
ID
;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
*/
p
u
b
l
i
c
S
N
M
P
M
a
n
a
g
e
r
(
S
t
r
i
n
g
a
d
d
)
{
address = add;
TransportMapping transport =
new
DefaultUdpTransportMapping();
snmp = new
Snmp(transport);
/
/
D
o
n
o
t
f
o
r
g
e
t
t
h
i
s
l
i
n
e
!
t
r
a
n
s
p
o
r
t
.
l
i
s
t
e
n
(
)
;
}
/**
* Method which takes a single OID and returns the response from the
agent as a String.
* @param oid
* @return
* @throws IOException
*/
a
b
l
e
B
i
n
d
i
n
g
(
o
i
d
)
)
;
}
pdu.setType(PDU.GET);
ResponseEvent event
=
snmp.send(pdu,
getTarget(),
null);
if(event != null) {
return event;
}
throw
new
RuntimeExce
ption("GET
timed out");
}
/**
* This method returns a Target, which contains information about
* where the data should be fetched and how.
* @return
*/
private Target getTarget() {
Address
targetAddress
=
GenericAddress.pars
e(address);
CommunityTarget
target
=
new
CommunityTarget();
target.setCom
munity(new
OctetString("
public"));
target.setAddr
ess(targetAdd
ress);
target.setRetries(2);
target.setTimeout(1500);
target.set
Version(S
nmpCons
tants.vers
ion2c);
return
target;
}}
OUT PUT
Hardware: x86 Family 6 Model 23 Stepping 10
AT/AT COMPATIBLE - Software: Windows
2000 Version 5.1 (Build 2600 Multiprocessor
Free)
C. File Transfer
AIM
To write a java program for applaction using TCP and UDP Sockets
Links
Program
F
i
l
e
C
l
i
e
n
t
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
;
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
c
l
a
s
s
C
l
i
e
n
t
f
i
l
e
{
{
T
r
y
{
BufferedReader in=new BufferedReader(new
InputStreamReader(System.in)); Socket clsct=new
Socket("127.0.0.1",139);
DataInputStream din=new DataInputStream(clsct.getInputStream());
DataOutputStream
dout=new
DataOutputStream(clsct.getOutputStr
eam()); System.out.println("Enter the
file name:");
String str=in.readLine();
dout.writeBytes(str+'\n');
System.out.
println("En
]
;
w
h
i
l
e
(
t
r
u
e
)
{ str1=din.readLine();
if(str1.equals("-1")) break;
System.out.println(str1);
buffer=new char[str1.length()];
str
1.
ge
tC
ha
rs(
0,s
tr1
.le
ng
th(
),b
uff
er,
0);
f.
wr
ite
(b
uff
er)
;
}
f.close();
c
l
s
c
t
.
c
l
o
s
e
(
)
;
}
c
a
t
c
h
(
E
x
c
e
p
t
i
o
n
e
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
e
)
;
}
}
}
Server
i
m
p
o
r
t
j
a
v
a
.
i
o
.
*
;
i
m
p
o
r
t
j
a
v
a
.
n
e
t
.
*
i
m
p
o
r
t
j
a
v
a
.
u
t
i
l
.
*
;
c
l
a
s
s
S
e
r
v
e
r
f
i
l
e
{p
ubl
ic
sta
tic
voi
d
ma
in(
Str
ing
arg
s[]
)
{
T
r
y
{
ServerSocket
obj=new
ServerSocket(139);
while(true)
{
Socket obj1=obj.accept();
DataInputStream din=new DataInputStream(obj1.getInputStream());
DataOutputStream
dout=new
DataOutputStream(obj1.getOutputStre
am()); String str=din.readLine();
F
i
l
e
R
e
a
d
e
r
f
=
n
e
w
F
i
l
e
R
e
a
d
e
r
(
s
t
r
)
;
B
u
f
f
e
r
e
d
R
e
a
d
e
r
b
=
n
e
w
BufferedReader(f); String s;
w
h
i
l
e
(
(
s
=
b
.
r
e
a
d
L
i
n
e
(
)
)
!
=
n
u
l
l
)
{
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
s
)
;
d
o
u
t
.
w
r
i
t
e
B
y
t
e
s
(
s
+
'
\
n
'
)
;
}
f.close();
dout.writeBytes("-1\n");
}
}
catch(Exception e)
{
System.out.println(e);}
}
Output
File
conten
t
Comp
uter
netwo
rks
jhfcgs
auf
jbsdav
a
jbvues
agv
client
E
n
t
e
r
t
h
e
f
i
l
e
n
a
m
e
:
s
a
m
p
l
e
.
t
x
t
s
e
r
v
e
r
C
o
m
p
u
t
e
r
n
e
t
w
o
r
k
s
j
h
f
c
g
s
a
u
f
j
b
s
d
a
v
a
j
b
v
u
e
s
a
g
v
c
l
i
e
n
t
E
n
t
e
r
t
h
e
n
e
w
f
i
l
e
n
a
m
e
:
n
e
t
.
t
x
t
C
o
m
p
u
t
e
r
n
e
t
w
o
r
k
s
j
h
f
c
g
s
a
u
f
jbsdava jbvuesagv Destination file
C
o
m
p
u
t
e
r
n
e
t
w
o
r
k
s
j
h
f
c
g
s
a
u
f
j
b
s
d
a
v
a
j
b
v
u
e
s
a
g
v
i
e
w
Ns
programming: A
Quick start
Case study I:
A simple
Wireless
network
Ns Status
Periodical
release
(ns2.26,
Feb
2003)
Platform
support
Ns unctionalities
Routing,
Transportatio
n,
Traffic
sources,Queu
ing
disciplines,
QoS
Wireless
Ad
hoc
routi
ng,
mobi
le IP,
senso
rMA
C
Traci
ng,
visua
lizati
on
and
vario
us
utiliti
e
NS(
Netw
ork
Simu
lators
)
Most of the commercial simulators are GUI driven,
while some network simulators are CLI driven. The network
model / configuration describes the state of the network
(nodes,routers, switches, links) and the events (data
transmissions, packet error etc.). An important output of
simulations are the trace files. Trace files log every packet,
every event that occurred in the simulation and are used for
analysis. Network simulators can also provide other tools to
facilitate visual analysis of trends and potential trouble spots.
Most network simulators use discrete event
simulation, in which a list of pending "events" is stored, and
those events are processed in order, with some events
triggering future events such as the event of the arrival of
a packet at one node triggering the event of the arrival of
that packet at a downstream node.
Simulation of networks is a very complex task. For
example, if congestion is high, then estimation of the
average occupancy is challenging because of high variance.
To estimate the likelihood of a buffer overflow in a
network, the time required for an accurate answer can be
extremely large. Specialized techniques such as "control
variates" and "importance sampling" have been developed to
speed simulation.
Throughput
This is the main performance measure characteristic, and most
widely used.
Incommunicationnetworks, such asEthernetorpacket radio, throughputor
network throughputis
the average rate of successfulmessage delivery over a
communication channel. The throughput is usually measured
inbitsper second (bit/s orbps), andsometimes indata
packetsper second or data packets pertime slotThis measure
how soon the receiver is able to get a certain amount of data
send by the sender. It is determined as the ratio of the total
data received to the end to end delay. Throughput is an
important factor which directly impacts the network
performance
Delay
Delay is the time elapsed while a packet travels from one
point
e.g.,
source
premise
or
network
ingress to destination premise or network degrees. The larger
the
valueof
delay,
the
more
difficult
it is for transport layer protocols to maintain highbandwidths.
We will calculate end to end delay
Queue Length
A queuing system in networks can be described as
packets arriving for service, waiting for service if it is not
immediate, and if having waited for service, leaving
thesystem after being served. Thus queue length is very
important characteristic to determine that how well the
active queue management of the congestion control
algorithm has been working.
ii. Flooding
Flooding s a simple routing algorithm in which every
incoming
packet
is
sent
through
every
outgoing link except the one it arrived on.Flooding is used
in
bridging
and
in
systems
such
as
Usenet and peer-to-peer file sharing and as part of some
routing
protocols,
including
OSPF,
DVMRP, and those used in ad-hoc wireless networks.There
are
generally
two
types
of
flooding
available,
Uncontrolled
Flooding
and
Controlled
Flooding.Uncontrolled Flooding is the fatal law
of flooding. All nodes have neighbours and route packets
indefinitely.
More
than
two
neighbours
creates a broadcast storm.
Controlled Flooding has its own two algorithms to make it
reliable,
SNCF
(Sequence
Number
Controlled Flooding) and RPF (Reverse Path Flooding). In
SNCF,
the
node
attaches
its
own
address and sequence number to the packet, since every
node
has
a
memory
of
addresses
and
sequence numbers. If it receives a packet in memory, it
drops
it
immediately
while
in
RPF, the
node will only send the packet forward. If it is received from
the next node, it sends it back to the
sender.
Algorithm
There are several variants of flooding algorithm. Most work roughly as
follows:
1. Each node acts as both a transmitter and a receiver.
2. Each node tries to forward every message to every one
of
its
neighbours
except
the
source
node.
This results in every message eventually being delivered to all reachable
parts of the network.
Advantages
Disadvantages
requiring
an
increase
in
processing complexity to
disregard duplicate messages.
Duplicate packets may circulate forever, unless certain precautions are taken:
Have each node keep track of every packet seen and only forward each packet once
Enforce a network topology without loops
measure of the cost to reach a certain node. The least cost route
between any two nodes is the route with minimum distance. Each
node maintains a vector (table) of minimum distance to every node.
The cost of reaching a destination is calculated using various route
metrics. RIP uses the hop count of the destination whereas IGRP takes
into account other information such as node delay and available
bandwidth.
Updates are performed periodically in a distance-vector protocol where
all or part of a router's routing table is sent to all its neighbors that are
configured to use the same distance-vector routing protocol. RIP
supports cross-platform distance vector routing whereas IGRP is a
Cisco Systems proprietary distance vector routing protocol. Once a
router has this information it is able to amend its own routing table to
reflect the changes and then inform its neighbors of the changes.
This process has been described as routing by rumor because
routers are relying on the information they receive from other routers
and cannot determine if the information is actually valid and true.
There are a number of features which can be used to help with
instability and inaccurate routing information.
EGP and BGP are not pure distance-vector routing protocols because a
distance-vector protocol calculates routes based only on link costs
whereas in BGP, for example, the local route preference value takes
priority over the link cost.
Count-to-infinity problem
The Bellman-Ford algorithm does not prevent routing loops from
happening
and
suffers
from
the
countto-infinity problem. The core of the count-to-infinity problem
is
that
if A tells
B
that
it
has
a
path
somewhere, there is no way for B to know if the path has B as a part
of
it.
To
see
the
problem
clearly,
imagine a subnet connected like A-B-C-D-E-F, and let the metric
between
the
routers
be
"number
of
jumps". Now suppose that A is taken offline. In the vector-updateprocess
B
notices
that
the
route
to
A,
which was distance 1, is down - B does not receive the vector update
from
A.
The
problem
is,
B
also
gets
an update from C, and C is still not aware of the fact that A is down so
it
tells
B
that
A
is
only
two
jumps
from C (C to B to A), which is false. This slowly propagates through
the
network
until
it
reaches
infinity
(in which case the algorithm corrects itself, due to the relaxation
property of Bellman-Ford).
Ex.no.11
Aim:
To write a ns2 program for implementing unicast routing protocol.
Algorithm:
Step 1: start the program.
Step 2: declare the global
variables ns for creating a new
simulator. Step 3: set the color
for packets.
Step 4: open the network animator file in
the name of file2 in the write mode. Step
5: open the trace file in the name of file 1
in the write mode.
Step 6: set the unicast routing protocol to
transfer the packets in network. Step 7:
create the required no of nodes.
Step 8: create the duplex-link between the nodes including the
delay
time,bandwidth
and
dropping
queue mechanism.
Step 9: give the position for the links between the nodes.
Step 10: set a tcp reno connection for source node.
Step 11: set the destination node using tcp sink.
Step 12: setup a ftp connection over the tcp connection.
Step 13: down the connection
between any nodes at a particular
time. Step 14: reconnect the
downed connection at a
particular time.
Step 15: define the finish
procedure.
Step 16: in the definition of the finish procedure
declare the global variables ns,file1,file2. Step 17:
close the trace file and namefile and execute the
network animation file.
Step 18: at the particular time call the finish
procedure.
Step 19: stop the program.
Program:
set ns [new Simulator]
#Define
different
colors for data
flows
(for
NAM)
$ns
color 1 Blue
$ns color 2 Red
#Open the Trace file
s
e
t
f
i
l
e
1
[
o
p
e
n
o
u
t
.
t
r
w
]
$
n
s
t
r
a
c
e
a
l
l
$
f
i
l
e
1
#
O
p
e
n
t
h
e
N
A
M
t
r
a
c
e
f
i
l
e
s
e
t
f
i
l
e
2
[
o
p
e
n
o
u
t
.
n
a
m
w
]
$
n
s
n
a
m
t
r
a
c
e
a
l
l
$
f
i
l
e
2
#
D
e
f
i
n
e
a
'
f
i
n
i
s
h
'
p
r
o
c
e
d
u
r
e
p
r
o
c
f
i
n
i
s
h
{
}
{
g
l
o
b
a
l
n
s
f
i
l
e
1
f
i
l
e
2
$
n
s
f
l
u
s
h
t
r
a
c
e
c
l
o
s
e
$
f
i
l
e
1
c
l
o
s
e
$
f
i
l
e
2
e
x
e
c
n
a
m
o
u
t
.
n
a
m
&
e
x
i
t
3
}
# Next line should be
commented out to have the
static routing $ns rtproto
DV
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n4 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
e
n
o
]
$
n
s
a
t
t
a
c
h
a
g
e
n
t
$
n
0
$
t
c
p
set
sin
k
[ne
w
Ag
ent
/T
CP
Sin
k/
Del
Ac
k]
$ns
atta
ch-
age
nt
$n
5
$si
nk
$
n
s
c
o
n
n
e
c
t
$
t
c
p
$
s
i
n
k
$
t
c
p
s
e
t
f
i
d
_
1
#
S
e
t
u
p
a
F
T
P
o
v
e
r
T
C
P
c
o
n
n
e
c
t
i
o
n
s
e
t
f
t
p
[
n
e
w
A
p
p
l
i
c
a
t
i
o
n
/
F
T
P
]
$
f
t
p
a
t
t
a
c
h
a
g
e
n
t
$
t
c
p
$ftp set type_ FTP
$
n
s
r
t
m
o
d
e
l
a
t
1
.
0
d
o
w
n
$
n
1
$
n
4
$
n
s
r
t
m
o
d
e
l
a
t
4
.
5
u
p
$
n
1
$
n
4
$ns at 0.1 "$ftp start"
$
n
s
a
t
6
.
0
"
f
i
n
i
s
h
"
$
n
s
r
u
n
Program:
# Create scheduler
#Create
an
event
scheduler wit
multicast
turned on set
ns
[new
Simulator
-multicast on]
#$ns multicast
#Turn on Tracing
s
e
t
t
f
[
o
p
e
n
o
u
t
p
u
t
.
t
r
w
]
$
n
s
t
r
a
c
e
a
l
l
$
t
f
# Turn on nam Tracing
s
e
t
f
d
[
o
p
e
n
m
c
a
s
t
.
n
a
m
w
]
$
n
s
n
a
m
t
r
a
c
e
a
l
l
$
f
d
# Create nodes
set n0 [$ns node]
set n1 [$ns node]
s
e
t
g
r
o
u
p
1
[
N
o
d
e
a
l
l
o
c
a
d
d
r
]
s
e
t
g
r
o
u
p
2
[
N
o
d
e
a
l
l
o
c
a
d
d
r
]
# UDP
Transpo
rt agent
for the
traffic
source
set udp0
[new
Agent/U
DP]
$
n
s
a
t
t
a
c
h
a
g
e
n
t
$
n
0
$
u
d
p
0
$
u
d
p
0
s
e
t
d
s
t
_
a
d
d
r
_
$
g
r
o
u
p
1
$
u
d
p
0
s
e
t
d
s
t
_
p
o
r
t
_
0
set
cbr1
[ne
w
App
licat
ion/
Traf
fic/
CB
R]
$cbr
1
atta
chage
nt
$ud
p0
#
Tr
an
sp
ort
ag
ent
for
the
tra
ffi
c
so
urc
e
set
ud
p1
[ne
w
Ag
ent
/U
DP
]
$
n
s
a
t
t
a
c
h
a
g
e
n
t
$
n
1
$
u
d
p
1
$
u
d
p
1
s
e
t
d
s
t
_
a
d
d
r
_
$
g
r
o
u
p
2
$
u
d
p
1
s
e
t
d
s
t
_
p
o
r
t
_
0
set
cbr2
[ne
w
App
licat
ion/
Traf
fic/
CB
R]
$cbr
2
atta
chage
nt
$ud
p1
# Create receiver
s
e
t
r
c
v
r
1
[
n
e
w
A
g
e
n
t
/
N
u
l
l
]
$
n
s
a
t
t
a
c
h
a
g
e
n
t
$
n
5
$
r
c
v
r
1
$ns at
1.0
"$n5
joingroup
$rcvr1
$group
1" set
rcvr2
[new
Agent/
Null]
$ns attach-agent $n6 $rcvr2
$ns at
1.5
"$n6
joingroup
$rcvr2
$group
1" set
rcvr3
[new
Agent/
Null]
$ns attach-agent $n7 $rcvr3
$ns at
2.0
"$n7
joingroup
$rcvr3
$group
1" set
rcvr4
[new
Agent/
Null]
$ns attach-agent $n5 $rcvr1
$ns at
2.5
"$n5
joingroup
$rcvr4
$group
2" set
rcvr5
[new
Agent/
Null]
$ns attach-agent $n6 $rcvr2
$ns at
3.0
"$n6
joingroup
$rcvr5
$group
2" set
rcvr6
[new
Agent/
Null]
$ns attach-agent $n7 $rcvr3
.
5
"
$
c
b
r
1
s
t
o
p
"
$
n
s
a
t
0
.
5
"
$
c
b
r
2
s
t
a
r
t
"
$
n
s
a
t
9
.
5
"
$
c
b
r
2
s
t
o
p
"
#post-processing
$
n
s
a
t
1
0
.
0
"
f
i
n
i
s
h
"
p
r
o
c
f
i
n
i
s
h
{
}
{
global ns tf
$
n
s
f
l
u
s
h
t
r
a
c
e
c
l
o
s
e
$
t
f
e
x
e
c
n
a
m
m
c
a
s
t
.
n
a
m
&
e
x
i
t
0
}
# For nam
#Color
s
for
packets
from
two
mcast
groups
$ns
color
10 red
$
n
s
c
o
l
o
r
1
1
g
r
e
e
n
$
n
s
c
o
l
o
r
3
0
p
u
r
p
l
e
$
n
s
c
o
l
o
r
3
1
g
r
e
e
n
# Manual layout: order of the link is significant!
#$ns duplex-link-op $n0 $n1 orient right
#$ns duplex-link-op $n0 $n2 orient right-up
#$ns duplex-link-op $n0 $n3 orient right-down
# Show queue on simplex link n0->n1
#$ns duplex-link-op $n2 $n3 queuePos 0.5
# Group 0 source
$
u
d
p
0
s
e
t
f
i
d
_
1
0
$
n
0
c
o
l
o
r
r
e
d
$n0 label "Source 1"
#
G
r
o
u
p
1
s
o
u
r
c
e
$
u
d
p
1
s
e
t
f
i
d
_
1
1
$
n
1
c
o
l
o
r
g
r
e
e
n
$
n
1
l
a
b
e
l
"
S
o
u
r
c
e
2
"
$
n
5
l
a
b
e
l
"
R
e
c
e
i
v
e
r
1
"
b
e
l
"
R
e
c
e
i
v
e
r
3
"
$
n
7
c
o
l
o
r
b
l
u
e
#
$
n
2
a
d
d
m
a
r
k
m
0
r
e
d
#
$
n
2
d
e
l
e
t
e
m
a
r
k
m
0
"
# Animation rate
$
n
s
s
e
t
a
n
i
m
a
t
i
o
n
r
a
t
e
3
.
0
m
s
$
n
s
r
u
n
Ex.no.11(c)
Aim:
To write a ns2 program for implementing carrier sense multiple access.
Algorithm:
Step 1: start the program.
Step 2: declare the global
variables ns for creating a new
simulator. Step 3: set the color
for packets.
Step 4: open the network animator file in the write mode.
Step 5: open the trace
file and the win file in
the write mode. Step 6:
transfer the packets in
network.
Step 7: create the capable no of nodes.
Program:
s
e
t
n
s
[
n
e
w
S
i
m
u
l
a
t
o
r
]
$
n
s
c
o
l
o
r
1
b
l
u
e
f
i
1
s
e
t
f
i
2
[
o
p
e
n
o
u
t
.
n
a
m
w
]
$
n
s
n
a
m
t
r
a
c
e
a
l
l
$
f
i
2
p
r
o
c
f
i
n
i
s
h
{
}
{
g
l
o
b
a
l
n
s
f
i
1
f
i
2
$
n
s
f
l
u
s
h
t
r
a
c
e
c
l
o
s
e
$
f
i
1
close $fi2
e
x
e
c
n
a
m
o
u
t
.
n
a
m
&
e
x
i
t
0
}
$si
nk
$
n
s
c
o
n
n
e
c
t
$
t
c
p
$
s
i
n
k
$
t
c
p
s
e
t
f
i
d
_
1
$tcp set window_ 8000
$tcp set packetsize_ 552
s
e
t
f
t
p
[
n
e
w
A
p
p
l
i
c
a
t
i
o
n
/
F
T
P
]
$
f
t
p
a
t
t
a
c
h
a
g
e
n
t
$
t
c
p
$
c
b
r
a
t
t
a
c
h
a
g
e
n
t
$
u
d
p
$
c
b
r
s
e
t
t
y
p
e
_
C
B
R
p
S
o
u
r
c
e
s
e
t
w
i
n
d
o
w
_
]
p
u
t
s
$
f
i
l
e
"
$
n
o
w
$
c
w
n
d
"
$ns at [expr $now+
$time] "plotwindow
$tcpSource $file"
}
$ns at 1.0 "plotwindow $tcp $winfile"
$ns at 5
"$ns
traceannotate
\"packet
drop\""
$ns
at
125.0
"finish"
$ns run
Output: