Sei sulla pagina 1di 62

Ex.No.

Implementation of ECHO Using TCP Socket

Aim: To implement echo by sending messages from client to server. Algorithm: Server: Step 1: Create a server socket and client socket Step 2: sing inp!t streams" get the message typed by the !ser. Step #: Send the message to the client$ Step %: &f the 'ord (end) is enco!ntered in the message place (end) in the o!tp!t stream then both the client and server programs exit. Client: Step 1: Create a client socket that connects to the re*!ired host and port n!mber. Step 2: sing inp!t streams read the message from the server thro!gh socket. Step #: let !serin be the variable to 'hich the message is stored. Step %: &f the !serin does)t end 'ith the 'ord (end) the print !serin Else print the message excl!ding the 'ord (end) and exit. Program: EchoServer: import +ava.io.,$ import +ava.net.,$ p!blic class EchoServer p!blic static void main.String args/01 String echoin$ ServerSocket ssock$ Socket csok$ 2!ffered3eader br$ try ssock 4 ne' ServerSocket.25551$ csok 4 ssock.accept.1$ br 4 ne' 2!ffered3eader.ne' &np!tStream3eader.csok.get&np!tStream.111$ 6rintStream ps 4 ne' 6rintStream.csok.get7!tp!tStream.11$ System.o!t.println.8Connected......for echo81$ 'hile..echoin 4 br.read9ine.11 :4 n!ll1 if.echoin.e*!als.8end811 System.o!t.println.8Client disconnected81$ br.close.1$ break$ ; else ps.println.echoin1$ ; ;

catch. nkno'n<ostException e1 System.o!t.println.e.toString.11$; catch.&7Exception ioe1 System.o!t.println.ioe1$ ; ; ; EchoClient import +ava.io.,$ import +ava.net.,$ p!blic class EchoClient p!blic static void main.String args/01 String sockin$ try Socket csok 4 ne' Socket.&net=ddress.get9ocal<ost.1"25551$ 2!ffered3eader br 4 ne' 2!ffered3eader.ne' &np!tStream3eader.System.in11$ 2!ffered3eader br>sock 4 ne' 2!ffered3eader.ne' &np!tStream3eader.csok.get&np!tStream.111$ 6rintStream ps 4 ne' 6rintStream.csok.get7!tp!tStream.11$ System.o!t.println.8Start Echoing...Type ?end? to terminate81$ 'hile..sockin 4 br.read9ine.11:4n!ll1 ps.println.sockin1$ if.sockin.e*!als.8end811 break$ else System.o!t.println.83eflected from Server:8@br>sock.read9ine.11$ ; ; catch. nkno'n<ostException e1 System.o!t.println.e.toString.11$ ; catch.&7Exception ioe1 System.o!t.println.ioe1$ ; ; ;

Output:

Result: Th!s" the program is 'ritten to send a message from server to client !sing TC6A&6 socket" 'hich acts like an echo command.

Ex.No.2

Implementation of ate an! Time Using TCP Socket

AI" To 'rite a program in +ava that print Bate and time from the server on the client slide !sing TC6 socket. A#$ORITH" C9&ENT step1. import the re*!ired packages Step2: !sing port n!mber and host name establish the connection 'ith the server 'ith the help of socket. Step#: Send re*!est to server to response its time and date. Step%: Create ob+ect for 2!ffered reader and get date and time from server !sing Bate inp!t stream class. StepC: Bisplay the date and time received from server in the client side !sing print statement SteptD: Terminate the proced!re. SE3EE3: Step1: import the re*!ired packages Step2: initialiFe the server socket and address. Then create ob+ect for print 'riter and 2!ffered 3eader classes. Step#: receive the re*!est from client re*!esting from its date and time Step%: !sing date class get the c!rrent date and time and it is converted into string. StepC: !sing print 'riter the response to the client is sent in the string format. StepD: close the connection and terminate the proced!re. PRO$RA": SER%ER: import +ava.io.,$ import +ava.net.,$ import +ava.!til.,$ import +ava.lang.,$ class Tserver p!blic static void main.String as/ 01thro's Exception Bate d4ne' Bate.1$ String s14d.toString.1$ ServerSocket ss 4ne' ServerSocket.15G51$ Socket s4ss.accept.1$

6rintHriter o!t4ne' 6rintHriter.ne' 7!tp!tStreamHriter.s.get7!tp!tStream.11"tr!e1$ o!t.println.s11$ s.close.1$ ; ; C#IE&T: import +ava.net.,$ import +ava.io.,$ class Tclient p!blic static void main.String df/01thro's Exception &net=ddress a4&net=ddress.get9ocal<ost.1$ Socket s4ne' Socket.a"15G51$ 2!ffered3eader in4ne' 2!ffered3eader.ne' &np!tStream3eader.s.get&np!tStream.111$ System.o!t.println.in.read9ine.11$ s.close.1$ ; ;

OUTPUT:

RESU#T: Th!s the above programs 'ere exec!ted and proper o!tp!ts 'ere obtained '

Ex. No.# Bate: Aim:

(ile Transfer using ) TCP

To 'rite a program to transfer file bet'een client to server !sing TC6. Algorithm: Step 1: Start the program Step 2: &ncl!de the import +ava.net.,$ &mport +ava.io., 6ackage Step #: Befine a class for implementing main method. Step %: sing Clint and server sockets" &netaddres in the main program" 'hich 'ill ret!rn an instance of the class. Step C: The TC6 I Transmission Control 6rotocol is one the core protocols of the &nternet 6rotocol S!ite. sing TC6" applications on net'orked hosts can create connections to one" over 'hich they can exchange data or packets. Step D: The data from the client is read by a data inp!t stream and 'rite by data o!tp!t Stream. Step J: The file is transferred by client to server. &f the server is ready it sends the se*!ence of the re*!ested string to the client. Step G: 6rint o!t 'ith the necessary details. Program: (client: import +ava.net.,$ import +ava.io.,$ class Kclient p!blic static void main.String args/01thro's Exception Socket echosocket4n!ll$ 2!ffered3eader in 4 n!ll$ try echosocket 4 ne' Socket.&net=ddress.get9ocal<ost.1"15L21$ in4 ne' 2!ffered3eader.ne' &np!tStream3eader.echosocket.get&np!tStream.111$ ; catch. nkno'n<ostException e1 System.err.println.8co!ld not get io for the connection81$ System.exit.11$ ; String ser&np!t$ 'hile.. ser&np!t4in.read9ine.11:4n!ll1 System.o!t.println. ser&np!t1$ ;

