STUDY OF HEADER FILES 1.
stdio.h: .h:
Has standar standard d input input and output output librar library y provid providing ing simple simple and effici efficient ent buffer buffered ed strea stream m IO interface. 2.
unistd.h:
It is a POSIX standard for open system interface. 3.
string.h:
This header file is used to perform string manipulation operations o n NU terminated strings. 4.
stdli.h:
This This header header file file contai contains ns the utili utility ty functi functions ons such such as string string convers conversion ion routin routines! es! memory memory allocation routines! random number generator! etc. !.
s"s#t"$%s.h:
"efines the data type of soc#et address structure in unsigned long. &.
s"s#so'(%t.h:
The soc#et functions can be defined as ta#ing pointers to the generic soc#et address structure called soc#addr. ).
n%tin%t#in.h:
"efines the IPv$ soc#et address structure commonly called Internet soc#et address structure called soc#addr%in. *.
n%td.h:
"efines the structure hostent for using the system call gethostbyname to get the net&or# host entry. +.
ti,%.h:
Has structures and functions to get the system date and time and to perform time manipulation functions. 'e use the function ctime()! that is defined in this header file ! to calculate the current date and time. 1-.
s"s#stt.h:
*ontains the structure stat to test a descriptor to see if it is of a specified type. +lso it is used to display file or file system status. 1
11.
s"s#io'tl.h ,
-acros and defines used in specifying an ioctl reuest are located in this header file. 'e use the function ioctl() that is defined in this header file. ioctl() function is used to perform +/P cache operations. 12.
$'$.h:
Has functi function on defini definitio tions ns that that are reuir reuired ed for pac#et pac#et captur capturing ing.. Some Some of the functi functions ons are pcap%loo#updev()!pcap%open%live() and pcap%loop(). pcap%loo#updev() is used to initiali0e the net&or# device.The device to be sniffed is opened using the pcap%open%live(). Pcap%loop() determines the number of pac#ets to be sniffed.
13.
n%t#i/0r$.h:
*ontains the definitions for +ddress /esolution Protocol. 'e use this to manipulate the +/P reuest structure and its data members arp%pa!arp%dev and arp%ha. The arp%ha structure1s data member sa%data2 3 has the hard&are address.
14.
%rrno.h:
It sets an error number &hen an error and that error can be displayed d isplayed using perror function. It has symbolic error names. The error number is never set to 0ero by any library function. 1!.
r$#in%t.h:
This is used to convert internet addresses bet&een +S*II strings and net&or# byte ordered binary values (values that are stored in soc#et address structures). It is used for inet%aton! inet%addr! inet%ntoa functions.
ILEETATIO ILEETATIO OF SLIDI 5IDO5 ROTO6OL 2
E7. o: 1 &rite a program to simulate Sliding 'indo& Protocol. AI: To &rite ETHODOLOY: Sliding 5indo8 roto'ol:
Sliding 'indo& Protocols are a feature of pac#et4based data transmission protocols. protocols. They are used in the data lin# layer (OSI model) as &ell as in T*P (transport (transport layer of of the OSI model). They are used to #eep a record of the frame seuences sent! and their respective ac#no&ledgements received! received! by both the users. In transmit flo& control! control! sliding &indo& is a variable4duration &indo& that that allo& allo&ss a sender to transmit transmit a specif specified ied number number of data data units befor beforee an ac#no&ledgment is received or before a specified event occurs. The sender creates a seuence number for each frame as it is transmitted. Throughout the communicati communication! on! it maintains the send window size! the last acknowledgment received ! and the last frame sent . To ensure that the &indo& does not overflo&! the sender ensures that the &indo& si0e is
grea greate terr than than the the seue euenc ncee numb number er of last last fram framee sent sent minu minuss the the seu seuen ence ce numb number er last last ac#no&ledgment received. •
Sender5s data lin# layer maintains a 5sending &indo&5 &hich consists of a set of seuence numbers corresponding to the frames it is permitted to send.
•
Similarly! the receiver maintains a 5receiving &indo&5 corresponding to the set of frames it is permitted to accept.
3
•
The seuence numbers &ithin the sender5s &indo& represent the frames sent but as yet not ac#no&ledged.
•
'henever a ne& pac#et arrives from the net&or# layer! the upper edge of the &indo& is advanced by one.
•
'hen an ac#no&ledgement arrives from the receiver the lo&er edge is advanced by one. The receiver5s &indo& corresponds to the frames that the receiver5s data lin# layer may accept.
•
'hen a frame &ith seuence number eual to the lo&er edge of the &indo& is received! an ac#no&ledgement is generated and the &indo& is rotated by one.
•
'hen a frame falling outside the &indo& is received! the receiver5s data lin# layer may either discard this frame or it may accept these frames and buffer them until the appropriate frame is received.
Algorith,: S%r9%r:
6. Start the program. 7. *reate the soc#et! establish a connection using accept function. 8. "ecide the Offered &indo& si0e 9/1 based on the minimum value of *ongestion &indo& and advertised &indo&. $. Send / bytes of data to receiver. :. /eceive the ac#no&ledgement from the receiver. ;. /eceive the 'indo& update value based on it transmit the data until
6. Start the program. 7. *reate the soc#et and *onnect to the server using connect() function. 8. Send the advertised &indo& si0e to the server. $. /eceive the data. :. Send the ac#no&ledgement of ne=t data to be received to the server. ;. *alculate the &indo& update and send it as the current &indo& si0e for sending data.
Sliding 5indo8 S%r9%r:
>include?stdio.h@ 4
>include?string.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?netinetAin.h@ >include?arpaAinet.h@ >include?unistd.h@ >include?stdlib.h@ >define SI
if(lis ? F) C printf(JKn Brror in listening KnJ)E e=it(46)E L printf(JKn istening is completedJ)E lenGsi0eof(sin)E accptGaccept(sid!(struct soc#addr)Msin!Mlen)E if(accpt ? F) C printf(JKn Brror in +ccepting KnJ)E e=it(46)E L printf(JKn *lient is +ccepted KnJ)E printf(JKn Bnter the te=t, J)E scanf(JQsJ!str)E iGFE &hile(i ? strlen(str)) C strncpy(frame!strRi!SI
printf(JQdJ!iRD)E
&rite(accpt!frame!si0eof(frame))E read(accpt!ac#!7F)E sscanf(ac#!JQdJ!Mstatus)E if(statusGG46) printf(JKn Transmission is successful KnJ)E else C printf(JKn /eceived error in rame, Qd KnJ!status)E 6
printf(JKn /etansmitting rames, J)E
for(DGFEE) C
frame2D3Gstr2DRstatus3E
DRRE
printf(JQdJ!DRstatus)E
if((DRstatus)Q$GGF)
brea#E L
frame2D3G5KF5E
&rite(accpt!frame!si0eof(frame))E L
iRGSI
>include?stdio.h@ >include?stdlib.h@ >include?string.h@ >include?sysAsoc#et.h@ >include?sysAtypes.h@ >include?netinetAin.h@ int main() C int sid!con!choiceE struct soc#addr%in sinE char str27F3!err27F3E sidGsoc#et(P%INBT!SO*%ST/B+-!F)E if(sid GG 46) C 7
printf(JKn Brror in creating the soc#et KnJ)E e=it(46)E L printf(JKn Soc#et created successfullyJ)E memset(Msin!F!si0eof(sin))E sin.sin%familyG+%INBTE sin.sin%portGhtons($F:F)E sin.sin%addr.s%addrGhtonl(IN+""/%+N)E conGconnect(sid!(struct soc#addr)Msin!si0eof(sin))E if(con ? F) C printf(JKn The connection established falied KnJ)E e=it(46)E L printf(JKn *onnection established successfully KnJ)E for(EE) C read(sid!str!7F)E if(strcasecmp(str!Je=itJ))
brea#E printf(JKn /eceived, Qs J!str)E printf(J Kn "o u &ant to report an error(64es F4 no), J)E
scanf(JQdJ!Mchoice)E if(choice)
&rite(sid!J46J!si0eof(J46J))E
else C printf(J Bnter the seuence no of the frame &here error has occurred, J)E
scanf(JQsJ!err)E
&rite(sid!err!si0eof(err))E
read(sid!str!7F)E printf(JKn /eceived the re4transmitted frames, QsJ!str)E L 8
L shutdo&n(sid!7)E close(sid)E L Out$ut
2testritlinu= 3V cc sl&server.c 2testritlinu= 3V .Aa.out The soc#et is created successfully inding *ompleted istening is completed *lient is +ccepted Bnter the te=t, computernet&or#s Transmitting rames, F678 Transmission is successful Transmitting rames, $:;W /eceived error in rame, : /etansmitting rames, ;W Transmitting rames, Y6F66 Transmission is successful Transmitting rames, 67686$6: Transmission is successful 2testritlinu= 3V cc sl&client.c 2testritlinu= 3V .Aa.out 44 INSB/T 44 Soc#et created successfully *onnection established successfully 44 INSB/T 44 /eceived, comp "o u &ant to report an error(64es F4 no), F 44 INSB/T 44 /eceived, uter "o u &ant to report an error(64es F4 no), 6 Bnter the seuence no of the frame &here error has occurred,: 9
44 INSB/T 44 /eceived the re4transmitted frames, ter /eceived, net& "o u &ant to report an error(64es F4 no), F 44 INSB/T 44 /eceived, or#s "o u &ant to report an error(64es F4 no), F 2testritlinu= 3V
R%sult:
Thus the program to simulate sliding &indo& protocol &as done and output &as verified.
10
STUDY OF SO6ET RORAI AD 6LIET ; SER
It is based on UNIX and defines a set of system calls (procedures) that are an e=tension of system calls used to access files. > + Jsoc#etJ is a loose term used to describe Jan end point for communication.J The traditional er#ley Soc#et +PI is a set of * function calls used to support net&or# c ommunication 2. ri,r" So'(%t 6lls so'(%t=> 4 create a ne& soc#et and return its descriptor ind=>? associate a soc#et &ith a port and address list%n=>? establish ueue for connection reuests ''%$t=>? accept a connection reuest 'onn%'t=>? initiate a connection to a remote host r%'9=>? receive data from a soc#et descriptor s%nd=>? send data to a soc#et descriptor 'los%=>? Zone4&ay[ close of a soc#et descriptor
3. %t8or( Dts% Ad,inistrtion /un'tions g%thost"n,% 4 given a hostname! returns a structure &hich specifies its "NS name(s) and IP address(es) g%ts%r9"n,% 4 given service name and protocol! returns a structure &hich specifies its name(s) and its port address g%thostn,% 4 returns hostname of local host g%ts%r9"n,% g%ts%r9"$ort g%ts%r9%nt g%t$roto"n,% @g%t$roto"nu,%r @g%t$roto"%nt g%tn%t"n,% @g%tn%t"ddr@ g%tn%t%nt
4. So'(%t Utilit" Fun'tions ntohs #ntohl 4 convert shortAlong from net&or# byte order (big endian to host byte order ) htons #htonl 4 convert shortAlong from host byte order to net&or# byte order in%t0nto #in%t0ddr 4 convert 874bit IP address (net&or# byte order toAfrom a dotted decimal string) $%rror => 4 print error message (based on Z errno[) to stderr h%rror => 4 print error message for gethostbyname () to stderr (used &ith "NS)
!. ri,r" H%d%r Fil%s 4 In Uni=,
>include ?sysAtypes.h@ >include ?sysAsoc#et.h@ >include ?sysAtime.h@ >include ?time.h@ >include ?netinetAin.h@ >include ?arpaAinet.h@
A basic system data types A A basic soc#et definitions A A timevalCL for select() A A timespecCL for pselect() A A soc#addr%inCL and other Internet defns A A inet(8) functions A 11
>include ?errno.h@ >include ?fcntl.h@ A for nonbloc#ing! on &indo&s don5t use fnctl()! use ioctlsoc#et() A >include ?netdb.h@ >include ?signal.h@ >include ?stdio.h@ >include ?stdlib.h@ >include ?string.h@ >include ?sysAstat.h@ A for S%=== file mode constants A >include ?unistd.h@ A this file is typically only for inu= ni= OS5s A >include ?sysA&ait.h@ >include ?sysAun.h@ A for Uni= domain soc#ets A >include ?stdarg.h@ A for sdprintf A - In Windows >include ?&indo&s.h@ >include ?sysAtypes.h@ >include ?&insoc#.h@ 6onn%'tions nd Asso'itions In Soc#et terms a connections bet&een t&o processes in called an association. +n association can be abstractly defined as a :4 tuple &hich specifies the t&o processes and a method of communication. or e=ample, \ $roto'ol@ lo'l?ddr@ lo'l?$ro'%ss@ /or%ign?ddr@ /or%ign?$ro'%ssB
+ half4association is a single Zside[ of an association (a 84 tuple) $roto'ol@ ddr@ $ro'%ssB %t8or(ing T%r,s • • •
$'(%t 4 the smallest unit that can be transferred Zthrough[ the net&or# by itself $roto'ol 4 a set of rules and conventions bet&een the communicating participants + collection of protocol layers is referred to as a Z$roto'ol suit% [! Zprotocol family[ or Zprotocol stac#[. T*PAIP is one such protocol suite.
& .Csi' So'(%t 6lls Th%ir $rotot"$%s Procedures that implement the Soc#et +PI 6.1. The Socket Procedure:
The socket procedure creates a soc#et and returns an integer descriptor. This descriptor is used by the application to be passed as an argument to call procedures to transfer data across the net&or#. •
Initiali0e a soc#et or create a soc#et FORAT: int sockfd so'(%t=int family@ int type@ int protocol > /eturns a soc#et descriptor if successfulE46 if error 12
&here, family stands for addressing family. symbolic name Protocol amily
o
+%UNIX
intra4host UNIX
+%INBT
T*PAIP2default3
+%NS
Xero= NS
+%"B*NBT
"B* "N+
Its purpose is to specify the method of addressing used by the soc#et. type stands for the type of soc#et interface to be used.
o
service type
symbolic name
comment
datagram
SO*%"]/+-
U"P protocol in +%INBT
reliable! in4order
SO*%ST/B+-
T*P protocol in +%INBT
ra& soc#et
SO*%/+'
direct access to net&or# layer
o
o
protocol can be U"P! T*P! IP or I*-P. sockfd is an integer (similar to a file descriptor) returned by the so'(%t call. This is the So'(%t D%s'ri$tor ! &hich uniuely defines the created soc#et if the creation is Successful. It returns ^6 if there is an error. The Soc#et "escriptor is used by other functions to refer the soc#et.
Eample: if (( soc#fd G soc#et(+%INBT!SO*%ST/B+-!F)) GG 46) C printf(J, error opening soc#etJ)E e=it(46)E L Eample: if ( (soc#fd G soc#et(+%INBT! SO*%"]/+-! F)) ? F) C A handle error A L
AA T*P
AA U"P
;.7 The !ind Procedure , ind (/egister) a soc#et to a port address. The Bind unction binds a soc#et to a local soc#et address by adding the local soc#et address to an already created soc#et. 'hen *reated a soc#et has neither a local address nor a remote address. + Server uses the bind procedure to supply a protocol port number at &hich the server &ill &ait for contact. The Prototype is given as FORAT:
int ind=int sockfd @ stru't so'(ddr "localaddr @ int addrlen> /eturns F if successfulE46 if error
&here, 13
o o
o
sockfd is the same integer returned by the so'(%t call. localaddr is the local address returned by the ind call. 2 i.e. 3 localaddr is a pointer to the stru't so'(ddr &hich is used in the bind call socklen#t localaddrlen is an interger returning the si0e of the struct soc#addr &hich can be obtained by the call si%o/=stru't so'(ddr>
Eample: if ( bind(soc#! (struct soc#addr ) Msoc#%addr! si0eof(soc#%addr)) ? F) C printf(J, error binding soc#et to local addressJ)E e=it(46)E L ; .$ The %ISTE& Procedure, Indicate readiness to receive connections
The listen function is called only by the server. It creates a passive soc#et from an unconnected soc#et. efore calling the listen function! the soc#et must already be created and the local soc#et address fields set. This function informs the OS that the server is ready to accept the connection through this soc#et. To do so! a server calls the listen procedure &hose prototype is as follo&s,
FORAT:
int list%n=int sockfd @ int 'ueue-si(e> /eturns F if successfulE46 if error
&here, o o
sockfd is the same integer returned by the so'(%t call. 'ueue-si(e indicates the number of connection reuests &hich can be ueued by the system &hile the local process has not yet issued the ''%$t call.
6.) The *ccept Procedure , +ccept a connection The accept function is called by a T*P Server to remove the first connection reuest from the corresponding ueue. If there are no reuests (the ueue is empty)! the accept function is put to sleep. The prototype is given as FORAT:
int ''%$t=int sockfd @ stru't so'(ddr "forei+n-address @ int addrlen> /eturns a soc#et descriptor if successfulE46 if error
&here,
o
sockfd is the same integer returned by the so'(%t call. forei+n-address is the pointer to the address of the foreign (client) process that has reuested the connection returned by the ''%$t call.
o
*ddrlen is the length of the client address.
o
Note that this ''%$t call is issued by a server process rather than a client process. If there is a connection reuest &aiting on the ueue for this soc#et connection! ''%$t ta#es the first reuest on the ueue and creates another soc#et &ith the same properties as sockfd E This function actually creates a ne& soc#et (child soc#et) that can be used by a child server to 14
connect to the client. Other&ise! ''%$t &ill bloc# the caller process until a connection reuest arrives. The return value of this function is the ne& soc#et descriptor & ., The onnect procedure , /euest connection to the server The connect function is used by a process ( usually a client) to establish an active connection to a remote process (server).The prototype is as follo&s FORAT: int 'onn%'t=int sockfd @ stru't so'(ddr "forei+n-address @ int addrlen> /eturns F if successfulE46 if error &here, o o
o
sockfd is the same integer returned by the so'(%t call. forei+n-address is the address of foreign (server) process returned by the 'onn%'t call. addrlen is the length of that server address
Note that this call is issued by a client process rather than a server process. E7,$l%: if (connect(soc#! ( struct soc#addr ) Msoc#%addr! si0eof soc#%addr) GG 46) C printf(J, soc#et connection errorJ)E e=it(46)E L 6.6 lose a socket: FORAT:
int 'los%=int sockfd > /eturns F if successfulE46 if error
&here, sockfd is the same integer returned by the so'(%t call. Example: A connected soc#et is int soc#t A close( soc#t )E A close soc#et imeediately A shutdo&n( soc#t! F)E A close soc#et! and dont recieve any more A shutdo&n( soc#t! 6)E A close soc#et! and dont send any more A shutdo&n( soc#t! 7)EA close soc#et! and dont recieve! or send any more A 6. Send and/or recei0e data
int r%d=int sockfd @ 'hr "uf2 unsign int nytes> int 8rit%=int sockfd @ 'hr "uf2 unsign int nytes>
r%d9(soc#fd! char *buffer ! int addrlen)!
r%'9()!
r%d/ro,()!
15
s%nd ( sockfd, msg, len, flags)! int r%'9/ro,(int sockfd ! char *buf, int nbytes, int flags, struct soc#addr *from, int addrlen) int s%ndto(int sockfd ! char *buf, int nbytes, int flags, struct soc#addr *to, int addrlen)
•
read() M &rite () are used for connection oriented Transport
•
recvfrom() and sendto() are used for connection less Transport
E7,$l%s:
6. if (&rite(soc#! message! si0e) ?F ) C printf(J, error &riting to remote hostJ)E e=it(46)E L 7 .bytes%received G read(soc#! reply! si0eof(reply))E These calls can be used to receive and send data in an established soc#et association (or connection). Note that these calls are similar to the standard r%d and 8rit% file IAO system calls. •
data sentAreceived using standard UNIX iAo system calls or net&or#4specific system calls system call
device
buffering
read()A&rite()
any iAo
single
readv()A&ritev()
any iAo
scatterAgather
recv()Asend()
soc#et
single
recvfrom()Asendto()
soc#et
single
recvmsg()Asendmsg()
soc#et
scatterAgather
S%nding 9i so'(%t: s%ndto=> ?6onn%'tionl%ss int s%ndto (int soc#fd! char buff! int bufflen! int flags! struct soc#addr toaddrptr! int addresslen) soc#fd is the variable assigned soc#et() return value. buff is a set of consecutive mem. locs. holding message to send • •
•
•
bufflen is number of bytes to send flags can be used to specify options. +l&ays F for us. 16
•
toaddrptr is address of soc#addr%in structure holding destination address info. -ust be cast to type soc#addr.
•
addresslen is the si0e of the address structure
•
O return code does NOT imply data correctly delivered! only correctly sent
/eadingA'riting Soc#ets, e=amples B=ample, using sendto() char buffer2:F3E struct soc#addr%in other%app%addrE int retcode •
•
A suppose &e have done soc#et() and bind() calls! filled in other%app%addr!and put 78 bytes of data into buffer A retcode G sendto(soc#fd! buffer! 78! F! (struct soc#addr ) Mother%app%addr! si0eof(other%app%addr)) R%ding /ro, so'(%t: r%'9/ro,=> ?6onn%'tionl%ss
B=ample, using recvfrom() char buffer2:F3E struct soc#addr%in other%app%addrE int nread! addrlenE A suppose &e have done soc#et()! bind() A nread G recvfrom(soc#fd! buffer! 78! F! (struct soc#addr ) Mother%app%addr! Maddrlen))
int r%'9/ro, =int so'(/d@ 'hr Gu//@ int u//l%n@ int /lgs@ stru't so'(ddr G/ro,ddr$tr@ int Gddr%ssl%n> • •
•
•
•
•
soc#fd is variable assigned soc#et() return value. buff is a set of consecutive mem. locs. into &hich received data to be &ritten bufflen is number of bytes to read flags can be used to specify options. +l&ays F for us. fromaddrptr is address of soc#addr%in structure containing address info of soc#et that sent this data. + returned value. addresslen is si0e of address structure. + returned value.
recvfrom() returns number bytes actually received S%nding Dt through s%nd=> To send data over a soc#et the program &ill need to ma#e a call to send ()E if the soc#et is non4 bloc#ing the send call should return immediately &ith the number of bytes that &ere successfully &ritten to the soc#ets output ueue. The return from send does not indicate the number of bytes received on the other end of the connection nor does it indicate ho& large the T*P pac#et si0e is. ut if you get a positive return value on a send call it means the OS has ta#en responsibility for transmitting that data B=ample,
int iE 17
A soc#d is the connected soc#et file descriptor A i G send( soc#d! JHello 'orldKnJ! 68! F)E if( i @ F )C printf(Jsent Qd bytesKnJ! i)E L R%'%i9ing Dt through r%'9=>
To receive data over a soc#et the program &ill need to ma#e a call to recv()E if the soc#et is non4 bloc#ing the recv call should return immediately &ith the number of bytes that &ere successfully read from the soc#ets input ueue. The return from recv does not indicate the number of bytes sent by the remote host! nor does it indicate ho& large the tcp pac#et si0e &as that &as read. It is also important to note that if recv() returns F ( 0ero ) that indicates the remote soc#et has closed the connection.
C"t% Ord%ring Routin%s yte ordering routines, convert toAfrom host 6;4 and 874bit integers fromAto __net&or# byte order55 "ifferent computers may store bytes of integer in different order in memory. o •
o
internal representation of 7`C7$L
o
net&or# byte order is big4endian
o
Integer uantities (such as addressing info) should be e=plicitly converted toAfrom net&or# byte order (nbo).
unction name
action
htonl
convert 874bit host format to Net&or# byte order
ntohl
convert Net&or# byte order to 874bit host 18
format htons
convert 6;4bit host format to Net&or# byte order
ntos
convert Net&or# byte order to 6;4bit host format
D%'lrtions /or "t%?ord%r trns/or,tions
u%short htons (u%short host%short) u%short ntohs (u%short net&or#%short) u%long htonl (u%long host%short) u%long ntohl (u%long net&or#%short)
An E7,$l% S'%nrio +s an e=ample! consider the soc#et system calls for a connection4oriented protocol.
19
RESULT,
Thus the basic functions used for Soc#et Programming &as studied successfully. SIULATI AR #RAR ROTO6OLS E7. o: 3 DATE: AI: To implement +/P and /+/P protocol 20
Addr%ss R%solution roto'ol =AR> is $roto'ol /or ,$$ing n Int%rn%t roto'ol ddr%ss = I ddr%ss> to $h"si'l ,'hin% ddr%ss tht is r%'ogni%d in th% lo'l n%t8or(. For %7,$l%@ in I <%rsion 4@ th% ,ost 'o,,on l%9%l o/ I in us% tod"@ n ddr%ss is 32 its long. In n Eth%rn%t lo'l r% n%t8or(@ ho8%9%r@ ddr%ss%s /or tt'h%d d%9i'%s r% 4* its long. =Th% $h"si'l ,'hin% ddr%ss is lso (no8n s %di A''%ss 6ontrol or A6 ddr%ss.> A tl%@ usull" 'll%d th% AR ''h%@ is us%d to ,intin 'orr%ltion %t8%%n %'h A6 ddr%ss nd its 'orr%s$onding I ddr%ss. AR $ro9id%s th% $roto'ol rul%s /or ,(ing this 'orr%ltion nd $ro9iding ddr%ss 'on9%rsion in oth dir%'tions.
Ho8 AR 5or(s 5h%n n in'o,ing $'(%t d%stin%d /or host ,'hin% on $rti'ulr lo'l r% n%t8or( rri9%s t gt%8"@ th% gt%8" s(s th% AR $rogr, to /ind $h"si'l host or A6 ddr%ss tht ,t'h%s th% I ddr%ss. Th% AR $rogr, loo(s in th% AR ''h% nd@ i/ it /inds th% ddr%ss@ $ro9id%s it so tht th% $'(%t 'n % 'on9%rt%d to th% right $'(%t l%ngth nd /or,t nd s%nt to th% ,'hin%. I/ no %ntr" is /ound /or th% I ddr%ss@ AR rod'sts r%u%st $'(%t in s$%'il /or,t to ll th% ,'hin%s on th% LA to s%% i/ on% ,'hin% (no8s tht it hs tht I ddr%ss sso'it%d 8ith it. A ,'hin% tht r%'ogni%s th% I ddr%ss s its o8n r%turns r%$l" so indi'ting. AR u$dt%s th% AR ''h% /or /utur% r%/%r%n'% nd th%n s%nds th% $'(%t to th% A6 ddr%ss tht r%$li%d ADDRESS RESOLUTIO ROTO6OL
>include?unistd.h@ >include?sysAtypes.h@ >include?netinetAin.h@ >include?stdio.h@ >include?string.h@ >include?sysAsoc#et.h@ int main() C int sd!DGF!portG7FFFE char con2:F3GJFJE struct soc#addr%in serE char ip2:F32:F3GCJ676.F.F.6J!J676.F.F.7J!J676.F.F.8J!J676.F.F.$JLE char eth2:F32:F3GCJ676.87.$:.F.F.6J!J676.87.$:.F.F.7J!J676.87.$:.F.F.8J!J676.87.$:.F.F.$JLE sdGsoc#et(+%INBT!SO*%ST/B+-!IPP/OTO%T*P)E b0ero((char)Mser!si0eof(ser))E ser.sin%familyG+%INBTE ser.sin%portGhtons(port)E ser.sin%addr.s%addrGhtonl(IN+""/%+N)E printf(JBnter the ip address,J)E scanf(JQsJ!Mcon)E for(DGFED?$EDRR) C if(strcmp(con!ip2D3)GGF) 21
C printf(JBthernet +ddress is,QsKnJ!eth2D3)E brea#E L L return FE L OUTUT: Bnter the ip address,676.F.F.6 Bthernet +ddress is,676.87.$:.F.F.6 RE
>include?unistd.h@ >include?sysAtypes.h@ >include?netinetAin.h@ >include?string.h@ >include?sysAsoc#et.h@ >include?stdio.h@ int main() C int sd!DGF!portG7FFFE char con2:F3GJFJE struct soc#addr%in serE char ip2:F32:F3GCJ676.F.F.6J!J676.F.F.7J!J676.F.F.8J!J676.F.F.$JLE char eth2:F32:F3GCJ676.87.$:.F.F.6J!J676.87.$:.F.F.7J!J676.87.$:.F.F.8J!J676.87.$:.F.F.$JL E sdGsoc#et(+%INBT!SO*%ST/B+-!IPP/OTO%T*P)E b0ero((char)Mser!si0eof(ser))E ser.sin%familyG+%INBTE ser.sin%portGhtons(port)E ser.sin%addr.s%addrGhtonl(IN+""/%+N)E printf(JBnter the Bthernet address,J)E scanf(JQsJ!Mcon)E for(DGFED?$EDRR) C if(strcmp(con!eth2D3)GGF) C printf(JIP +ddress is,QsKnJ!ip2D3)E brea#E L L return FE L
OUTUT:
22
Bnter the Bthernet address,676.87.$:.F.F.6 IP +ddress is,676.F.F.6
RESULT,
Thus the +/P and /+/P &as implement successfully.
SIULATI I AD TRA6EROUTE 6OADS E7. o: 4 DATE: PING SERVER:
#include #include #include #include #include #include int main(int argc,char **argv) { int sock,chat=0; struct sockaddr_in add,addr; bzero(&add,sizeof(add)); add.sin_family=AF_INET; add.sin_addr.s_addr=htons(INADDR_ANY); add.sin_port=htons(5478); sock=socket(AF_INET,SOCK_STREAM,0); int b=bind(sock,(struct sockaddr *)&add,sizeof(add)); if(b<0) perror("no bind"); else perror("bind"); int lis=listen(sock,3); int nsd,addrlen; addrlen=sizeof(addr); nsd=accept(sock,(struct sockaddr*)&addr,&addrlen); printf("%d",nsd); if(nsd<0) perror("no accept"); else printf("connected,%d",nsd); int str,buf_size=1024; 23
char *buffer="This is the test message"; int s,ii=0; while(ii!=3) { s=send(nsd,buffer,buf_size,0); if(s<0) perror("send"); ii++; } close(sock); close(b); close(nsd); } OUTPUT:
[sivait@localhost ~]$ cc pingser.c [sivait@localhost ~]$ ./a.out bind: Success 4connected,4[sivait@localhost ~]$ PING CLIENT:
#include #include #include #include #include #include #include int main(int argc,char** argv) { int sock, i=10, r, s, buf_size=1024; struct sockaddr_in add; int c=0; bzero(&add,sizeof(add)); add.sin_family=AF_INET; add.sin_addr.s_addr=htonl(INADDR_ANY); add.sin_port=htons(5478); sock=socket(AF_INET,SOCK_STREAM,0); int con=connect(sock,(struct sockaddr*)&add,sizeof(add)); if(con<0) perror("network is unreachable"); char buffer[buf_size],buf[1024]; while(c!=4) { r=recv(sock,buf,buf_size,0); if(r<0) 24
{ perror("request is timed out"); printf("packet send=4;received=0;loss=100"); } else { printf("replay from %s",inet_ntoa(add.sin_addr)); printf("icmp_seq=%d:",i); printf("tt1=64\n"); i++; c++; } } printf("packet send=4;received=4;loss=0"); close(sock); } OUTPUT: Server:
[test@ritlinux ~]$ ./a.out bind: Success 4connected,4[test@ritlinux ~]$ Client:
[test@ritlinux ~]$ cc pingcli.c [test@ritlinux ~]$ ./a.out replay from 0.0.0.0icmp_seq=10:tt1=64 replay from 0.0.0.0icmp_seq=11:tt1=64 replay from 0.0.0.0icmp_seq=12:tt1=64 replay from 0.0.0.0icmp_seq=13:tt1=64 packet send=4;received=4;loss=0[test@ritlinux ~]$
25
!. SO6ET FOR HTT FOR 5EC AE ULOAD AD DO5LOAD E7. o: !.1
ILEETATIO OF HTT AI:
To &rite a program for implementing HTTP.
ETHODOLOY: HTT:
Hyperte=t Transfer Protocol (HTTP) is an application4level protocol for distributed! collaborative! hypermedia information systems.263 Its use for retrieving inter4lin#ed resources! called hyperte=t documents. There are t&o maDor versions! HTTPA6.F that uses a separate connection for every document and HTTPA6.6 that can reuse the same connection to do&nload! for the Dust served page. Hence HTTPA6.6 may be faster as it ta#es time to set up such connections.
HTTP is a reuestAresponse standard as is typical in client4server computing. The client is an application (e.g. &eb bro&ser! spider etc) on the computer used by an end4user! the server is an 26
application running on the computer hosting the &eb site. The client&hich submits HTTP reuests is also referred to as the user agent . The responding server&hich stores or creates resources such as HT- files and imagesmay be called the origin server .
Algorith,: S%r9%r:
6. Start the program. 7. *reate the soc#et and accept the connection from the client. 6li%nt:
6. Start the program. 7. *reate the soc#et and *onnect to the server using connect() function. 8. Send the file name the get reuest to the server
HTT SER
>include?stdio.h@ >include?sysAsoc#et.h@ >include?netinetAin.h@ >include?string.h@ int main() C IB fE int sd!len!iGF!l!s!sd6!si!eofE struct soc#addr%in serE struct soc#addr%in cliE char buf2:F3!msg2:F3!rmsg2:F3E if((sdGsoc#et(+%INBT!SO*%ST/B+-!F))?F) C printf(JBrrorJ)E return FE L b0ero(Mser!si0eof(ser))E 27
ser.sin%familyG+%INBTE ser.sin%portGhtons(WWY)E ser.sin%addr.s%addrGhtonl(IN+""/%+N)E if((bind(sd!(struct soc#addr)Mser!si0eof(ser)))?F) C printf(Jinding errorJ)E return FE L if((listen(sd!:))?F) C printf(Jistening errorJ)E return FE L siGsi0eof(cli)E if((sd6Gaccept(sd!(struct soc#addr)Mcli!Msi))?F) C printf(J+ccepting errorJ)E return FE L recv(sd6!msg!:F!F)E printf(JKnThe ile Name Is QsKnJ!msg)E fGfopen(msg!JrJ)E do C b0ero(Mbuf!:F)E fread(Mbuf!:F!6!f)E send(sd6!buf!:F!F)E AAgetchar()E L&hile(feof(f))E send(sd6!J*O-PBTB"J!7F!F)E fclose(f)E close(sd6)E L 28
HT 6LIET
>include?stdio.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?netinetAin.h@ >include?string.h@ main() C IB fpE int sd!iGF!len!eofE struct soc#addr%in serE char buf2:F3!rmsg2:F3!source26:3!dest26:3E if((sdGsoc#et(+%INBT!SO*%ST/B+-!F))?F) C printf(JBrror on Soc#et creationJ)E return FE L b0ero(Mser!si0eof(ser))E ser.sin%familyG+%INBTE ser.sin%portGhtons(WWY)E ser.sin%addr.s%addrGhtonl(IN+""/%+N)E if((connect(sd!(struct soc#addr )Mser!si0eof(ser)))GG46) C printf(JBrror on *onnectJ)E return FE L printf(JBnter the source file name,J)E scanf(JQsJ!source)E printf(JKnBnter the "estination file name,J)E scanf(JQsJ!dest)E send(sd!source!:F!F)E fpGfopen(dest!J&J)E 29
recv(sd!rmsg!:F!F)E &hile((strcmp(rmsg!J*O-PBTB"J))GF) C
f&rite(Mrmsg!strlen(rmsg)!6!fp)E
b0ero(Mrmsg!:F)E
recv(sd!rmsg!:F!F)E L printf(Jile is receivedKnJ)E
fclose(fp)E close(sd)E L
sour'%.ht,l
?HT-@ ?HB+"@ ?TITB@ HTTP Program ?ATITB@ ?O"@ ?font faceG JTimes Ne& /omanJ@ ?*BNTB/@?H6@ HO-B P+]B?AH6@ ?-+/UBB ]*OO/GJPINJ "I/B*TIONGJ/I]HTJ@?ONT *OO/GJgreenJ@ ?@?I@ *O-PUTB/ NBT'O/ ?AI@?@?A-+/UBB@?AONT@ ?AO"@ ?AHT-@
Out$ut: S%r9%r:
2ritcselinu= 3V cc httpserver.c 2rit cselinu= 3V .Aa.out 30
The ile Name Is source.html 2rit cselinu= 3V
6li%nt:
2rit cselinu= httpclient3V cc httpclient.c 2rit cselinu= httpclient3V .Aa.out Bnter the source file name,source.html
Bnter the "estination file name,dest.html ile is received 2ritcselinu= httpclient3V
D%st.ht,l:
?HT-@ ?HB+"@ ?TITB@ HTTP Program ?ATITB@ ?O"@ ?font faceG JTimes Ne& /omanJ@ ?*BNTB/@?H6@ HO-B P+]B?AH6@ ?-+/UBB ]*OO/GJPINJ "I/B*TIONGJ/I]HTJ@?ONT *OO/GJgreenJ@ ?@?I@ PS/ BN]INBB/IN] *OB]B ?AI@?@?A-+/UBB@?AONT@ ?AO"@ ?AHT-@ R%sult:
Thus the program to implement HTTP &as done and output &as verified.
31
TO ILEET R6 =REOTE RO6EDURE 6ALL> E7. o: & DATE: Ai,: To i,$l%,%nt R%,ot% ro'%dur% 'll R6 S%r9%r: >include?stdio.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?arpaAinet.h@ >include?stdlib.h@ >include?string.h@ >define PO/T ;;;; void main() C int soc#6!soc#7!iGPO/T!l!=!y!0EE char b27:3!a27:3!c27:3E struct soc#addr%in myaddr!clientE if((soc#6Gsoc#et(+%INBT!SO*%ST/B+-!F))GG46) C printf(JKnKnSoc#et *reation Brror.J)E L myaddr.sin%familyG+%INBTE myaddr.sin%portGhtons(PO/T)E myaddr.sin%addr.s%addrGhtonl(IN+""/%+N)E if(bind(soc#6!(struct soc#addr)Mmyaddr!si0eof(struct soc#addr))G46) C printf(JKnKnSoc#et is binded.J)E L else C printf(JKninding error.J)E L if(listen(soc#6!6F)G46) C printf(JKnistening Successful.J)E L else C printf(JKnistening error..J)E L lGsi0eof(client)E if((soc#7Gaccept(soc#6!(struct soc#addr)Mclient!Ml))G46) C printf(JKn*onnection accepted from client.J)E L else C 32
printf(JKn+ccepting error.J)E L do C read(soc#7!a!:)E if(strcmp(JBN"J!a)GGF) e=it(F)E read(soc#7!b!:)E read(soc#7!c!:)E =Gatoi(b)E yGatoi(c)E 0Gatoi(a)E s&itch(0) C case 6,0G=RyE brea#E case 7,0G=4yE brea#E case 8,0G=yE brea#E case $,0G=AyE brea#E L sprintf(a!JQdJ!0)E &rite(soc#7!a!:)E L&hile(6)E shutdo&n(soc#7!7)E close(soc#6)E L R$' 6li%nt: >include?stdio.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?arpaAinet.h@ >include?string.h@ >include?stdlib.h@ >define PO/T ;;;; void main() C int soc#6!iGPO/T!lE char a26F3!b26F3E struct soc#addr%in myaddr!clientE if((soc#6Gsoc#et(+%INBT!SO*%ST/B+-!F))GG46) C printf(JKnKnSoc#et *reation Brror.J)E L myaddr.sin%familyG+%INBTE myaddr.sin%portGhtons(PO/T)E 33
myaddr.sin%addr.s%addrGhtonl(IN+""/%+N)E lGconnect(soc#6!(struct soc#addr)Mmyaddr!si0eof(myaddr))E if(l?F) printf(JKnBrror in connection.J)E else printf(JKn*onnected.J)E do C printf(JKnBnter the operation to be performed,KnKt6.+dditionKnKt7.SubtractionKnKt8.-ultiplicationKnKt$."ivisionKnBnter BN" to end.J)E scanf(JQsJ!a)E &rite(soc#6!a!strlen(a))E if(strcmp(a!JBN"J)GGF) e=it(F)E printf(JKnBnter the t&o operands,KnJ)E scanf(JQs QsJ!a!b)E &rite(soc#6!a!strlen(a))E &rite(soc#6!b!strlen(b))E read(soc#6!a!strlen(a))E printf(JKnThe result is QsJ!a)E L&hile(6)E shutdo&n(soc#6!7)E close(soc#6)E L
RESULT,
Thus the /P* &as implement successfully.
34
) ALI6ATIOS USI T6 SO6ETS E6HO 6LIET AD E6HO SER
The Transmission *ontrol Protocol is one of the core protocols of the Internet Protocol Suite. T*P operates at a higher level! concerned only &ith the t&o end systems. T*P provides reliable! ordered delivery of a stream of bytes from a program on one computer to another program on another computer. T*P controls segment si0e! flo& control! the rate at &hich data is e=changed! and net&or# traffic congestion. T*P protocol operations may be divided into three phases. *onnections must be properly established in a multi4step handsha#e process (connection establishment) before entering the data transfer phase. +fter data transmission is completed! the connection termination closes established virtual circuits and releases all allocated resources.
S%r9%r: Step 6, start a program.
Step 7, create a soc#et &ith address family +%INBT! type protocol.
SO*%ST/B+- and default
35
Step 8, Initiali0e the soc#et and assign any port number. Step $, ind the server to the soc#et using bind() function. Step :, Bstablish the listen ueue using the listen function. Step ;, &ait for client program on reuest! establish a connection using accept function. Step W, /epeat steps 46F until the server sends e=it. Step , /eceive the message from the client using recv() and display the message. Step Y, If the client message is e=it go to step 66. Step 6F, ]et the server message and send it to client using send() function. Step 66, close the connection. Step 67, ]o to step ;. 6li%nt:
Step 6, Start. Step 7, *reate a soc#et &ith address family +%INBT type SO*%STB/+- and default protocol. Step 8, Initiali0e the soc#et and set it attributes set the reuired port number. Step $, *onnect to the server using connect() function to initiali0e the reuest. Step :, /epeat steps ;4 until server sends bye Step ;, +ccept the client message and send it to the server using send function. Step W, If the client message is e=it go to step Y. Step , /eceive the message from the server using recv() and display the message. Step Y, stop.
T6SER
>include?stdio.h@ >include?string.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?netinetAin.h@ >include?arpaAinet.h@ >include?unistd.h@ >include?stdlib.h@ int main() C int sid!bd!lis!acptd!rd!sdE struct soc#addr%in sinE char buf26F3E soc#len%t lenE sidGsoc#et(P%INBT!SO*%ST/B+-!F)E if(sid GG 46) C printf(JKn Brror in creating soc#et KnJ)E e=it(46)E L printf(JKn The soc#et is created successfullyJ)E memset(Msin!F!si0eof(sin))E sin.sin%familyG+%INBTE sin.sin%portGhtons(:66F)E sin.sin%addr.s%addrGhtonl(IN+""/%+N)E bdGbind(sid!(struct soc#addr)Msin!si0eof(sin))E if(bd ? F) C printf(JKn inding failed KnJ)E e=it(46)E L printf(JKn inding *ompletedJ)E lisGlisten(sid!:)E if(lis ? F) C printf(JKn Brror in listening KnJ)E e=it(46)E L printf(JKn istening is completedJ)E lenGsi0eof(sin)E acptdGaccept(sid!(struct soc#addr)Msin!Mlen)E if(accept ? F) C 37
printf(JKn Brror in +ccepting KnJ)E e=it(46)E L printf(JKn *lient is +ccepted KnJ)E do C rdGread(acptd!buf!6F)E if(rd ? F) C printf(JKn Brror in receiving the date from the client KnJ)E e=it(46)E L printf(J -essage received from client, Qs KnJ!buf)E sdGsend(acptd!buf!6F!F)E if(sd ? F) C printf(JKn Brror in sending the data to the client KnJ)E e=it(46)E L L&hile(strcasecmp(buf!JBN"J)GF)E shutdo&n(sid!7)E close(sid)E L T66li%nt :
>include?stdio.h@ >include?string.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?netinetAin.h@ >include?arpaAinet.h@ >include?unistd.h@ >include?stdlib.h@ int main() C int sid!con!sd!rdE struct soc#addr%in sinE char buf26F3E sidGsoc#et(P%INBT!SO*%ST/B+-!F)E if(sid GG 46) C printf(JKn Brror in creating the soc#et KnJ)E e=it(46)E L printf(JKn Soc#et created successfullyJ)E 38
memset(Msin!F!si0eof(sin))E sin.sin%familyG+%INBTE sin.sin%portGhtons(:66F)E sin.sin%addr.s%addrGhtonl(IN+""/%+N)E conGconnect(sid!(struct soc#addr)Msin!si0eof(sin))E if(con ? F) C printf(JKn The connection established falied KnJ)E e=it(46)E L printf(JKn *onnection established successfully KnJ)E do C printf(J Bnter the string, J)E scanf(JQsJ!buf)E sdGsend(sid!buf!6F!F)E if(sd ? F) C printf(JKn Brror in sending the data to the server KnJ)E e=it(46)E L rdGread(sid!buf!6F)E if(rd ? F) C printf(JKn Brror in reading the data send by the server KnJ)E e=it(46)E L printf(J The content received from sender is, Qs KnJ!buf)E L&hile(strcasecmp(buf!JBN"J)GF)E shutdo&n(sid!7)E close(sid)E L Out$ut:
2testritlinu= 3V cc tcpechoser.c 2testritlinu= 3V .Aa.out The soc#et is created successfully inding *ompleted istening is completed *lient is +ccepted -essage received from client, hai -essage received from client, hello 39
-essage received from client, BN" 2testritlinu= 3V cc tcpechocli.c 2testritlinu= 3V .Aa.out Soc#et created successfully *onnection established successfully Bnter the string, hai The content received from sender is, hai Bnter the string, hello The content received from sender is, hello Bnter the string, BN" The content received from sender is, BN"
40
E7. o: ).2 DATE:
6HAT Ai,: To I,$l%,%nt Hl/ du$l%7 nd Full du$l%7 using T6 HALF DULE 6HAT USI T6#I
>include?sysAtypes.h@ >include?stdio.h@ >include?netdb.h@ >include?sysAsoc#et.h@ >include?arpaAinet.h@ >include?unistd.h@ >include?netinetAin.h@ int main(int argc!char argv23) C int n!sd!adE struct soc#addr%in servaddr!cliaddrE soc#len%t clilen!servlenE char buff26FFFF3!buff bu ff26FFFF3!buff626FFFF3E 626FFFF3E b0ero(Mservaddr!si0eof(servaddr))E b0ero(Mservaddr!si0eof(servaddr))E #GSo'(%t ddr%ss stru'tur%G#
servaddr.sin%familyG+%INBTE servaddr.sin%addr.s%addrGhtonl(IN+""/%+N)E servaddr.sin%portGhtons(:FFF)E #GT6 so'(%t is 'r%t%d@ n Int%rn%t so'(%t ddr%ss stru'tur% is /ill%d 8ith 8ild'rd ddr%ss s%r9%rJs 8%ll (no8n $ortG#
sdGsoc#et(+%INBT!SO*%ST/B+-!F)E #GCind /un'tion ssigns lo'l $roto'ol ddr%ss to th% so'(%tG#
bind(sd!(struct soc#addr)Mservaddr!si0eof(servaddr))E soc#addr)Mservaddr!si0eof(servaddr))E #GList%n /un'tion s$%'i/i%s th% ,7i,u, nu,%r o/ 'onn%'tions tht (%rn%l should u%u% /or this so'(%tG#
listen(sd!:)E printf(JQsKnJ!Jserver is runningJ)E runningJ)E #GTh% s%r9%r to r%turn th% n%7t 'o,$l%t%d 'onn%'tion /ro, th% /ront o/ th% 'o,$l%t%d 'onn%'tion Ku%u% 'lls itG# 41
adGaccept(sd!(struct soc#addr)Mcliaddr!Mclilen)E soc#addr)Mcliaddr!Mclilen)E &hile(6) C b0ero(Mbuff!si0eof(buff))E b0ero(Mbuff!si0eof(buff ))E #GR%'%i9ing th% r%u%st /ro, 'li%ntG#
recv(ad!buff!si0eof(buff)!F)E printf(J/eceive from the client,QsKnJ!buff)E client,QsKnJ!buff)E nG6E &hile(nGG6) C b0ero(Mbuff6!si0eof(buff6))E b0ero(Mbuff6!si0eof(buff 6))E printf(JQsKnJ!JBnter the input data,J)E #GR%d th% ,%ssg% /ro, 'li%ntG#
fgets(buff6!6FFFF!stdin)E #GS%nds th% ,%ssg% to 'li%ntG#
send(ad!buff6!strlen(buff6)R6!F)E printf(JQsKnJ!J"ata sentJ)E nGnR6E L L return FE L 6li%nt: h'li%nt.'
>include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?arpaAinet.h@ >include?netinetAin.h@ >include?unistd.h@ >include?stdio.h@ >include?netdb.h@ int main(int argc!char argv23) C int n!sd!cdE 42
struct soc#addr%in servaddr!cliaddrE soc#len%t servlen!clilenE char buff26FFFF3!buff bu ff26FFFF3!buff626FFFF3E 626FFFF3E b0ero(Mservaddr!si0eof(servaddr))E b0ero(Mservaddr!si0eof(servaddr))E #GSo'(%t ddr%ss stru'tur%G#
servaddr.sin%familyG+%INBTE servaddr.sin%addr.s%addrGinet%addr(argv263)E servaddr.sin%portGhtons(:FFF)E #G6r%ting so'(%t@ ssigning I ddr%ss nd $ort nu,%r /or tht so'(%tG#
sdGsoc#et(+%INBT!SO*%ST/B+-!F)E #G6onn%'t %stlish%s 'onn%'tion 8ith th% s%r9%r using s%r9%r I ddr%ssG#
cdGconnect(sd!(struct soc#addr)Mservaddr!si0eof(servaddr))E soc#addr)Mservaddr!si0eof(servaddr))E &hile(6) C b0ero(Mbuff!si0eof(buff))E b0ero(Mbuff!si0eof(buff ))E printf(JQsKnJ!JBnter the input data,J)E #GThis /un'tion is us%d to r%d /ro, s%r9%rG#
fgets(buff!6FFFF!stdin)E #GS%nd th% ,%ssg% to s%r9%rG#
send(sd!buff!strlen(buff)R6!F)E printf(JQsKnJ!J"ata sentJ)E nG6E &hile(nGG6) C b0ero(Mbuff6!si0eof(buff6))E b0ero(Mbuff6!si0eof(buff 6))E #GR%'%i9% th% ,%ssg% /ro, s%r9%rG#
recv(sd!buff6!si0eof(buff6)!F)E printf(J/eceived from the server,QsKnJ!buff6)E server,QsKnJ!buff6)E nGnR6E L L return FE L 43
SALE OUTUT: S%r9%r , =Host ,%:Root1>
2rootlocalhost $ita883> vi hserver.c 2rootlocalhost $ita883> cc hserver.c 2rootlocalhost $ita883> .Aa.out Server is running /eceive from the client,hi Bnter the input data, ho& are u da .. "ata sent /eceive from the client,me fine da ... Bnter the input data, 6li%nt: =Host ,%:Root2>
2rootlocalhost $ita883> vi hclient.c 2rootlocalhost $ita883> cc hclient.c 2rootlocalhost $ita883> .Aa.out 67W.F.F.6 Bnter the input data, hi "ata sent, /eceived from the server,ho& are u da .. Bnter the input data, me fine da ... "ata sent
R%sult : Thus the chat application full duple= communication is established by sending the reuest
from the client to the server! server gets the message and gives response to the client and prints it. FULL DULE 6HAT USI T6#I
>include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?stdio.h@ 44
>include?unistd.h@ >include?netdb.h@ >include?arpaAinet.h@ >include?netinetAin.h@ int main(int argc!char argv23) C int ad!sdE struct soc#addr%in servaddr!cliaddrE soc#len%t servlen!clilenE char buff26FFF3!buff626FFF3E pid%t cpidE b0ero(Mservaddr!si0eof(servaddr))E #GSo'(%t ddr%ss stru'tur%G# servaddr.sin%familyG+%INBTE servaddr.sin%addr.s%addrGhtonl(IN+""/%+N)E servaddr.sin%portGhtons(::FF)E #GT6 so'(%t is 'r%t%d@ n Int%rn%t so'(%t ddr%ss stru'tur% is /ill%d 8ith 8ild'rd ddr%ss s%r9%rJs 8%ll (no8n $ortG# sdGsoc#et(+%INBT!SO*%ST/B+-!F)E #GCind /un'tion ssigns lo'l $roto'ol ddr%ss to th% so'(%tG# bind(sd!(struct soc#addr)Mservaddr!si0eof(servaddr))E #GList%n /un'tion s$%'i/i%s th% ,7i,u, nu,%r o/ 'onn%'tions tht (%rn%l should u%u% /or this so'(%tG# listen(sd!:)E printf(JQsKnJ!JServer is running.......J)E #GTh% s%r9%r to r%turn th% n%7t 'o,$l%t%d 'onn%'tion /ro, th% /ront o/ th% 'o,$l%t%d 'onn%'tion Ku%u% 'lls itG# adGaccept(sd!(struct soc#addr)Mcliaddr!Mclilen)E #GFor( s"st%, 'll is us%d to 'r%t% n%8 $ro'%ssG# cpidGfor#()E if(cpidGGF) C &hile(6) C b0ero(Mbuff!si0eof(buff))E #GR%'%i9ing th% r%u%st /ro, 'li%ntG# recv(ad!buff!si0eof(buff)!F)E printf(J/eceived message from the client,QsKnJ!buff)E L L else C 45
&hile(6) C b0ero(Mbuff6!si0eof(buff6))E printf(JQsKnJ!JBnter the input data,J)E #GR%d th% ,%ssg% /ro, 'li%ntG# fgets(buff6!6FFFF!stdin)E #GS%nds th% ,%ssg% to 'li%ntG# send(ad!buff6!strlen(buff6)R6!F)E printf(JQsKnJ!J"ata sentJ)E L L return FE L 6li%nt: /'li%nt.' >include?sysAsoc#et.h@ >include?sysAtypes.h@ >include?stdio.h@ >include?arpaAinet.h@ >include?unistd.h@ >include?netdb.h@ >include?netinetAin.h@ int main(int argc!char argv23) C int sd!cdE struct soc#addr%in servaddr!cliaddrE soc#len%t servlen!clilenE char buff26FFF3!buff626FFF3E pid%t cpidE b0ero(Mservaddr!si0eof(servaddr))E servaddr.sin%familyG+%INBTE servaddr.sin%addr.s%addrGinet%addr(argv263)E servaddr.sin%portGhtons(::FF)E #G6r%ting so'(%t@ ssigning I ddr%ss nd $ort nu,%r /or tht so'(%tG# sdGsoc#et(+%INBT!SO*%ST/B+-!F)E #G6onn%'t %stlish%s 'onn%'tion 8ith th% s%r9%r using s%r9%r I ddr%ssG# cdGconnect(sd!(struct soc#addr)Mservaddr!si0eof(servaddr))E #GFor( is us%d to 'r%t% n%8 $ro'%ssG# cpidGfor#()E if(cpidGGF) C &hile(6) C 46
b0ero(Mbuff!si0eof(buff))E printf(JQsKnJ!JBnter the input data,J)E #GThis /un'tion is us%d to r%d /ro, s%r9%rG# fgets(buff!6FFFF!stdin)E printf(JQsKnJ!J"ata sentJ)E #GS%nd th% ,%ssg% to L s%r9%rG# send(sd!buff!strlen(buff)R6!F)E L el &hile(6) se C C b0ero(Mbuff6!si0eof(buff6))E L #GR%'%i9% th% ,%ssg% /ro, s%r9%rG# recv(sd!buff6!si0eof(buff6)!F)E return FE printf(J/eceived message from the server, L QsKnJ!buff6)E SALE OUTUT: L S%r9%r , =Host ,%:Root1> 2rootlocalhost $ita883> vi fserver.c 2rootlocalhost $ita883> cc fserver.c 2rootlocalhost $ita883> .Aa.out Server is running....... Bnter the input data, /eceived message from the client,hi ho& are u "ata sent Bnter the input data, /eceiv 6li%nt: =Host ,%:Root2> 2rootlocalhost $ita883> vi fclient.c 2rootlocalhost $ita883> cc fclient.c 2rootlocalhost $ita883> .Aa.out 67W.F.F.6 Bnter the input data, hi "ata sent Bnter the input data, /eceived message from the server,ho& are u i am fine "ata sent Bnter the input data, R%sult: 47
Thus the chat application full duple= communication is established by sending the reuest from the client to the server! server gets the message and gives response to the client and prints it. ed message from the client,i am fine
48
FILE TRASFER E7. o: *.3 DATE: Ai,: To i,$l%,%nt Fil% trns/%r S%r9%r: /t$s.' >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?sysAstat.h@ >include?arpaAinet.h@ >include?netinetAin.h@ >include?netdb.h@ >include?unistd.h@ >include?stdio.h@
include?string.h@ int main(int argc!char argv23) C int sd!ad!si0eE struct soc#addr%in servaddr!cliaddrE soc#len%t clilenE clilenGsi0eof(cliaddr)E struct stat =E char buff26FF3!file26FFFF3E IB fpE b0ero(Mservaddr!si0eof(servaddr))E servaddr.sin%familyG+%INBTE servaddr.sin%addr.s%addrGhtonl(IN+""/%+N)E servaddr.sin%portGhtons(6:FF)E sdGsoc#et(+%INBT!SO*%ST/B+-!F)E bind(sd!(struct soc#addr)Mservaddr!si0eof(servaddr))E listen(sd!:)E printf(JQsKnJ!JServer Is /unning....J)E adGaccept(sd!(struct soc#addr)Mcliaddr!Mclilen)E &hile(6) C b0ero(buff!si0eof(buff))E b0ero(file!si0eof(file))E recv(ad!buff!si0eof(buff)!F)E fpGfopen(buff!JrJ)E stat(buff!M=)E si0eG=.st%si0eE fread(file!si0eof(file)!6!fp)E send(ad!file!si0eof(file)!F)E 49
L L 6li%nt: /t$'.' >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?netinetAin.h@ >include?arpaAinet.h@ >include?netdb.h@ >include?stdio.h@ >include?unistd.h@ int main(int argc!char argv23) C int sd!cdE struct soc#addr%in servaddr!cliaddrE soc#len%t clilenE char buff26FF3!file26FFFF3E struct hostent hE hGgethostbyname(argv263)E b0ero(Mservaddr!si0eof(servaddr))E servaddr.sin%familyGh4@h%addrtypeE memcpy((char )Mservaddr.sin%addr.s%addr!h4@h%addr%list2F3!h4@h%length)E servaddr.sin%portGhtons(6:FF)E sdGsoc#et(+%INBT!SO*%ST/B+-!F)E cdGconnect(sd!(struct soc#addr)Mservaddr!si0eof(servaddr))E &hile(6) C printf(JQsKnJ!JBnter the ile Name ,J)E scanf(JQsJ!buff)E send(sd!buff!strlen(buff)R6!F)E printf(JQsKnJ!Jile Output ,J)E recv(sd!file!si0eof(file)!F)E printf(JQsJ!file)E L return FE L SALE OUTUT: S%r9%r , =Host ,%:Root1> 2rootlocalhost $ita883> vi ftps.c 2rootlocalhost $ita883> cc ftps.c 2rootlocalhost $ita883> .Aa.out Server is /unning IB /B+*HB" ile output , this is my net&or# lab 50
6li%nt: =Host ,%:Root2> 2rootlocalhost $ita883> vi ftpc.c 2rootlocalhost $ita883> cc ftpc.c 2rootlocalhost $ita883> .Aa.out Bnter the filename, ita.t=t
Sending the file content "ata sent.....
R%sult, Thus the TP client4server communication is established and data is transferred bet&een the client and server machines.
51
+. ALI6ATIOS USI T6 AD UD SO6ETS E7. o: +.1 DATE: AI:
To &rite a c program to implement "NS application.
ETHODOLOY: D%/inition
The "omain Name System ("NS) is basically a large database &hich resides on various computers and it contains the names and IP addresses of various hosts on the internet and various domains. The "omain Name System is similar to a file system in UNIX or "OS starting &ith a root. ranches attach to the root to create a huge set of paths. Bach branch in the "NS is called a label. The domain name system database is divided into sections called 0ones. The name servers in their respective 0ones are responsible for ans&ering ueries for their 0ones. + 0one is a subtree of "NS and is administered separately. There are multiple name servers for a 0one. There is usually one primary name server and one or more secondary name servers. + name server may be authoritative for more than one 0one. "NS names are assigned through the Internet /egistries by the Internet +ssigned Number +uthority (I+N+). The domain name is a name assigned to a domain. or e=ample! mycollege.edu represents the domain name of an educational institution.Three main components of "NS •
•
•
/esolver Name server "atabase of resource records(//s)
In UNIX the resolver is accessed by using the library functions JgethostbynameJ and JgethostbyaddrJ. The resolver &ill send reuests to the name servers to return information reuested by the user. The reuesting computer tries to connect to the name server using its IP address rather than the name.
52
Stru'tur% nd ,%ssg% /or,t:
The dra&ing belo& sho&s a partial "NS hierarchy. +t the top is &hat is called the root and it is the start of all other branches in the "NS tree. It is designated &ith a period. Bach branch moves do&n from level to level. 'hen referring to "NS addresses! they are referred to from the bottom up &ith the root designator (period) at the far right. B=ample, Jmyhost.mycompany.com.[
Algorith,: S%r9%r: 6. Start the program. 7. *reate the soc#et! establish a connection using accept function. 8. ]et the domain name from the client. $. -atch the domain name to the corresponding IP address and send it to client. :. *lose the connection. ;. Stop the program. 6li%nt:
6. Start the program. 7. *reate the soc#et and *onnect to the server using connect() function. 8. Send the domain name to the "NS server. $. /eceive the corresponding IP address from the server. :. "isplay the address. ;. *lose the soc#et. 53
DS S%r9%r1:
>include?stdio.h@ >include?string.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?arpaAinet.h@ >include?netinetAin.h@ >include?unistd.h@ >include?time.h@ int main() C int sid!i!D!len!#GF!sid6E char buf627FF3!buf727F3!buf827F3!buf27FF3!msg27FF3E struct soc#addr%in sin!sin6E sidGsoc#et(P%INBT!SO*%"]/+-!F)E memset(Msin!F!si0eof(sin))E sin.sin%familyG+%INBTE sin.sin%portGhtons(:666)E sin.sin%addr.s%addrGhtonl(IN+""/%+N)E lenGsi0eof(sin)E bind(sid!(struct soc#addr )Msin!len)E recvfrom(sid!Mmsg!si0eof(msg)!F!(struct soc#addr )Msin!Mlen)E if(strcasecmp(msg!J]mailJ)) C strcpy(buf!J67$.67$.7:7.668J)E strcpy(buf6!JServer6J)E L else if(strcasecmp(msg!JahoomailJ)) C strcpy(buf!J7:.7::.6;Y.;J)E strcpy(buf6!Jserver6J)E L else if(strcasecmp(msg!J/ediffmailJ)) C strcpy(buf!J77:.6W;.6;.7:$J)E 54
strcpy(buf6!Jserver6J)E L else C strcpy(buf6!Jserver7J)E sid6Gsoc#et(P%INBT!SO*%"]/+-!F)E memset(Msin6!F!si0eof(sin6))E sin6.sin%familyG+%INBTE sin6.sin%portGhtons($666)E sin6.sin%addr.s%addrGhtonl(IN+""/%+N)E lenGsi0eof(sin6)E sendto(sid6!Mmsg!si0eof(msg)!F!(struct soc#addr )Msin6!len)E recvfrom(sid6!Mbuf!si0eof(buf)!F!(struct soc#addr )Msin6!Mlen)E L sendto(sid!Mbuf!si0eof(buf)!F!(struct soc#addr )Msin!len)E sendto(sid!Mbuf6!si0eof(buf)!F!(struct soc#addr )Msin!len)E printf(JKn "isconnected KnJ)E close(sid)E shutdo&n(sid!F)E L DS S%r9%r2.'
>include?stdio.h@ >include?string.h@ >include?sysAtypes.h@ >include?sysAsoc#et.h@ >include?arpaAinet.h@ >include?netinetAin.h@ >include?unistd.h@ >include?time.h@ int main() C int sid!i!D!len!#GFE char buf627F3!buf727F3!buf827F3!buf27FF3!msg27FF3E struct soc#addr%in sinE sidGsoc#et(P%INBT!SO*%"]/+-!F)E 55
memset(Msin!F!si0eof(sin))E sin.sin%familyG+%INBTE sin.sin%portGhtons($666)E sin.sin%addr.s%addrGhtonl(IN+""/%+N)E lenGsi0eof(sin)E bind(sid!(struct soc#addr )Msin!len)E recvfrom(sid!Mmsg!si0eof(msg)!F!(struct soc#addr )Msin!Mlen)E if(strcasecmp(msg!J'i#ipediaJ)) C strcpy(buf!J7F.F.6:7.7J)E sendto(sid!Mbuf!si0eof(buf)!F!(struct soc#addr )Msin!len)E L else if(strcasecmp(msg!J]oogleJ)) C strcpy(buf!J67;.8.6:.7$:J)E sendto(sid!Mbuf!si0eof(buf)!F!(struct soc#addr )Msin!len)E L else if(strcasecmp(msg!JingJ)) C strcpy(buf!J.7F.67:.7FJ)E sendto(sid!Mbuf!si0eof(buf)!F!(struct soc#addr )Msin!len)E L else C strcpy(buf!JNotfoundJ)E sendto(sid!Mbuf!si0eof(buf)!F!(struct soc#addr )Msin!len)E L printf(JKn "isconnected KnJ)E close(sid)E shutdo&n(sid!F)E L DS6li%nt
>include?stdio.h@ >include?string.h@ >include?sysAtypes.h@ 56
>include?sysAsoc#et.h@ >include?arpaAinet.h@ >include?netinetAin.h@ >include?unistd.h@ int main() C int sid!i!D!len!#GF!sid6E char buf27FF3!msg27FF3!buf627FF3E struct soc#addr%in sin!sin6E sidGsoc#et(P%INBT!SO*%"]/+-!F)E memset(Msin!F!si0eof(sin))E sin.sin%familyG+%INBTE sin.sin%portGhtons(:666)E sin.sin%addr.s%addrGhtonl(IN+""/%+N)E lenGsi0eof(sin)E printf(JKnBnter &ebsite name,J)E scanf(JQsJ!msg)E sendto(sid!Mmsg!si0eof(msg)!F!(struct soc#addr )Msin!len)E recvfrom(sid!Mbuf!si0eof(buf)!F!(struct soc#addr )Msin!Mlen)E recvfrom(sid!Mbuf6!si0eof(buf)!F!(struct soc#addr )Msin!Mlen)E printf(JIP,QsKt rom4@QsKnJ!buf!buf6)E close(sid)E shutdo&n(sid!F)E L Out$ut
2testritlinu= 3V cc dnsser6.c 2testritlinu= 3V .Aa.out "isconnected 2testritlinu= 3V cc dnsser7.c 2testritlinu= 3V .Aa.out "isconnected 2testritlinu= 3V cc dnscli.c 2testritlinu= 3V .Aa.out Bnter &ebsite name,]oogle IP,67;.8.6:.7$:
rom4@server7 57
2testritlinu= 3V cc dnscli.c 2testritlinu= 3V .Aa.out Bnter &ebsite name,hotmail IP,Notfound
rom4@server7
R%sult:
Thus the program to implement "NS &as done and output &as verified
1-. STUDY OF ET5OR SIULATOR =S>. E7. o: 1-. DATE: 1-. S ; 2 O9%r9i%8 NS (Net&or# Simulartor) is an event driven net&or# simulator developed at U* er#eley that simulates variety of IP net&or#s. It implements net&or# protocols such as T*P and UP"! traffic source behavior such as TP! Telnet! 'eb! */ and /! router ueue management mechanism such as "rop Tail! /B" and *! routing algorithms such as "iD#stra! and more. NS also implements multicasting and some of the -+* layer protocols for +N simulations. The NS proDect is no& a part of the INT proDect that develops tools for simulation results display! analysis and converters that convert net&or# topologies generated by &ell4#no&n generators to NS formats. *urrently! NS (version 7) &ritten in *RR and OTcl (Tcl script language &ith ObDect4oriented e=tensions developed at -IT) is available.
Figur% 1. Simplified User5s ie& of NS +s sho&n in igure 6! in a simplified user5s vie&! NS is ObDect4oriented Tcl (OTcl) script interpreter that has a simulation event scheduler and net&or# component obDect libraries! and net&or# setup (plumbing) module libraries (actually! plumbing modules are implemented as member functions of the base simulator obDect). In other &ords! to use NS! you program in OTcl script language.. NS is &ritten not only in OTcl but in *RR also. or efficiency reason! NS separates the data path implementation from control path implementations. In order to reduce pac#et and event processing time (not simulation time)! the event scheduler and the basic net&or# component obDects in the data 58
path are &ritten and compiled using *RR. These compiled obDects are made available to the OTcl interpreter through an OTcl lin#age that creates a matching OTcl obDect for each of the *RR obDects and ma#es the control functions and the configurable variables specified by the *RR obDect act as member functions and member variables of the corresponding OTcl obDect. In this &ay! the controls of the *RR obDects are given to OTcl. It is also possible to add member functions and variables to a *RR lin#ed OTcl obDect. The obDects in *RR that do not need to be controlled in a simulation or internally used by another obDect do not need to be lin#ed to OTcl. i#e&ise! an obDect (not in the data path) can be entirely implemented in OTcl. igure 7 sho&s an obDect hierarchy e=ample in *RR and OTcl. One thing to note in the figure is that for *RR obDects that have an OTcl lin#age forming a hierarchy! there is a matching OTcl obDect hierarchy very similar to that of *RR.
Figur% 2. *RR and OTcl, The "uality igure 8 sho&s the general architecture of NS. In this figure a general user (not an NS developer) can be thought of standing at the left bottom corner! designing and running simulations in Tcl using the simulator obDects in the OTcl library. The event schedulers and most of the net&or# components are implemented in *RR and available to OTcl through an OTcl lin#age that is implemented using tclcl. The &hole thing together ma#es NS! &hich is a OO e=tended Tcl interpreter &ith net&or# simulator libraries.
Figur% 3. +rchitectural ie& of NS 'hen a simulation is finished! NS produces one or more te=t4based output files that contain detailed simulation data! if specified to do so in the input Tcl (or more specifically! OTcl) script. The data can be used for simulation analysis or as an input to a graphical simulation display tool called Net&or# +nimator (N+-) that is developed as a part of INT proDect. N+- has a nice graphical user interface similar to that of a *" player (play! fast for&ard! re&ind! pause and so on)! and also has a display speed controller. urthermore! it can graphically present information such as throughput and number of pac#et drops at each lin#! although the graphical information cannot be used for accurate simulation analysis. OT'l = O%'t Ori%nt%d T'l> no&ing that OTcl is ObDect4orieneted e=tension of Tcl! it is obvious that all Tcl commands &or# on OTcl 4 the relationship bet&een Tcl and Otcl is Dust same as * and *RR. Fil% n,% : ile name must have the e=tension Z.tcl[ S"nt7 : 59
1. Assigning 9lu% to 9ril% : Bg , a G 6F is &ritten as set a 6F Bg 7 , set X ZThis is a string[ Bg 8 , set y 6.:W 2. %tting th% 9ril% 9lu% : Bg , getting the value of variable 9a[ Va 3. E7$r%ssion : aRb is &ritten as 2e=pr Va R Vb3 4. Assigning th% 9lu% o/ n %7$r%ssion : c G aRb is &riiten as set c 2e=pr Va R Vb3 !. I/M. Els% stt%,%nt : if C V# ?6FL C puts Z#? 6F[ L else C puts Z @G 6F[ L &. S8it'h Stt%,%nt : Synta= , s&itch string { pattern1 body1 pattern! body!"""pattern# body# $ Bg , s&itch V= C ZV0[ C Set y6 2e=pr CVyR6L3 L ONB C Set y6 2e=pr CVyR6L3 T'O C Set y6 2e=pr CVy R 7L3 default C puts ZV= is not a match[ L L ). rinting , Eg , set name ZHello 'orld[ puts Vname It prints H%llo 5orld ot% : Bach time Zputs[ command is called! a ne& line is started. To avoid ne& line ! add Z4none&line[ after puts. 60
Bg , puts Z Sample ,[ puts 4none&line Vname &ill print S,$l% : H%llo 5orld *. rinting into /il% : Bg , &riting the value of Znumber[ into the file Zfilename[ $uts N/il%,% Nnu,%rP +. Tulting in /il% : It is done &ith Qt Bg , $uts N/il%,% Nnu,%rP Qt Nno2P 1-. For Loo$ : for( iG FE i?:E iRR) C 4444444444 4444444444 L can be &ritten as
for C set i FL CVi ? :L Cincr iL C 444444444 44444444 L L 11. 5hil% loo$ : S"nt7 : &hile condition body Bg , &hile CV= ?G :L C puts Z= G V=[ set y 2e=pr CV=R7L3 L 12. ro'%dur%# Fun'tion : S"nt7 : proc name CargsL C bodyL Bg , proc sum Carg6 arg7 L C set = 2e=pr CVarg6 R Varg7L3 return V= L 13. 6lling th% $ro'%dur% : *alling the above procedure is su, 2 ! 14. glol 'o,,nd: 61
glol command &ill causes a variable in a local scope (inside a procedure) to refer to the global variable of that name.
Bg , set a 6F 4444444 4444444 44444444 proc sum CbL C global a set = 2e=pr CVa R VbLL return V= L 1!. Running th% $rogr, The command for running the tcl program is ns filename.tcl
???????????????? Si,ulting Si,$l% %t8or(
1. 6r%ting Si,ultor O%'t : Bg , set ns 2ne& Simulator3
variable name
•
'hat this line does is the follo&ing, Initiali0e the pac#et format *reate a scheduler Select the default address format The JSimulatorJ obDect has member functions that do the follo&ing,
o o
*reate compound obDects such as nodes and lin#s *onnect net&or# component obDects created (e=. attach4agent)
o
Set net&or# component parameters (mostly for compound obDects)
o
*reate connections bet&een agents (e=. ma#e connection bet&een a JtcpJ and Jsin#J)
o
Specify N+- display options
o
Btc.
62
The JSimulatorJ obDect member function implementations are located in the Jns47AtclAlibAns4lib.tclJ file. 7 6r%ting nod% : Bg , set nF 2Vns node3 variable name for node number
simulator obDect
3. 6oloring nod% : Bg , VnF color red
$. Sh$% /or nod% : Bg,
Vn6 shape bo= or he=agon or circle (default)
!. 6r%ting lin( %t8%%n nod%s : Bg 6, n 0
Vns duple=4 lin# Simulator obDect
n 2
VnF
Vn7
lin# type
7-b
6Fms
node numbers speed
"ropTail ma=.delay
Pac#et dropping mechanism
Bg7 ,
n n 1 2 Vns simple=4lin# Vn6 Vn7 :-b 7F ms Dro$Til
Bg 8 , Vns simple=4lin# Vn6 Vn7 :-b 7F ms RED &. S%tting Ku%u% si% o/ lin( :
Bg , setting the ueue si0e of lin# bet&een node n7 and n8 to 6F Vns ueue4limit Vn7 Vn8 6F ). onitoring th% u%u% =/or A> : 63
Bg , -onitoring the ueue bet&een n7 and n8 at every F.: sec is Vns duple=4lin#4op Vn7 Vn8 ueuePos F.: *. S%tting u$ T6 'onn%'tion : Bg , +dding ZT*P[ agent to node ZnF[
ft p TCP
n 0 n 2
n 3
TCP Sink
n 1
Estlishing T6 Sour'% : set tcp 2ne& +gentAT*P3
variable name Vns attach4agent VnF Vtcp Estlishing T6 Sin( : set sin# 2ne& +gentAT*PSin#3
ariable name Vns attach4agent Vn8 Vsin# 6onn%'ting Sour'% nd Sin(:
Vns connect Vtcp Vsin#
+. S%tting u$ FT tr//i' o9%r T6 6onn%'tion:
set ftp 2ne& +pplicationATP3 variable name Vftp attach4agent Vtcp 1-. S%tting u$ UD 'onn%'tion : Bg , +dding ZU"P[ agent to node Zn6[
n 0
n 2
n 3
64
NULL n 1
UDP
cb r
Estlishing UD Sour'% : set udp 2ne& +gentAU"P3
variable name Vns attach4agent Vn6 Vudp Estlishing ULL Sin( : set null 2ne& +gentANull3
variable name Vns attach4agent Vn8 Vnull 6onn%'ting Sour'% nd Sin(: Vns connect Vudp Vnull 11. S%tting u$ 6CR Tr//i' o9%r UD 6onn%'tion:
set cbr 2ne& +pplicationATrafficA*/3 variable name Vcbr attach4agent Vudp 12. S%tting u$ 9lu%s /or Tr//i' : Eg : S%tting 9lu%s /or 6CR Tr//i' : Settin+ traffic type : Vcbr set type% */ Settin+ Packet Si(e : Vcbr set pac#et%si0e% 7FFF Settin+ Traffic 3ate : 65
Vcbr set rate% 6 mb 13. rinting 6CR $'(%t si% nd int%r9l: Eg : puts Z*/ pac#et si0e G 2Vcbr set pac#et%si0e%3[ puts Z*/ interval G 2Vcbr set interval%3[ 14. S%tting id /or Flo8 :
Bg , Setting lo& id for T*P *onnection , Vtcp set fid% 6 Bg7 , Setting lo& id for U"P *onnection , Vudp set fid% 7 1!. S%tting 'olors /or /lo8 : =/or A> Bg , Vns color 6 lue
lo& id Bg7, Vns color 7 /ed 1&. S'h%duling %9%nts /or Tr//i' : Bg , Vns at F.6 ZVcbr start[
Time
traffic
Bg7 , Vns at 6.F ZVftp start[ Bg 8 , Vns at $.F ZVcbr stop[ 1). Ku%u% onitoring :
Bg , -onitoring the ueue bet&een n7 and n8 every F.6 sec and storing the result in the file Zm.out[ set mon
2Vns monitor4ueue3 Vn7 Vn8 2open m.out &3 F.63
variable name
node numbers
output file name
&rite mode
time interval
2Vns lin# Vn7 Vn8 3 ueue4sample4timeout The output trace file contains the follo&ing 66 columns , • • • • • •
time input node output node ueue si0e in bytes (si0e%) ueue si0e in pac#ets (p#t%) no. of pac#ets arrived (parrivals%) 66
• • • • •
no. of pac#ets departured (pdepartures%) no. of pac#ets dropped (pdrops%) no. of bytes arrived (barrivals%) no. of bytes departured (bdepartures%) no. of bytes dropped (bdrops%)
1*. A''%ssing 9lu%s in Tr'% /il%s /or Ku%u% onitoring :
Vmon instvar parrivlas%
pdepartures%
pdrops%
set pac#etost 2e=pr Vparrivals% 4Vpdepartures% 4 Vpdrops%3 set time F.F: set no& 2Vns no&3 set t 2Vno& R Vtime3 set band&idth 2e=pr Vbdepartures%A6F7$AVt3 1+. Tr'ing Si,ultion r%sults : Trace file can be created for storing simulation results. Bg , set file6 2open out.tr &3
file pointer
file name
&rite mode
Vns trace4all file6 2-. 6r%ting Tr'% /il% /or A current time set no& 2Vns no&3
>current &indo& si0e from tcp ZtcpSource[ set c&nd 2VtcpSoucre set c&nd%3 >&rite current time M current &indo& si0e into file Zfile[ puts Vfile ZVno& Vc&nd[ >call the procedure at an interval of Ztime[ sec Vns at 2e=pr Vno& R Vtime3 Zplot'indo& VtcpSource Vfile[ L Starting the procedure Zplot'indo&[ at F.6 sec , Vns at F.6 Zplot'indo& Vtcp V'infile 67
21. i9ing od% $osition /or A :
Bg , n 0 n 2 Vns duple=4lin#4op VnF Vn7 orient right4do&n
22. Adding l%l to Lin( : Bg , +dding abel ZT*P in#[ for lin# from node nF to node 7 Vns duple=4lin#4op VnF Vn7 label ZT*P in#[ 23. Adding T%7t t th% otto, o/ A 5indo8 : Bg , Vns at : Vns trace4annotate K[Pac#et dropK[ Z 24. Finishing $ro'%dur% :
+ll trace file contents must be used . N+- trace file must be closed . N+- trace file can be e=ecuted in bac#ground . Bg , proc finish C L C global ns file6 file7 Vns flush4trace close Vfile7 e=ec nam out.nam M > /un in bac#ground e=it F L 2!. lotting gr$h 8ith 7gr$h : =graph uses one or more ascii files containing =4y data point per line. O$tions : Titl% : 4t ZTitle[ Si% : 4geometry =si0e = ysi0e Titl% /or ?7is : 4= Z=a=isTitle[ Titl% /or Y?7is : 4y Zya=isTitle[ Bg , %7%' 7gr$h out.tr out.n, ?g%o,%tr" *--74-- ;t Lost Rt%sP ?7 Ti,%P ?" Lost '(%tsP
run in bac#ground 2&. Running th% Si,ultion : Nns run rogr, Outlin%
%&reate a simulator ob'ect s%t ns n%8 Si,ultor 68
%(pen t)e trace files+ s%t n/ o$%n out.n, 8 Nns n,tr'%?ll Nn/ %efine a -finis)- procedure $ro' /inish B glol ns n/ Nns /lush?tr'% 'los% Nn/ 6los% th% tr'% /il% %7%' n, out.n, E7%'ut% n, on th% tr'% /il% =o$tionl> e=it F L l'% "our 'od% h%r% %&all t)e finis) procedure after . seconds simulation time Nns t !.- V/inishV %/un t)e simulation Nns run L E7%r'is% : 6. Simulate the follo&ing net&or# ,
sources n 1
6;-b! 7: ms
6; -b! 7: ms
n 2
:$-b! 67ms
6
n 3
n 5 ;-b! 7:ms
6FF-b! ms
n 6
n 7
destination
7:-b! :Fms n 4
n6 ^ nW 4 TP traffic n$ ^ nW 4 */ traffic Implement "ropTail M /B" scheme in the overloaded lin# and plot both &indo& si0e and pac#ets dropped. 7. Simulate the follo&ing net&or# ,
n 5
W: -b! 7:ms
N 1 n 2
69
W:-b! 7:ms n 4
7:-b! 7Fms n 3 6FF-b! 7Fms n6 ^ n: TP traffic n7 44 n$ */ Traffic Implement "ropTail M /B" scheme in the overloaded lin# and plot both &indo& si0e and pac#ets dropped.
70
11 ERFOR A 6ASE STUDY ACOUT THE DIFFERET ROUTI ALORITHS TO SELE6T THE ET5OR ATH 5ITH ITS OTIU AD E6OOI6AL DURI DATA TRASFER E7. o: 11. DATE:
AA in# State /outing rogr, 6oding: >include?stdio.h@ >include?conio.h@ struct pac#et C intadD26:3E int cost26:3E intcntE LP+*BT26:3E int routE intdist26:326:3E int viaE input() C char chG5y5E int=!y!lG;!mGY!coE inti!DE printf(J No of /outers,J)E scanf(JQdJ!Mrout)E for(iG6Ei?GroutEiRR) C for(DG6ED?GroutEDRR) C dist2i32D3GYYYYE L L goto=y(;!W)E printf(Jrom to cost ( yAn)J)E &hile(chGG5y5) C coGFE goto=y(l!m)E scanf(JQdJ!M=)E goto=y(lR;!m)E scanf(JQdJ!My)E goto=y(6R6:!m)E scanf(JQdJ!Mco)E dist2=32y3GcoE 71
dist2y32=3GcoE goto=y(6R7$!m)E fflush(stdin)E scanf(JQcJ!Mch)E mRRE L L pc#t() C inti!DE int countE for(iG6Ei?GroutEiRR) C countG6E for(DG6ED?RroutEDRR) C if(dist2i32D3GYYYY) C P+*BT2i3.adD2count3GDE P+*BT2i3.cost2count3Gdist2i32D3E countRRE L P+*BT2i3.cntGcount46E L L L build() C inti!D!#E intflgGFE getch()E clrscr()E printf(Juffer /outer Table or QdKnJ!via)E printf(J444444444444444444444444444444444KnJ)E printf(J S/* SBNTA+*KnJ)E printf(J44444444444444444KnJ)E for(iG6Ei?GroutEiRR) C printf(JQd@@J!i)E for(DG6ED?GP+*BT2via3.cntEDRR) C flgG6E for(#G6E#?GP+*BT2i3.cntE#RR) C if(viaGGi) goto outerE 72
if(P+*BT2via3.adD2D3GGi) flgGFE if(P+*BT2via3.adD2D3GGP+*BT2i3.adD2#3) flgGFE L if(D?GP+*BT2via3.cnt)E C if(flgGG6) printf(JQdSNT J!P+*BT2via3.adD2D3)E if(flgGGF) printf(JQd+* J!P+*BT2via3.adD2D3)E L L outer,E printf(JKnJ)E LL int main() C clrscr()E printf(JKtKtKt in# State /outingKnJ)E printf(JStage 6, /outer InputKnJ)E input()E printf(JStage 7, P+*BT *onstructionKnJ)E pc#t()E printf(JStage 8, I+ /outerKnJ)E printf(J/outer,J)E scanf(JQ"J!Mvia)E printf(JKn Stage $, uilding uffer TableKnJ)E build()E L
## Flooding rogr, 6oding: >include?stdio.h@ >include?conio.h@ >include?dos.h@ int g27F327F3!v!eE void main() C int i!D!v626F3!source!a!b!destE char msg2:F3E clrscr()E AANumber of /outers printf(JBnter the no.of.hostsJ)E 73
scanf(JQdJ!Mv)E AANumber of *onnections in Net&or# printf(JBnter the no.of.lin#s,J)E scanf(JQdJ!Me)E for(iGFEi?GvEiRR) for(DG6ED?GeEDRR) C g2i32D3GFE g2D32i3GFE L for(iG6Ei?GvEiRR) v62i3G6E printf(JBnter the lin# bet&een a and b,J)E for(iG6Ei?GeEiRR) C scanf(JQdQdJ!Ma!Mb)E g2a32b3G6E L AAirst sending router printf(JBnter the host &ho &ant to send the pac#age to other hosts,J)E scanf(JQdJ!Msource)E AA"estination /outer printf(JBnter the actual destination hosts,J)E scanf(JQdJ!Mdest)E AA-essage to be sent printf(JBnter the msg to be sent,J)E scanf(JQsJ!Mmsg)E AA*hec# if "estination /outer sends bac# message to the source if(g2dest32source3GGg2source32dest3) C g2dest32source3GFE printf(JKnPac#et cannot be transmitted from Qd to host QdJ!dest!source)E L AA-essage transmitted to all /outers for(iG6Ei?GvEiRR) for(DG6ED?GvEDRR) C if((g2i32D3GF)MM(sourceGD)MM(v62D3GF) C printf(JKnKnPlease &ait...Pac#ages are being transmitted...J)E delay(6FFF)E printf(JKnPac#ages are passed from hosts Qd to host QdJ!i!D)E printf(JKn message received by hostQd is,QsJ!D!msg)E if(g2i32D3Gg2D32i3) C printf(JKnKnPac#age cannot be transmitted from host Qd to host QdJ!D!i)E 74
L L Lgetch()E L ## Distn'% 9%'tor rogr, 6oding: >include?stdio.h@ >include?stdlib.h@ >include?string.h@
typedef struct table C int desE int hcE char nh283E LtableE typedef struct ripmsg C int desE int hcE LripmsgE int main() C table t26F3!t626F3E ripmsg r26F3E char ch283E int #!DGF!n!n6!iE int flagE int flag6E printf(JBnter No Of Bntries In Old /T,J)E scanf(JQdJ!Mn)E printf(JBnter "es!Hc!Nh,J)E for(iGFEi?nEiRR) C scanf(JQd Qd QsJ!Mt2i3.des!Mt2i3.hc!Mt2i3.nh)E L printf(JBnter /ip -sg rom 'here ,J)E scanf(JQsJ!Mch)E printf(JBnter No Of Bntries In /ip -sg,J)E scanf(JQdJ!Mn6)E printf(JBnter "es!Hc,J)E for(iGFEi?n6EiRR) C scanf(JQd QdJ!Mr2i3.des!Mr2i3.hc)E L 75
#GnE for(iGFEi?nEiRR) C t62i3Gt2i3E L for(iGFEi?n6EiRR) C flagGFE for(DGFED?nEDRR) C if(t62D3.desGGr2i3.des) C flag6GFE if(strcmp(t62D3.nh!ch)GGF) C t62D3.desGr2i3.desE t62D3.hcGr2i3.hcR6E flag6G6E L if(flag6GGF) if([email protected]) C t62D3.desGr2i3.desE t62D3.hcGr2i3.hcR6E strcpy(t62D3.nh!ch)E L flagG6E L L if(flagGGF) C t62#3.desGr2i3.desE t62#3.hcGr2i3.hcR6E strcpy(t62#3.nh!ch)E #RRE L L printf(JKnKnOld /outing TableJ)E printf(JKn"estination Hop*ount Ne=tHopJ)E for(iGFEi?nEiRR) C printf(JKnKtQd KtQd KtQsJ!t2i3.des!t2i3.hc!t2i3.nh)E L printf(JKnKn/ip -sg rom Qs J!ch)E printf(JKn"estination Hop*ountJ)E for(iGFEi?n6EiRR) C printf(JKnKtQd KtQdJ!r2i3.des!r2i3.hc)E L printf(JKnKnNe& /outing TableJ)E 76