in.close.1$ echosocket.close.1$ ; ; (server: import +ava.net.,$ import +ava.io.,$ class Kserver p!blic static void main.String args/01thro's Exception ServerSocket serversock4n!ll$ try serversock 4 ne' ServerSocket.15L21$ ; catch.&7Exception e1 System.err.println.8co!ld not listen on port 15L281$ System.exit.11$ ; Socket clientsock4n!ll$ try clientsock4serversock.accept.1$ System.o!t.println.8connected to client sock81$ ; catch.&7Exception e1 System.err.println.8accept failed81$ System.exit.11$ ; 6rintHriter o!tprint4ne' 6rintHriter.clientsock.get7!tp!tStream.1"tr!e1$ String inp!tline"o!tp!tline$ 2!ffered3eader b4ne' 2!ffered3eader.ne' &np!tStream3eader.System.in11$ System.o!t.println.8enter the name of the file81$ String s4b.read9ine.1$ Kile f4ne' Kile.s1$ if.f.exists.11 2!ffered3eader d4ne' 2!ffered3eader.ne' Kile3eader.s11$ String line$ 'hile..line4d.read9ine.11:4n!ll1 o!tprint.'rite.line1$ o!tprint.fl!sh.1$ ; d.close.1$ ;

b.close.1$ o!tprint.close.1$ serversock.close.1$ clientsock.close.1$ ; ;

Output:

Result: Th!s" the file transfer !sing TC6 has been exec!ted s!ccessf!lly.

Ex.No.% Bate:

ATE A& TI"E SER%ER USI&$ U P

Aim: To 'rite a program in +ava that print Bate and time from the server on the client slide !sing B6 socket A#$ORITH" C#IE&T: Step1: import re*!ired packages. Step2: create a socket !sing Batagram socket to establish connection 'ith B6 server Step#: <ere message are sent and received in the form of packet Step%: sing Batagram packet receive the message .Bate1 from the server.

StepC: print the date received from server side on client side. StepD: close the connection and terminate the proced!re. SER%ER: Step1: import the re*!ired packages Step2: Create datagram socket and datagram packet for establishing connection 'ith the client side. Step#: sing getBate. 1 method" get the c!rrent date form the system.

Step%: convert the date 'hich is in the form of string to byte and store it in the byte array. StepC: Transmit the date to the client !sing Batagram packet. StepD: Close the connection and terminate the proced!re. PRO$RA": SER%ER import +ava.net.,$ import +ava.!til.,$ class server p!blic static void main.String a/01thro's Exception BatagramSocket ds4ne' BatagramSocket.G5551$ byte/0 m4ne' byte/1550$ Batagram6acket in4ne' Batagram6acket.m"m.length1$ Batagram6acket o!t$ ds.receive.in1$ &net=ddress ip4in.get=ddress.1$

int po4in.get6ort.1$ String s14ne' String.in.getBata.1"5"in.get9ength.11$ System.o!t.println.8String is:8@s11$ Bate d4ne' Bate.1$ m4d.toString.1.get2ytes.1$ o!t4ne' Batagram6acket.m"m.length"ip"po1$ ds.send.o!t1$ ds.close.1$ ; ; C#IE&T import +ava.!til.,$ import +ava.net.,$ class client p!blic static void main.String a/01thro's Exception BatagramSocket ds4ne' BatagramSocket.1$ &net=ddress ip4&net=ddress.get9ocal<ost.1$ byte/0 m4ne' byte/1550$ Batagram6acket in4ne' Batagram6acket.m"m.length1$ Batagram6acket o!t$ String s48send data8$ m4s.get2ytes.1$ o!t4ne' Batagram6acket.m"m.length"ip"G5551$ ds.send.o!t1$ ds.receive.in1$ String s14ne' String.in.getBata.1"5"in.get9ength.11$ System.o!t.println.8data is:8@s11$ ds.close.1$ ; ;

OUTPUT:

RESU#T: Th!s the above programs 'ere exec!ted and proper o!tp!ts 'ere obtained.

E*'&o'+

O"AI& &A"E SER%ER using U P

AI": To implement Bomain name server !sing B6 client server program. A#$ORITH" Server: 1. Create a socket on server side !sing socket ro!tine. 2. =ssign &6 address "port n!mber and the protocol family to the members of str!ct sockaddr>in #. 2ind &6 address" port n!mber and protocol family to the socket. %. 9isten for re*!est from the client. C =ccept connection from the client application D. 7btain the <ostname and retrieve address !sing gethostbyname f!nction. J. Hrite the host address to the client side. G. Close socket. Client: 1. Create socket on client side !sing socket ro!tine. 2. =ssign &6 address" 6ort n!mber and 6rotocol family. #. Connect to the server !sing connect ro!tine. %. Enter the hostname and 'rite to the server. C. 3ead the &6 address and display it. D. Close the socket. Program: Server: Mincl!deNiostream.hO Mincl!deNstdio.hO Mincl!deNstdlib.hO Mincl!deNstring.hO Mincl!deN!nistd.hO Mincl!deNnetdb.hO Mincl!deNsysAsocket.hO Mincl!deNsysAtypes.hO Mincl!deNarpaAinet.hO Mincl!deNnetinetAin.hO void dg>echo.int sockfd"str!ct sockaddr, pcliaddr"socklen>t clilen1$ int main.int argc"char ,,argv1 int sockfd$ char mesg2/1550$ str!ct hostent ,hp$ str!ct sockaddr>in servaddr"cliaddr$ sockfd4socket.=K>&NET"S7CP>BQ3=R"51$ bFero.Sservaddr"siFeof.servaddr11$

servaddr.sin>family4=K>&NET$ servaddr.sin>addr.s>addr4htonl.&N=BB3>=NT1$ servaddr.sin>port4htons.LGJJ1$ bind.sockfd".str!ct sockaddr,1Sservaddr"siFeof.servaddr11$ dg>echo.sockfd".str!ct sockaddr,1Scliaddr"siFeof.cliaddr11$ ; void dg>echo.int sockfd"str!ct sockaddr, pcliaddr"socklen>t clilen1 int n$ socklen>t len$ str!ct hostent ,hp$ char mesg/1550$ for.$$1 len4clilen$ n4recvfrom.sockfd"mesg"155"5"pcliaddr"Slen1$ co!tNNmesg$ hp4gethostbyname.mesg1$ inet>ntop.=K>&NET"hpUOh>addr"mesg"siFeof.mesg11$ co!tNNmesg$ sendto.sockfd"mesg"n"5"pcliaddr"len1$ ; ; Client: Mincl!deNiostream.hO Mincl!deNstdio.hO Mincl!deN!nistd.hO Mincl!deNsysAsocket.hO Mincl!deNsysAtypes.hO Mincl!deNarpaAinet.hO Mincl!deNnetinetAin.hO void dg>cli.K&9E ,fp"int sockfd"str!ct sockaddr, pservaddr"socklen>t servlen1$ int main.int argc"char ,,argv1 int sockfd$ str!ct sockaddr>in servaddr$ if.argc:421 co!tNN8Vn !sage: !pdcli 8$ exit.51$ ; bFero.Sservaddr"siFeof.servaddr11$ servaddr.sin>family4=K>&NET$ servaddr.sin>port4htons.LGJJ1$ inet>pton.=K>&NET"argv/10"Sservaddr.sin>addr1$ sockfd4socket.=K>&NET"S7CP>BQ3=R"51$ co!tNN8Vn enter the host name:8$ dg>cli.stdin"sockfd".str!ct sockaddr,1 Sservaddr"siFeof.servaddr11$ exit.51$

; void dg>cli.K&9E ,fp"int sockfd"str!ct sockaddr, pservaddr"socklen>t servlen1 int n$ char sendline/1550" recvline/1510$ 'hile.fgets.sendline"155"fp1:4N 991 sendto.sockfd"sendline"strlen.sendline1"5"pservaddr"servlen1$ n4recvfrom.sockfd"recvline"155"5"N 99"N 991$ recvline/n045$ fp!ts.recvline"stdo!t1$ ; ;

OUTPUT: /armstronglWcseserver X0Y c@@ dnscli.cpp /armstronglWcseserver X0Y .Aa.o!t 5 1L2.1DG.D.1C1 Enter the domain name:localhost 12J.5.5.1 /armstrongWcseserver X0Y /armstronglWcseserver X0Y c@@ dnsserv.cpp /armstronglWcseserver X0Y .Aa.o!t Connected to 12J.5.5.1:localhost12J.5.5.1 /armstronglWcseserver X0Y

Result: Th!s the program 'as exec!ted s!ccessf!lly.

E*'&o',

Create a RA- socket .ith U P protocol

AI": To test a 3=H socket 'ith B6 protocol A#$ORITH" 1. Create a ra' socket !sing socket ro!tine. 2. =ssign &6 address "port n!mber and the protocol family to the members of str!ct sockaddr>in for so!rce and destination addresses. #. Kabricate the &6 header 'ith standard header str!ct!res b!t assign o!r o'n val!es. %. Kabricate the B6 header 'ith so!rce port n!mber and destination port n!mber. C. Calc!late the checks!m for integrity. D. 2!ild o!r o'n packet str!ct!re !sing setsockopt.1. J. send for every 2 second for 155 co!nt G. Close socket. Program: Mincl!de N!nistd.hO Mincl!de Niostream.hO Mincl!de Nstdio.hO Mincl!de NsysAsocket.hO Mincl!de NnetinetAip.hO Mincl!de NnetinetA!dp.hO AA The packet length Mdefine 6CPT>9EN G1L2 AA Can create separate header file ..h1 for all headers? str!ct!re AA The &6 header?s str!ct!re str!ct ipheader !nsigned char iph>ihl:C" iph>ver:%$ !nsigned char iph>tos$ !nsigned short int iph>len$ !nsigned short int iph>ident$ !nsigned char iph>flag$ !nsigned short int iph>offset$ !nsigned char iph>ttl$ !nsigned char iph>protocol$ !nsigned short int iph>chks!m$ !nsigned int iph>so!rceip$ !nsigned int iph>destip$ ;$ AA B6 header?s str!ct!re str!ct !dpheader !nsigned short int !dph>srcport$ !nsigned short int !dph>destport$ !nsigned short int !dph>len$ !nsigned short int !dph>chks!m$ ;$ AA total !dp header length: G bytes .4D% bits1

AA K!nction for checks!m calc!lation. Krom the 3KC" AA the checks!m algorithm is: AA 8The checks!m field is the 1D bit one?s complement of AAthe one?s complement s!m of all 1D bit 'ords in the AAheader. Kor p!rposes of comp!ting the checks!m" the AAval!e of the checks!m field is Fero.8 !nsigned short cs!m.!nsigned short ,b!f" int n'ords1 AA !nsigned long s!m$ for.s!m45$ n'ordsO5$ n'ordsUU1 s!m @4 ,b!f@@$ s!m 4 .s!m OO 1D1 @ .s!m S5xffff1$ s!m @4 .s!m OO 1D1$ ret!rn .!nsigned short1.Xs!m1$ ; AA So!rce &6" so!rce port" target &6" target port from the AAcommand line arg!ments int main.int argc" char ,argv/01 int sd$ AA No dataApayload +!st datagram char b!ffer/6CPT>9EN0$ AA 7!r o'n headers? str!ct!res str!ct ipheader ,ip 4 .str!ct ipheader ,1 b!ffer$ str!ct !dpheader ,!dp 4 .str!ct !dpheader ,1 .b!ffer @ siFeof.str!ct ipheader11$ AA So!rce and destination addresses: &6 and port str!ct sockaddr>in sin" din$ int one 4 1$ const int ,val 4 Sone$ memset.b!ffer" 5" 6CPT>9EN1$ if.argc :4 C1 co!tNN8U &nvalid parameters:::Vn8$ co!tNN8U sage Zs Nso!rce hostnameA&6O Nso!rce portO Ntarget hostnameA&6O Ntarget portOVn8NN argv/50$ exit.U11$ ; AA Create a ra' socket 'ith B6 protocol sd 4 socket.6K>&NET" S7CP>3=H" &6637T7> B61$ if.sd N 51 co!tNN8socket.1 error8$ AA &f something 'rong +!st exit exit.U11$ ; else co!tNN8socket.1 U sing S7CP>3=H socket and B6 protocol is 7P.Vn8$ AA The so!rce is red!ndant" may be !sed later if needed

AA The address family sin.sin>family 4 =K>&NET$ din.sin>family 4 =K>&NET$ AA 6ort n!mbers sin.sin>port 4 htons.atoi.argv/2011$ din.sin>port 4 htons.atoi.argv/%011$ AA &6 addresses sin.sin>addr.s>addr 4 inet>addr.argv/101$ din.sin>addr.s>addr 4 inet>addr.argv/#01$ AA Kabricate the &6 header or 'e can !se the AA standard header str!ct!res b!t assign o!r o'n val!es. ipUOiph>ihl 4 C$ ipUOiph>ver 4 %$ ipUOiph>tos 4 1D$ AA 9o' delay ipUOiph>len 4 siFeof.str!ct ipheader1 @ siFeof.str!ct !dpheader1$ ipUOiph>ident 4 htons.C%#211$ ipUOiph>ttl 4 D%$ AA hops ipUOiph>protocol 4 1J$ AA B6 AA So!rce &6 address" can !se spoofed address here::: ipUOiph>so!rceip 4 inet>addr.argv/101$ AA The destination &6 address ipUOiph>destip 4 inet>addr.argv/#01$ AA Kabricate the B6 header. So!rce port n!mber" red!ndant !dpUO!dph>srcport 4 htons.atoi.argv/2011$ AA Bestination port n!mber !dpUO!dph>destport 4 htons.atoi.argv/%011$ !dpUO!dph>len 4 htons.siFeof.str!ct !dpheader11$ AA Calc!late the checks!m for integrity ipUOiph>chks!m 4 cs!m..!nsigned short ,1b!ffer" siFeof.str!ct ipheader1 @ siFeof.str!ct !dpheader11$ AA &nform the kernel do not fill !p the packet str!ct!re. 'e 'ill b!ild o!r o'n... if.setsockopt.sd" &6637T7>&6" &6><B3&NC9" val" siFeof.one11 N 51 co!tNN8setsockopt.1 error8$ exit.U11$ ; else co!tNN8setsockopt.1 is 7P.Vn8$ AA Send loop" send for every 2 second for 155 co!nt co!tNN8Trying...Vn8$ co!tNN8 sing ra' socket and B6 protocolVn81$ co!tNN8 sing So!rce &6: [NN arv/10NN\ port:\NNatoi.argv/201NN\ Target &6:\NNargv/#0NN\ port:\NN atoi.argv/%01 Vn$ int co!nt$ for.co!nt 4 1$ co!nt N425$ co!nt@@1 if.sendto.sd" b!ffer" ipUOiph>len" 5" .str!ct sockaddr ,1Ssin" siFeof.sin11 N 51 AA Eerify -

Co!tNN8sendto.1 error8$ exit.U11$ ; else Co!tNN8Co!nt MZ! U sendto.1 is 7P.Vn8 NN co!nt1$ sleep.21$ ; ; close.sd1$ ret!rn 5$ ; OUTPUT:) /rootWbaka'ali testra'0M c@@ ra'!dp.cpp Uo ra'!dp /rootWbaka'ali testra'0M .Ara'!dp U &nvalid parameters::: U sage .Ara'!dp Nso!rce hostnameA&6O Nso!rce portO Ntarget hostnameA&6O Ntarget portO /rootWbaka'ali testra'0M .Ara'!dp 1L2.1DG.2.222 21 1L2.1DG.1.15 G5G5 socket.1 U sing S7CP>3=H socket and B6 protocol is 7P. setsockopt.1 is 7P. Trying... sing ra' socket and B6 protocol sing So!rce &6: 1L2.1DG.15.15 port: 21" Target &6: 25#.15D.L#.L1 port: G5G5. Co!nt M1 U sendto.1 is 7P. Co!nt M2 U sendto.1 is 7P. Co!nt M# U sendto.1 is 7P. Co!nt M% U sendto.1 is 7P. Co!nt MC U sendto.1 is 7P. Co!nt MD U sendto.1 is 7P. Co!nt MJ U sendto.1 is 7P. ...

Result: Th!s the program 'as exec!ted s!ccessf!lly. Ex.No.J Packet Capturing an! "onitoring

AI": To implement 6acket capt!ring and Ronitoring !ses +pcap. A#$ORITH": &et.ork interface !evice

1. Kind net'ork interface device name !sing ]pcapCaptor.getBevice9ist.1. 2. 7btain the information abo!t device name" device description" device)s R=C address" s!bnet and broadcast address. #. 3epeat steps 1U2 !ntil available net'ork interface devices. Sen!ing Packets to &et.ork !evice 1. 7pen available net'ork devices for sending packets thro!gh that device !sing ]pcapSender.openBevice.1. 2. Send the TC6 packets !sing ]pcapSender.1. Program: &et.ork interface !evice import +pcap.,$ import +pcap.packet.6acket$ class Tcpd!mp implements 6acket3eceiver p!blic void receive6acket.6acket packet1 System.o!t.println.packet1$ ; p!blic static void main.String/0 args1 thro's Exception Net'ork&nterface/0 devices 4 ]pcapCaptor.getBevice9ist.1$ if.args.lengthN11System.o!t.println.8!sage: +ava Tcpd!mp Nselect a n!mber from the follo'ingO81$ for .int i 4 5$ i N devices.length$ i@@1 System.o!t.println.i@8 :8@devices/i0.name @ 8.8 @ devices/i0.description@8181$ System.o!t.println.8 data link:8@devices/i0.datalink>name @ 8.8 @ devices/i0.datalink>description@8181$ System.o!t.print.8R=C address:81$ for .byte b : devices/i0.mac>address1 System.o!t.print.&nteger.to<exString.bS5xff1 @ 8:81$ System.o!t.println.1$ for .Net'ork&nterface=ddress a : devices/i0.addresses1 System.o!t.println.8 address:8@a.address @ 8 8 @ a.s!bnet @ 8 8 @ a.broadcast1$ ; ;else]pcapCaptor +pcap 4 ]pcapCaptor.openBevice.devices/&nteger.parse&nt.args/5010" 2555" false" 251$ +pcap.loop6acket.U1" ne' Tcpd!mp.11$ ; ; ; Sen!ing Packets to &et.ork !evice

import +ava.net.&net=ddress$ import +pcap.,$ import +pcap.packet.Ethernet6acket$ import +pcap.packet.&66acket$ import +pcap.packet.TC66acket$ class SendTC6 p!blic static void main.String/0 args1 thro's +ava.io.&7ExceptionNet'ork&nterface/0 devices 4 ]pcapCaptor.getBevice9ist.1$ if.args.lengthN11System.o!t.println.8 sage: +ava SentTC6 Ndevice index .e.g." 5" 1..1O81$ for.int i45$iNdevices.length$i@@1 System.o!t.println.i@8:8@devices/i0.name@8.8@devices/i0.description@8181$ System.exit.51$ ; int index4&nteger.parse&nt.args/501$ ]pcapSender sender4]pcapSender.openBevice.devices/index01$ TC66acket p4ne' TC66acket.12"#%"CD"JG"false"false"false"false"tr!e"tr!e"tr!e"tr!e"15"151$ p.set&6v%6arameter.5"false"false"false"5"false" false"false"5"1515151"155"&66acket.&6637T7>TC6" &net=ddress.get2yName.8'''.microsoft.com81" &net=ddress.get2yName.8'''.google.com811$ p.data4.8data81.get2ytes.1$ Ethernet6acket ether4ne' Ethernet6acket.1$ ether.frametype4Ethernet6acket.ET<E3TT6E>&6$ ether.src>mac4ne' byte/0-.byte15".byte11".byte12".byte1#".byte1%".byte1C;$ ether.dst>mac4ne' byte/0-.byte15".byte1D".byte1J".byte1G".byte1L".byte115;$ p.datalink4ether$ for.int i45$iN15$i@@1 sender.send6acket.p1$ ; ;

Output C:V 6rogram Kiles V]avaV]BP1DX1.5VbinO+avac Tcpd!mp.+ava C:V 6rogram Kiles V]avaV]BP1DX1.5VbinO+ava Tcpd!mp 5 12G25%GLD1:C#5J%D A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 hop.1 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#5LJL A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 hop.1 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#125# A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 hop.1 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K

12G25%GLD1:C#1%2C A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#1D%1 A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#1GD2 A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#25G5 A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#2#51 A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#2C25 A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K 12G25%GLD1:C#2J%5 A25J.%D.1L.2C%UOA25L.GC.2#1.15% protocol.D1 priority.51 551 offset.51 ident.2J5D11 TC6 12 O #% se*.CD1 'in.151 S K Other -in!o. C:V6rogram KilesV]avaV+dk1.D.5VbinO+ava SendTC6 5 C:V6rogram KilesV]avaV+dk1.D.5VbinO

hop.1 hop.1 hop.1 hop.1 hop.1 hop.1 hop.1

Result: Th!s the program 'as exec!ted s!ccessf!lly. Ex. No.G R"I Implementation for a given function

Aim: / To 'rite a +ava program to implement the remote method invocation. Algorithm: Server: Step 1: Start the program Step 2: &ncl!de the import +ava.rmi.Naming$ &mport +ava.rmi.,$6ackage Step #: Befine a class for implementing main method.

Step %: Beclare f!nctions in interface program. Step C: Befine f!nctions in implementations program. Step D: 3ebind the ob+ect of implementation by rebind.1. Step J: Terminate the program. Client: Step 1: Qet the host program Step 2: 9ook!p.1 for the given host name. Step #: 6rocess according to the choice Step %: Terminate the program. Program: I&TER(ACE import +ava.rmi.3emote$ import +ava.rmi.3emoteException$ p!blic interface intf extends 3emote int/0 fib.int a1thro's 3emoteException$ ; I"P#E"E&TATIO&: import +ava.rmi.3emoteException$ import +ava.rmi.server. nicast3emote7b+ect$ p!blic class impl extends nicast3emote7b+ect implements intf p!blic impl.1thro's 3emoteException ; p!blic int/0 fib.int n1thro's 3emoteException int fi45"a45"b41"i$ int ab/04ne' int/C50$ for.i45$iNn$i@@1 fi4fi@a$ a4b$ b4fi$ ab/i04fi$ ; ret!rn.ab1$ ; ; C#IE&T: import +ava.rmi.,$ import +ava.io.,$ p!blic class rmic p!blic static void main.String argr/01

try int i$ 2!ffered3eader br4ne' 2!ffered3eader.ne' &np!tStream3eader.System.in11$ System.o!t.println.8Enter the Server host name81$ String serip4br.read9ine.1$ String !rl48rmi:AA8@serip@8Armis8$ intf ref4.intf1Naming.look!p.!rl1$ 'hile.tr!e1 System.o!t.println.8Enter a n!mber81$ String st4br.read9ine.1$ if.st.e*!als.8end811 break$ ; else int n4&nteger.parse&nt.st1$ int aa/04ref.fib.n1$ for.i45$iNn$i@@1 System.o!t.println.aa/i01$ ; ; ; ; catch.Exception e1 ; ; ; SER%ER: import +ava.net. nkno'n<ostException$ import +ava.rmi.Naming$ p!blic class rmis p!blic static void main.String arg/01 try impl im4ne' impl.1$ Naming.rebind.8rmis8"im1$ System.o!t.println.8binding Complete81$ ; catch.Exception e1 ; ;

OUTPUT:

Result: Th!s the 3emote Rethod &nvocation has been implemented s!ccessf!lly. Ex.No:L Aim: Implementation of Sli!ing -in!o. Protocol

To 'rite a program in ]=E= to implement the sliding 'indo' protocol. Algorithm: 11 21 #1 %1 C1 D1 J1 Theor0: Sliding Hindo' 6rotocol implement ho' the sending and receiving sides of TC6 interact 'ith other to implement reliable and ordered delivery. <ere a 'indo' is maintained in the sender side 'ith the property. 9ast 2yte ackno'ledged less then or e*!al to 9ast 2yte send. Since the receiver side cannot ackno'ledge a byte that has not been sent and last 2yte send less then or e*!al to 9ast 2yte Hritten. Hhen ackno'ledge is received from receiver side the 'indo' is moved to next items. =t receiver side 'indo' is maintained as =dvertised Hindo' 4 Rax3ev2!ffer I .9ast byte received I 9ast 2yte 3ead1. Program: import +ava.io.,$ class p11sld'nd int len$ p!blic static void main.String args/01 thro's Exception p11sld'nd pvar 4 ne' p11sld'nd.1$ int a/0 4 ne' int/C50$ int b/0 4 ne' int/C50$ int n" 'sF" i" +" k" t" v" ch" sdch" rvch$ Bata&np!tStream dis 4 ne' Bata&np!tStream.System.in1$ String st$ + 4 t 4 v 4 'sF 4 5$ 'hile.tr!e1 System.o!t.println.8VnVtVtVtS9&B&NQ H&NB7H 637T7C7981$ System.o!t.println.8VtVtVt,,,,,,,,,,,,,,,,,,,,,,,,,Vn81$ System.o!t.println.8VtVt1. Sender81$ System.o!t.println.8VtVt2. 3eceiver81$ System.o!t.println.8VtVt#. Exit81$ System.o!t.print.8VtVt Enter To!r Choice : 81$ st 4 dis.read9ine.1$ ch 4 &nteger.parse&nt.st1$ s'itch.ch1 case 1 : bk1: 'hile.tr!e1 System.o!t.println.8VnVtVtBata Senders Ren!81$ 3ead the n!mber of items that sho!ld be in transmitter side. Set the 'indo' siFe. The n!mber of items to be sent is read. The n!mber of items at the receiver side is e*!al to n!mber of items sent. The n!mber of items to be ackno'ledged is set at receiver side. The 'indo' is moved to the next items according to items ackno'ledged. 3epeat the step 1 to D for all items to store and receive.

System.o!t.println.8VtVtUUUUUUUUUUUUUUUUU81$ System.o!t.println.8VtVt1. Store81$ System.o!t.println.8VtVt2. Hindo' SiFe Set81$ System.o!t.println.8VtVt#. Transit81$ System.o!t.println.8VtVt%. C!rrent Hindo' &tems81$ System.o!t.println.8VtVtC. Exit81$ System.o!t.print.8VtVt Enter To!r Choice : 81$ st 4 dis.read9ine.1$ sdch 4 &nteger.parse&nt.st1$ s'itch.sdch1 case 1: System.o!t.print.8VnVtVt <o' many Bata 'ant to Store U n : 81$ st 4 dis.read9ine.1$ n 4 &nteger.parse&nt.st1$ for.i45$ iNn$ @@i1 System.o!t.print.8VnVtVt Enter the data : 81$ st 4 dis.read9ine.1$ a/i0 4 &nteger.parse&nt.st1$ ; break$ case 2: System.o!t.print.8VnVtVt Enter the Hindo' SiFe : 81$ st 4 dis.read9ine.1$ 'sF 4 &nteger.parse&nt.st1$ break$ case #: System.o!t.print.8VnVtVt <o' many data 'ant to transmit : 81$ st 4 dis.read9ine.1$ pvar.len 4 &nteger.parse&nt.st1$ for.i45$ i N pvar.len$ @@i1 System.o!t.println.b/i01$ break$ case %: System.o!t.println.8VnVtVt The Bata inside Transmitter Hindo'81$ t 4 5$ 'sF 4 'sF @ +$ v 4 v @ +$ for.i4v$ i N 'sF$ @@i1 b/t@@0 4 a/i0$ for.i45$ i N t$ @@i1 System.o!t.println.b/i01$ break$ case C: break bk1$ ; ; break$ case 2: bk2: 'hile.tr!e1

System.o!t.println.8VnVtVtBata 3eceivers Ren!81$ System.o!t.println.8VtVtUUUUUUUUUUUUUUUUUUU81$ System.o!t.println.8VtVt1. 3eceive81$ System.o!t.println.8VtVt2. =ckno'ledge81$ System.o!t.println.8VtVt#. Exit81$ System.o!t.print.8VtVt Enter To!r Choice : 81$ st 4 dis.read9ine.1$ rvch 4 &nteger.parse&nt.st1$ s'itch.rvch1 case 1: System.o!t.println.8VnVtVt The n!mber of data 3eveived 81$ for.i45$ i N pvar.len$ @@i1 System.o!t.println.b/i01$ break$ case 2: System.o!t.print.8VnVtVt Enter the n!mber of data =ckno'ledged: 81$ st 4 dis.read9ine.1$ + 4 &nteger.parse&nt.st1$ break$ case #: break bk2$ ; ; break$ case #: System.exit.11$ ;;; ;

Output:

Result: Th!s the implementation of sliding 'indo' protocol is performed and o!tp!t is verified. Ex.No.15 &S)1 &ET-OR2 SI"U#ATOR

AI": St!dy of NSU2 Net'ork sim!lator. ESCRIPTIO&: Simulators = device" comp!ter program or system !sed d!ring soft'are verification" 'hich behaves or operates like a given system 'hen provided 'ith a set of controlled inp!ts. &ee! of Simulators: C!stomers 'ith the sim!lator spend less time deb!gging simple program errors The sim!lator makes it easy to 'rite and test code &t is easier for o!r s!pport engineers to explain complex problems if yo! have a sim!lator. The sim!lator re*!ires no set!p time. =n em!lator may re*!ire config!ration and a target board before yo! can deb!g. E*amples of Simulators: Some of the famo!s net'ork sim!lators available are listed belo'U NSU2 76NET 6=BNS QTNETS Q97R7S&R RCUS&R 9=T73 B=3R7 T< SSK 3ackgroun! of &S: NS began as a variant of the 3E=9 net'ork sim!lator in 1LGL and has evolved s!bstantially over the past fe' years. &n 1LLC ns development 'as s!pported by B=36= thro!gh the E&NT pro+ect at 929" ^erox 6=3C" C2" and SCA&S&. C!rrently ns development is s!pport thro!gh B=36= 'ith S=R=N and thro!gh NSK 'ith C7NSE3" both in collaboration 'ith other researchers incl!ding =C&3& NS doesn)t give priority to Q & f!ndamentally and is almost a textUbased code. Th!s" !sers sho!ld 'rite the code by Text Editor. 2!t since NS is free soft'are" it has been !sed 'idely by many researchers and its deb!gging is freely done. Therefore" the So!rce Codes of the c!rrent NS are very reliable and are config!red systematically. No'adays" many people have been !sing NS for Net'ork Sim!lators &S)1 &et.ork Simulator:

NS .version 21 is an ob+ectUoriented" discrete event driven net'ork sim!lator developed at C 2erkley 'ritten in C@@ and 7Tcl. NS is primarily !sef!l for sim!lating local and 'ide area net'orks. NS2 is an openUso!rce sim!lation tool that r!ns on 9in!x =ltho!gh NS is fairly easy to !se once yo! get to kno' the sim!lator" it is *!ite diffic!lt for a first time !ser" beca!se there are fe' !serUfriendly man!als. Even tho!gh there is a lot of doc!mentation 'ritten by the developers" 'hich has in depth explanation of the sim!lator" it is 'ritten 'ith the depth of a skilled NS !ser. &et.ork Animator 4&A"5 NS together 'ith its companion" N=R" form a very po'erf!l set of tools for teaching net'orking concepts. NS contains all the &6 protocols typically covered in !ndergrad!ate and most grad!ate co!rses" and many experimental protocols contrib!ted by its everUexpanding !sers base. Hith nam" these protocols can vis!aliFed as animations. N=R Qraphical Editor This is the latest .Sept. 25511 addition to N=R. Hith this editor" yo! no longer have to type TC9 code to create animations. To! can create yo!r net'ork topology and sim!late vario!s protocols and traffic so!rces by dragging the mo!se. What can you do with these tools? Create Terrestrial" satellite and 'ireless net'orks 'ith vario!s ro!ting algorithms .BE" 9S" 6&RUBR" 6&RUSR" =7BE" BS31. Traffic so!rces like 'eb" ftp" telnet" cbr" and stochastic traffic. Kail!res" incl!ding deterministic" probabilistic loss" link fail!re" etc. Eario!s *!e!ing disciplines .dropUtail" 3EB" K_" SK_" B33" etc.1 and _oS . Overvie. of &S The overall sim!lation proced!re in the NS is sho'n in Kig.1. Kirst of all" there is a lang!age named 7Tcl" and this is an ob+ect oriented version lang!age 'hich 'as converted from the conventional Tcl .Tool Command 9ang!age1. =s sho'n in Kig.2" an 7Tcl Script representing a given net'ork config!ration is 'ritten. NS is simply composed of 7Tcl Script and 7Tcl &nterpreter. 7Tcl &nterpreter can translate the code related to NS !sing NS Sim!lation 9ibrary. NS sim!lation res!lts can be observed thro!gh graphs for analysis or animations 'ith N=R .Net'ork =nimator1. N=R is a net'ork animator that is developed for ed!cation p!rposes. &t can display the detailed proced!re of the sim!lation. N=R is not !sed in this experiment.

Eis!aliFe 6acket flo'" *!e!e b!ilds !p and packet drops. 6rotocol behavior: TC6 slo' start" selfU clocking" congestion control" fast retransmit and recovery. Node movement in 'ireless net'orks. =nnotations to highlight important events. 6rotocol state .e.g." TC6 c'nd1.

Eery simply speaking" NS is the 7b+ectUoriented Tcl .7Tcl1 script interpreter that has a sim!lation event sched!ler and net'ork component ob+ect libraries" and net'ork setU!p mod!le libraries. sers 'rite an 7Tcl script !sing 7Tcl.

(ig'6 Simplifie! User7s %ie. of &S 465 ualit0 of OTcl an! C88

(ig'1 C88 an! OTcl: The ualit0

(igure 9' Architectural %ie. of &S =s sho'n in Kig.#" NS has a oneUtoUone mapping str!ct!re bet'een C@@ and 7Tcl. Kor example" if there exists a class 'hich implements HK_ .Heighted Kair _!e!ing1 in C@@" there exists a similar Class in 7Tcl. This str!ct!re is introd!ced for improving the sim!lation speed and providing the

convenience. =nd the core components in net'ork sim!lation" s!ch as Node" 9ink" and _!e!e are 'ritten !sing C@@ Classes. Since 7Tcl performs at a slo' processing speed" the components for Net'ork Sim!lation are 'ritten !sing C@@ Classes" and the process that comprises Net'ork by connecting these components 'ith each other is 'ritten in 7Tcl Script. Roreover" NS is implemented !sing a d!al str!ct!re as sho'n in the above fig!re for accessing components 'ritten in C@@ Classes thro!gh 7Tcl Script. Conse*!ently" if a !ser makes a ne' Net'ork Component" heAshe sho!ld 'rite C@@ Classes and 7Tcl Classes and the part to connect each other. Since it is some'hat diffic!lt to 'rite C@@ Classes and to perform Sim!lation" it is omitted in this experiment. 415 &S irector0 Structure

(ig': &S irector0 Structure =mong the s!bUdirectories of nsUallinoneU2.1b sho'n in Kig.%" nsU2 has all of the sim!lator implementations .either in C@@ or in 7Tcl1" validation test 7Tcl scripts and example 7Tcl scripts. Hithin this directory" all 7Tcl codes and testAexample scripts are located !nder the s!bUdirectory called tcl" and most of C@@ code. Kor example" NS)s 2.1b has the Birectory of nsUallinoneU2.1b" 'hich is !s!ally located in A!srAlocalAnsUallinoneU2.1b. =ll kinds of tools needed in NS form the s!bUdirectory str!ct!re" and in partic!lar" nsU2 directory has all so!rce codes of C@@ Class. Since every Net'ork Component is 'ritten in C@@ Class in this 'ay" !sers can easily !nderstand the real operations of Net'ork Components to be !sed. This is one of large advantages. There is also a tcl directory in nsU2 directory. The tcl directory has 7Tcl Codes" and its s!bU directories incl!de several examples" validation test" and 7Tcl so!rces.

495 #earning OTcl The first step to !tiliFe NS is to !nderstand the 7Tcl lang!age. Kig.C sho's a program example 'ritten in Tcl lang!age. &n Tcl" the key'ord (proc) is !sed to define a proced!re" follo'ed by a proced!re

name and arg!ments in the first brace and definition in the second brace. =t this moment" note that yo! m!st !se the brace" differently from C@@. Note that yo!)ll enco!nter an error if yo! move the last brace [-[ of [proc test -; -[ to the next line. &f yo! r!n [set a %#\" NS checks 'hether or not the variable [a\ is already declared. &f the variable name has already been declared" NS inserts %# into

(ig'+ A Sample Tcl Script [a\. 7ther'ise" a ne' one is declared and NS s!bstit!tes %# into it. &n other 'ords" declaration and assignment are combined together. [set a\ means that the instance [a\ is declared only in case that [a\ doesn)t exist. &n [set c /expr Ya @ Yb0\" one thing to note is that to get the val!e assigned to a variable" Y is !sed 'ith the variable name" and another thing is that [expr\ is !sed to do mathematical comp!tations. Therefore" %#@2J4J5 is inserted into c. Hhen 'e !se [for\ command" 'e initialiFe k 'ith [-set k 5;\" and give a condition 'ith [-Yk N 15;\" and !pdate for &ndex variable 'ith [-incr k;\. Kinally" the key'ord (p!ts) is to print o!t the follo'ing string 'ithin do!ble *!otation marks like [printf.1\ in C. OTcl 7Tcl is the lang!age 'hich modifies the conventional Tcl to an ob+ectUoriented version. Kig.D sho's an example of an 7Tcl script that defines t'o ob+ect classes" 8mom8 and 8kid8" 'here 8kid8 is the child class of 8mom8" and a member f!nction called 8greet8 for each class. =fter defining the classes" each ob+ect instance is declared" the 8age8 variable of each instance is set to %C .for mom1 and 1C .for kid1" and the 8greet8 member f!nction of each ob+ect instance is called.

(ig', A Sample OTcl Script The key'ord (Class) is to create an ob+ect class and (instproc) is to define a member f!nction to an ob+ect class. Class inheritance is specified !sing the key'ord (Us!perclass.) &n defining member f!nctions" (Yself) acts like the 8this8 pointer in C@@" and (instvar) is !sed to check if the follo'ing variable name has already been declared in its class or in its s!perclass. &f the variable name has already been declared" the variable is referred. 7ther'ise" a ne' one is declared. Kinally" to create an ob+ect instance" the key'ord (ne') is !sed as sho'n in the example.

4:5 &S Class Hierarch0 &f yo! !nderstood 7Tcl in some extent" yo! need to look into the Class <ierarchy of NS. NS has the Class <ierarchy 'hich is implemented !sing both C@@ and 7Tcl" as sho'n in Kig.J.

(ig'/ &S Class Hierarch0 The root of the hierarchy is the Tcl7b+ect class that is the s!perclass of all 7Tcl libraries. =s a descendant class of Tcl7b+ect" Ns7b+ect class is the s!perclass of all basic net'ork component ob+ects. Kig. 1J sho's the NS Class <ierarchy. =ct!ally" it has a very complex class hierarchy. &n the fig!re" =_R .=ctive _!e!e Ranagement1 s!ch as BropTail" 3EB" 3ER" and B33 is implemented !nder _!e!e. TC6 means TC6 Tahoe. TC6 3eno" Ne'reno" and S=CP are implemented by inheriting TC6 Tahoe. No' yo! can 'rite 7Tcl Script to perform a sim!lation by !sing Net'ork Components of NS. & 'o!ld recommend that yo! perform some simple sim!lations by referring to the front part of NS Ran!al /D0. & also 'o!ld recommend that yo! refer to very !sef!l examples of 7Tcl Script located in A!srAlocalAnsUallinoneUxxxAnsUxxxAtclAex. Har!.are;Soft.are re<uirements: To b!ild ns yo! need a comp!ter and a C@@ compiler. The allUinUone package re*!ires abo!t #25R2 of disk space to b!ild. 2!ilding ns from pieces can save some disk space. There are t'o 'ays to b!ild ns: from all the pieces or all at once. &f yo! +!st 'ant to try it o!t *!ickly" yo! might try all at once. &f yo! 'ant to do CUlevel development" or save do'nload time or disk space" or have tro!ble 'ith allUinU one yo! sho!ld b!ild it from the pieces NsUallinone is a package" 'hich contains en*!ired components and some optional components !sed in r!nning ns. The package contains an 8install8 script to a!tomatically config!re" compile and install these components. =fter do'nloading" r!n the install script. &f yo! haven?t installed ns before and 'ant to *!ickly try ns o!t" nsUallinone may be easier than getting all the pieces by hand.

RESU#T Th!s the st!dy of ns2 sim!lator has been done. Ex.No.11 Implementing Routing protocols using &S1

AI": To &mplement 3o!ting protocols !sing NS2 A#$ORITH" 1. Create a sim!lator ob+ect and Befine different colors for data flo's .for N=R1 2. 7pen the N=R trace file #. Befine a ?finish? proced!re for Close the trace file and Exec!te N=R on the trace file %. Create topology as fo!r nodes n5" n1" n2 and n# 'ith a d!plex link bet'een them. C. Qive node position .for N=R1 D. Set _!e!e SiFe of link .n2Un#1 to 15 J. Ronitor the *!e!e for link .n2Un#1. .for N=R1 G. Set!p TC6 connection and B6 connection G. Set!p a KT6 over TC6 connection and C23 over B6 connection L. Sched!le events for the C23 and KT6 agents 15. Call the finish proced!re after C seconds of sim!lation time 11. 6rint C23 packet siFe and interval 12. 3!n the sim!lation Program: MCreate a sim!lator ob+ect set ns /ne' Sim!lator0 MBefine different colors for data flo's .for N=R1 Yns color 1 2l!e Yns color 2 3ed M7pen the N=R trace file set nf /open o!t.nam '0 Yns namtraceUall Ynf MBefine a ?finish? proced!re proc finish -; global ns nf Yns fl!shUtrace MClose the N=R trace file close Ynf MExec!te N=R on the trace file exec nam o!t.nam S exit 5 ; MCreate fo!r nodes set n5 /Yns node0 set n1 /Yns node0 set n2 /Yns node0 set n# /Yns node0 MCreate links bet'een the nodes Yns d!plexUlink Yn5 Yn2 2Rb 15ms BropTail

Yns d!plexUlink Yn1 Yn2 2Rb 15ms BropTail Yns d!plexUlink Yn2 Yn# 1.JRb 25ms BropTail MSet _!e!e SiFe of link .n2Un#1 to 15 Yns *!e!eUlimit Yn2 Yn# 15 MQive node position .for N=R1 Yns d!plexUlinkUop Yn5 Yn2 orient rightUdo'n Yns d!plexUlinkUop Yn1 Yn2 orient rightU!p Yns d!plexUlinkUop Yn2 Yn# orient right MRonitor the *!e!e for link .n2Un#1. .for N=R1 Yns d!plexUlinkUop Yn2 Yn# *!e!e6os 5.C MSet!p a TC6 connection set tcp /ne' =gentATC60 Ytcp set class> 2 Yns attachUagent Yn5 Ytcp set sink /ne' =gentATC6Sink0 Yns attachUagent Yn# Ysink Yns connect Ytcp Ysink Ytcp set fid> 1 MSet!p a KT6 over TC6 connection set ftp /ne' =pplicationAKT60 Yftp attachUagent Ytcp Yftp set type> KT6 MSet!p a B6 connection set !dp /ne' =gentA B60 Yns attachUagent Yn1 Y!dp set n!ll /ne' =gentAN!ll0 Yns attachUagent Yn# Yn!ll Yns connect Y!dp Yn!ll Y!dp set fid> 2 MSet!p a C23 over B6 connection set cbr /ne' =pplicationATrafficAC230 Ycbr attachUagent Y!dp Ycbr set type> C23 Ycbr set packet>siFe> 1555 Ycbr set rate> 1mb Ycbr set random> false MSched!le events for the C23 and KT6 agents Yns at 5.1 8Ycbr start8 Yns at 1.5 8Yftp start8 Yns at %.5 8Yftp stop8 Yns at %.C 8Ycbr stop8

Mdetach tcp and sink agents .not really necessary1 Yns at %.C 8Yns detachUagent Yn5 Ytcp $ Yns detachUagent Yn# Ysink8 MCall the finish proced!re after C seconds of sim!lation time Yns at C.5 8finish8 M6rint C23 packet siFe and interval p!ts 8C23 packet siFe 4 /Ycbr set packet>siFe>08 p!ts 8C23 interval 4 /Ycbr set interval>08 M3!n the sim!lation Yns r!n OUTPUT:)

Result: Th!s the program 'as exec!ted s!ccessf!lly. Ex.No.12 Performance comparison of "AC protocols

Aim: To &mplement performance comparison of R=C protocols !sing NS2

A#$ORITH" 1. Create a sim!lator ob+ect and Befine different colors for data flo's .for N=R1 2. 7pen the N=R trace file #. Befine a ?finish? proced!re for Close the trace file and Exec!te N=R on the trace file %. Create topology as fo!r nodes n5" n1" n2 and n# 'ith a d!plex link bet'een them. C. Qive node position .for N=R1 D. Set _!e!e SiFe of link .n2Un#1 to 15 J. Set !p R=C 6rotocol G. 3!n the sim!lation Program co!ing: M:AbinAsh M the next line finds ns V nshome4`dirname Y5`$ / : Ux YnshomeAns 0 SS / Ux ..A..Ans 0 SS nshome4..A.. M the next line starts ns V export nshome$ exec YnshomeAns 8Y58 8YW8 if /info exists env.nshome10 set nshome Yenv.nshome1 ; elseif /file exec!table ..A..Ans0 set nshome ..A.. ; elseif -/file exec!table .Ans0 aa /file exec!table .Ans.exe0; set nshome 8/p'd08 ; else p!ts 8Yargv5 cannot find ns directory8 exit 1 ; set env.6=T<1 8YnshomeAbin:Yenv.6=T<18 set opt.tr1 opt set opt.namtr1 88 set opt.seed1 5 set opt.stop1 25 set opt.node1 # set opt.*siFe1 155 set opt.b'1 2Rb set opt.delay1 1ms set opt.ll1 99 set opt.if*1 _!e!eABropTail set opt.mac1 RacACsmaACa set opt.chan1 Channel set opt.tcp1 TC6A3eno set opt.sink1 TC6Sink set opt.app1 KT6 proc sage -; global opt argv5 p!ts 8 sage: Yargv5 V/Ustop secV0 V/Useed val!eV0 V/Unode n!mNodesV08

p!ts 8VtV/Utr tracefileV0 V/UgV08 p!ts 8VtV/Ull lltypeV0 V/Uif* *typeV0 V/Umac mactypeV08 p!ts 8VtV/Ub' Yopt.b'10 V/Udelay Yopt.delay1V08 exit 1 ; proc Qetopt -; global opt argc argv if -Yargc 44 5; sage for -set i 5; -Yi N Yargc; -incr i; set key /lindex Yargv Yi0 if :/string match -U,; Ykey0 contin!e set key /string range Ykey 1 end0 set val /lindex Yargv /incr i00 set opt.Ykey1 Yval if /string match -U/=UF0,; Yval0 incr i U1 contin!e ; s'itch Ykey ll - set opt.Ykey1 99AYval ; if* - set opt.Ykey1 _!e!eAYval ; mac - set opt.Ykey1 RacAYval ; ; ; ; proc finish -; global env nshome p'd global ns opt trfd Yns fl!shUtrace close Ytrfd foreach key -node b' delay ll if* mac seed; lappend comment Yopt.Ykey1 ; set force 88 if -/info exists opt.f10 aa /info exists opt.g10; set force 8Uf8 ; exec perl YnshomeAbinAtrsplit Utt r Upt tcp Uc ?Ycomment? V Yforce Yopt.tr1 OS Yopt.tr1Ub' exec head Un 1 Yopt.tr1Ub' OW stdo!t if /info exists opt.g10 catch 8exec xgraph Unl UR Udisplay Yenv.B&S69=T1 V /lsort /glob Yopt.tr1.,00 S8 ; exit 5 ; proc createUtrace -; global ns opt if /file exists Yopt.tr10 -

catch 8exec rm Uf Yopt.tr1 Yopt.tr1Ub' /glob Yopt.tr1.,08 ; set trfd /open Yopt.tr1 '0 Yns traceUall Ytrfd if -Yopt.namtr1 :4 88; Yns namtraceUall /open Yopt.namtr1 '0 ; ret!rn Ytrfd ; proc createUtopology -; global ns opt global lan node so!rce set n!m Yopt.node1 for -set i 5; -Yi N4 Yn!m; -incr i; set node.Yi1 /Yns node0 lappend nodelist Ynode.Yi1 ; set lan /Yns ne'9an Ynodelist Yopt.b'1 Yopt.delay1 V UllType Yopt.ll1 Uif*Type Yopt.if*1 V UmacType Yopt.mac1 UchanType Yopt.chan10 ; proc createUso!rce -; global ns opt global lan node so!rce set n!m Yopt.node1 for -set i 1; -Yi N4 Yn!m; -incr i; set src 5 set dst Yi set tp.Yi1 /Yns createUconnection Yopt.tcp1 V Ynode.Ysrc1 Yopt.sink1 Ynode.Ydst1 50 set so!rce.Yi1 /Ytp.Yi1 attachUapp Yopt.app10 Yns at /expr YiA1555.50 8Yso!rce.Yi1 start8 ; ; MM R=&N MM Qetopt if -Yopt.seed1 O4 5; - nsUrandom Yopt.seed1 ; if /info exists opt.*siFe10 - _!e!e set limit> Yopt.*siFe1 ; set ns /ne' Sim!lator0 set trfd /createUtrace0 createUtopology createUso!rce Ylan trace Yns Ytrfd Yns at Yopt.stop1 8finish8 Yns r!n OUTPUT:

Result: Th!s the program 'as exec!ted s!ccessf!lly

Ex.No. 1#

Performance comparison of routing protocols

AI": To compare the performance of 3o!ting protocols !sing NS2 A#$ORITH" 1. Create a sim!lator ob+ect and Befine different colors for data flo's .for N=R1 2. 7pen the N=R trace file #. Befine a ?finish? proced!re for Close the trace file and Exec!te N=R on the trace file. %. Call xgraph to display the res!lts. C. Create topology as five nodes as n5" n1" n2" n# and n%. Connect a d!plex link bet'een them. D. Befine a proced!re ?attachUexpooUtraffic? that attaches a B6 agent to a previo!sly created node ?node? and attaches an Expoo traffic generator to the agent 'ith the characteristic val!es ?siFe? for packet siFe ?b!rst? for b!rst time" ?idle? for idle time and ?rate? for b!rst peak rate. The proced!re connects the so!rce 'ith the previo!sly defined traffic sink ?sink? and ret!rns the so!rce ob+ect. J. Befine a proced!re ?record? 'hich periodically records the band'idth received by the three traffic sinks sink5A1A2 and 'rites it to the three files f5A1A2. G. Calc!late the band'idth .in R2itAs1 and 'rite it to the files. L. 3eUsched!le the proced!re ?record? at reg!lar intervals.5.C seconds1. 15. Create three traffic so!rces" start and stop traffic so!rces at reg!lar intervals respectively. 11. Call the finish proced!re after D5 seconds of sim!lation time 12. 3!n the sim!lation Program: Bsdv.tcl M 'rls1.tcl M = #Unode example for adUhoc sim!lation 'ith BSBE M Befine options set val.chan1 set val.prop1 set val.netif1 set val.mac1 set val.if*1 set val.ll1 set val.ant1 set val.if*len1 set val.nn1 set val.rp1 set val.x1 set val.y1 set val.stop1 ChannelAHirelessChannel $M channel type 6ropagationAT'o3ayQro!nd $M radioUpropagation model 6hyAHireless6hy $M net'ork interface type RacAG52>11 $M R=C type _!e!eABropTailA6ri_!e!e $M interface *!e!e type 99 $M link layer type =ntennaA7mni=ntenna $M antenna model C5 $M max packet in if* # $M n!mber of mobilenodes BSBE $M ro!ting protocol C55 $M ^ dimension of topography %55 $M T dimension of topography 1C5 $M time of sim!lation end

set ns> /ne' Sim!lator0 Yns> !seUne'trace set tracefd /open simple.tr '0 set 'indo'EsTime2 /open 'in.tr '0 set namtrace /open simple.nam '0

Yns> traceUall Ytracefd Yns> namtraceUallU'ireless Ynamtrace Yval.x1 Yval.y1 M set !p topography ob+ect set topo /ne' Topography0 Ytopo load>flatgrid Yval.x1 Yval.y1 createUgod Yval.nn1 M M Create nn mobilenodes /Yval.nn10 and attach them to the channel. M M config!re the nodes Yns> nodeUconfig Uadhoc3o!ting Yval.rp1 V UllType Yval.ll1 V UmacType Yval.mac1 V Uif*Type Yval.if*1 V Uif*9en Yval.if*len1 V UantType Yval.ant1 V UpropType Yval.prop1 V UphyType Yval.netif1 V UchannelType Yval.chan1 V Utopo&nstance Ytopo V UagentTrace 7N V Uro!terTrace 7N V UmacTrace 7N V UmovementTrace 7N for -set i 5; -Yi N Yval.nn1 ; - incr i ; set node>.Yi1 /Yns> node0 ; M 6rovide initial location of mobilenodes Ynode>.51 set ^> C.5 Ynode>.51 set T> C.5 Ynode>.51 set b> 5.5 Ynode>.11 set ^> %L5.5 Ynode>.11 set T> 2GC.5 Ynode>.11 set b> 5.5 Ynode>.21 set ^> 1C5.5 Ynode>.21 set T> 2%5.5 Ynode>.21 set b> 5.5 M Qeneration of movements Yns> at 15.5 8Ynode>.51 setdest 2C5.5 2C5.5 #.58 Yns> at 1C.5 8Ynode>.11 setdest %C.5 2GC.5 C.58 Yns> at 115.5 8Ynode>.51 setdest %G5.5 #55.5 C.58

M Set a TC6 connection bet'een node>.51 and node>.11 set tcp /ne' =gentATC6ANe'reno0 Ytcp set class> 2 set sink /ne' =gentATC6Sink0 Yns> attachUagent Ynode>.51 Ytcp Yns> attachUagent Ynode>.11 Ysink Yns> connect Ytcp Ysink set ftp /ne' =pplicationAKT60 Yftp attachUagent Ytcp Yns> at 15.5 8Yftp start8 M 6rinting the 'indo' siFe proc plotHindo' -tcpSo!rce file; global ns> set time 5.51 set no' /Yns> no'0 set c'nd /YtcpSo!rce set c'nd>0 p!ts Yfile 8Yno' Yc'nd8 Yns> at /expr Yno'@Ytime0 8plotHindo' YtcpSo!rce Yfile8 ; Yns> at 15.1 8plotHindo' Ytcp Y'indo'EsTime28 M Befine node initial position in nam for -set i 5; -Yi N Yval.nn1; - incr i ; M #5 defines the node siFe for nam Yns> initial>node>pos Ynode>.Yi1 #5 ; M Telling nodes 'hen the sim!lation ends for -set i 5; -Yi N Yval.nn1 ; - incr i ; Yns> at Yval.stop1 8Ynode>.Yi1 reset8$ ; M ending nam and the sim!lation Yns> at Yval.stop1 8Yns> namUendU'ireless Yval.stop18 Yns> at Yval.stop1 8stop8 Yns> at 1C5.51 8p!ts V8end sim!lationV8 $ Yns> halt8 proc stop -; global ns> tracefd namtrace Yns> fl!shUtrace close Ytracefd close Ynamtrace ; Yns> r!n

OUTPUT:

Bsr.tcl

M 'rls1.tcl M = #Unode example for adUhoc sim!lation 'ith BSBE M Befine options set val.chan1 set val.prop1 set val.netif1 set val.mac1 set val.if*1 set val.ll1 set val.ant1 set val.if*len1 set val.nn1 set val.rp1 set val.x1 set val.y1 set val.stop1 ChannelAHirelessChannel $M channel type 6ropagationAT'o3ayQro!nd $M radioUpropagation model 6hyAHireless6hy $M net'ork interface type RacAG52>11 $M R=C type _!e!eABropTailA6ri_!e!e $M interface *!e!e type 99 $M link layer type =ntennaA7mni=ntenna $M antenna model C5 $M max packet in if* # $M n!mber of mobilenodes BS3 $M ro!ting protocol C55 $M ^ dimension of topography %55 $M T dimension of topography 1C5 $M time of sim!lation end

set ns> /ne' Sim!lator0 Yns> !seUne'trace set tracefd /open simple.tr '0 set 'indo'EsTime2 /open 'in.tr '0 set namtrace /open simple.nam '0 Yns> traceUall Ytracefd Yns> namtraceUallU'ireless Ynamtrace Yval.x1 Yval.y1 M set !p topography ob+ect set topo /ne' Topography0 Ytopo load>flatgrid Yval.x1 Yval.y1 createUgod Yval.nn1 M M Create nn mobilenodes /Yval.nn10 and attach them to the channel. M M config!re the nodes Yns> nodeUconfig Uadhoc3o!ting Yval.rp1 V UllType Yval.ll1 V UmacType Yval.mac1 V Uif*Type Yval.if*1 V Uif*9en Yval.if*len1 V UantType Yval.ant1 V UpropType Yval.prop1 V UphyType Yval.netif1 V UchannelType Yval.chan1 V Utopo&nstance Ytopo V UagentTrace 7N V

Uro!terTrace 7N V UmacTrace 7N V UmovementTrace 7N for -set i 5; -Yi N Yval.nn1 ; - incr i ; set node>.Yi1 /Yns> node0 ; M 6rovide initial location of mobilenodes Ynode>.51 set ^> C.5 Ynode>.51 set T> C.5 Ynode>.51 set b> 5.5 Ynode>.11 set ^> %L5.5 Ynode>.11 set T> 2GC.5 Ynode>.11 set b> 5.5 Ynode>.21 set ^> 1C5.5 Ynode>.21 set T> 2%5.5 Ynode>.21 set b> 5.5 M Qeneration of movements Yns> at 15.5 8Ynode>.51 setdest 2C5.5 2C5.5 #.58 Yns> at 1C.5 8Ynode>.11 setdest %C.5 2GC.5 C.58 Yns> at 115.5 8Ynode>.51 setdest %G5.5 #55.5 C.58 M Set a TC6 connection bet'een node>.51 and node>.11 set tcp /ne' =gentATC6ANe'reno0 Ytcp set class> 2 set sink /ne' =gentATC6Sink0 Yns> attachUagent Ynode>.51 Ytcp Yns> attachUagent Ynode>.11 Ysink Yns> connect Ytcp Ysink set ftp /ne' =pplicationAKT60 Yftp attachUagent Ytcp Yns> at 15.5 8Yftp start8

M 6rinting the 'indo' siFe proc plotHindo' -tcpSo!rce file; global ns> set time 5.51 set no' /Yns> no'0 set c'nd /YtcpSo!rce set c'nd>0 p!ts Yfile 8Yno' Yc'nd8 Yns> at /expr Yno'@Ytime0 8plotHindo' YtcpSo!rce Yfile8 ; Yns> at 15.1 8plotHindo' Ytcp Y'indo'EsTime28 M Befine node initial position in nam for -set i 5; -Yi N Yval.nn1; - incr i ; -

M #5 defines the node siFe for nam Yns> initial>node>pos Ynode>.Yi1 #5 ; M Telling nodes 'hen the sim!lation ends for -set i 5; -Yi N Yval.nn1 ; - incr i ; Yns> at Yval.stop1 8Ynode>.Yi1 reset8$ ; M ending nam and the sim!lation Yns> at Yval.stop1 8Yns> namUendU'ireless Yval.stop18 Yns> at Yval.stop1 8stop8 Yns> at 1C5.51 8p!ts V8end sim!lationV8 $ Yns> halt8 proc stop -; global ns> tracefd namtrace Yns> fl!shUtrace close Ytracefd close Ynamtrace ; Yns> r!n

OUTPUT:

=odv.tcl M 'rls1.tcl M = #Unode example for adUhoc sim!lation 'ith BSBE M Befine options set val.chan1 set val.prop1 set val.netif1 set val.mac1 set val.if*1 set val.ll1 set val.ant1 set val.if*len1 set val.nn1 set val.rp1 set val.x1 set val.y1 set val.stop1 ChannelAHirelessChannel $M channel type 6ropagationAT'o3ayQro!nd $M radioUpropagation model 6hyAHireless6hy $M net'ork interface type RacAG52>11 $M R=C type _!e!eABropTailA6ri_!e!e $M interface *!e!e type 99 $M link layer type =ntennaA7mni=ntenna $M antenna model C5 $M max packet in if* # $M n!mber of mobilenodes =7BE $M ro!ting protocol C55 $M ^ dimension of topography %55 $M T dimension of topography 1C5 $M time of sim!lation end

set ns> /ne' Sim!lator0 Yns> !seUne'trace set tracefd /open simple.tr '0 set 'indo'EsTime2 /open 'in.tr '0 set namtrace /open simple.nam '0 Yns> traceUall Ytracefd Yns> namtraceUallU'ireless Ynamtrace Yval.x1 Yval.y1 M set !p topography ob+ect set topo /ne' Topography0 Ytopo load>flatgrid Yval.x1 Yval.y1 createUgod Yval.nn1 M M Create nn mobilenodes /Yval.nn10 and attach them to the channel. M

M config!re the nodes Yns> nodeUconfig Uadhoc3o!ting Yval.rp1 V UllType Yval.ll1 V UmacType Yval.mac1 V Uif*Type Yval.if*1 V Uif*9en Yval.if*len1 V UantType Yval.ant1 V UpropType Yval.prop1 V UphyType Yval.netif1 V

UchannelType Yval.chan1 V Utopo&nstance Ytopo V UagentTrace 7N V Uro!terTrace 7N V UmacTrace 7N V UmovementTrace 7N for -set i 5; -Yi N Yval.nn1 ; - incr i ; set node>.Yi1 /Yns> node0 ; M 6rovide initial location of mobilenodes Ynode>.51 set ^> C.5 Ynode>.51 set T> C.5 Ynode>.51 set b> 5.5 Ynode>.11 set ^> %L5.5 Ynode>.11 set T> 2GC.5 Ynode>.11 set b> 5.5 Ynode>.21 set ^> 1C5.5 Ynode>.21 set T> 2%5.5 Ynode>.21 set b> 5.5 M Qeneration of movements Yns> at 15.5 8Ynode>.51 setdest 2C5.5 2C5.5 #.58 Yns> at 1C.5 8Ynode>.11 setdest %C.5 2GC.5 C.58 Yns> at 115.5 8Ynode>.51 setdest %G5.5 #55.5 C.58 M Set a TC6 connection bet'een node>.51 and node>.11 set tcp /ne' =gentATC6ANe'reno0 Ytcp set class> 2 set sink /ne' =gentATC6Sink0 Yns> attachUagent Ynode>.51 Ytcp Yns> attachUagent Ynode>.11 Ysink Yns> connect Ytcp Ysink set ftp /ne' =pplicationAKT60 Yftp attachUagent Ytcp Yns> at 15.5 8Yftp start8 M 6rinting the 'indo' siFe proc plotHindo' -tcpSo!rce file; global ns> set time 5.51 set no' /Yns> no'0 set c'nd /YtcpSo!rce set c'nd>0 p!ts Yfile 8Yno' Yc'nd8 Yns> at /expr Yno'@Ytime0 8plotHindo' YtcpSo!rce Yfile8 ; Yns> at 15.1 8plotHindo' Ytcp Y'indo'EsTime28

M Befine node initial position in nam for -set i 5; -Yi N Yval.nn1; - incr i ; M #5 defines the node siFe for nam Yns> initial>node>pos Ynode>.Yi1 #5 ; M Telling nodes 'hen the sim!lation ends for -set i 5; -Yi N Yval.nn1 ; - incr i ; Yns> at Yval.stop1 8Ynode>.Yi1 reset8$ ; M ending nam and the sim!lation Yns> at Yval.stop1 8Yns> namUendU'ireless Yval.stop18 Yns> at Yval.stop1 8stop8 Yns> at 1C5.51 8p!ts V8end sim!lationV8 $ Yns> halt8 proc stop -; global ns> tracefd namtrace Yns> fl!shUtrace close Ytracefd close Ynamtrace ; Yns> r!n

Result: Th!s the program 'as exec!ted s!ccessf!lly

Potrebbero piacerti anche