You are on page 1of 37

SIMULATION OF ARP Program #include<stdio.h> #include<string.h> #include<stdlib.

h> int main() { int sys[5],ip[5],et[5],i,arp; for(i=0;i<5;i++) { et[i]=i*3; ip[i]=rand()%50; } arp=rand()%6; printf("\nSERVER->IP ADDRESS=192.168.11%d",ip[arp]); for(i=0;i<5;i++) { if(i!=arp) printf("\nSYSTEM%d->NO RESPONSE",i+10); else printf("\nSYSTEM%d->ETHERNET ADDRESS=%d(a.b%d 10.25%d",i+1,(i*2)%10,et[i]); } getch(); return 0; }

Output SERVER->IP ADDRESS=192.168.11.30 SYSTEM10->NO RESPONSE SYSTEM2->ETHERNET ADDRESS=2(a.b3 10.25956 SYSTEM12->NO RESPONSE SYSTEM13->NO RESPONSE SYSTEM14->NO RESPONSE

DEVELOP A CLIENT SERVER APPLICATION FOR CHAT

PROGRAM:

CLIENT: #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.h> #include<arpa/inet.h> #include<errno.h> int main() { int sd,nsd,i,port=8787; char c[30]="\0",buf[1024]="\0",buf1[1024]="\0"; FILE *fp; struct sockaddr_in ser; if((sd=socket(AF_INET,SOCK_STREAM,0))<0) {

printf("\n error:socket creation"); return 0; } bzero((char*)&ser,sizeof(ser)); printf("\n port address is %d",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htonl(INADDR_ANY); if(connect(sd,(struct sockaddr*)&ser,sizeof(ser))==-1) { printf("\n error:binding"); return 0; } printf("client module\n"); while(1) { printf("client"); fgets(buf1,sizeof(buf1),stdin); if(buf1[0]=='q'); { printf(" exit(0)"); } if((i=send(sd,buf1,sizeof(buf1),0))==-1) { perror("send"); break; } strcpy(buf1,"\0"); if((i=recv(sd,buf,sizeof(buf),0))==-1) { perror("recv");

buf). return 0.c [it28@localhost ~]$ cc ccli./a.out port address is 8787client module clienthai exit(0)serverhai clienti am student exit(0)serveri am proff client PROGRAM: . } printf("server%s\n".break. } close(sd). } OUTPUT: [it28@localhost ~]$ vi ccli.c [it28@localhost ~]$ .

h> #include<stdio.sin_family=AF_INET. .sin_port=htons(port). return 0. } i=sizeof(cli).sizeof(ser))<0) { printf("\n error:binding").s_addr=htonl(INADDR_ANY).SOCK_STREAM.nsd.0))<0) { printf("\n Error:socket creation"). ser.h> #include<netinet/in.(struct sockaddr*)&ser.port=8787.sin_addr.n. char c[30]="\0".cpid. ser.1).h> #include<string. } bzero((char*)&ser.h> int main() { int sd.h> #include<sys/socket. ser. return 0. struct sockaddr_in ser. FILE *fp.i. if(bind(sd. if((sd=socket(AF_INET.buf[1024]="\0".sizeof(ser)).h> #include<sys/types.buf1[1024]="\0". listen(sd.SERVER: #include<unistd. struct sockaddr_in cli.

if((nsd=accept(sd.0))==-1) { perror("recv").stdin).printf("\n server module \n").(sd.&i))==-1) { perror("accept"). } while(1) { if((i=recv(nsd.sizeof(buf).(struct sockaddr*)&cli).0))==-1) { perror("send"). } if(nsd==-1) { printf("\n error:client accept the problem"). } close(sd). . return 0. } printf("Client:%s\n". printf("server").sizeof(buf).buf1). if((i=send(nsd. break. break.buf1. printf(" exit(0)").sizeof(buf1). } if(buf[0]=='q') printf(" exit(0)"). fgets(buf.buf.

h> #include<netinet/in.h> #include<sys/times.h> #include<string.h> #include<sys/un. } OUTPUT: [it28@localhost ~]$ vi cser.h> #include<unistd.close(nsd).h> .h> #include<sys/time.out server module hai Client:hai serverClient:i am student serveri am proff Client:i am student Server DEVELOP A TRACE ROUTE PROGRAM Program: #include<stdio.h> #include<errno./a.h> #include<sys/iostl.c [it28@localhost ~]$ cc cser. return 0.c [it28@localhost ~]$ .h> #include<stdlib.

//Flags unsigned char ttl.UDP etc) //IP checksum //Source IP //Destination IP unsigned short i_cksum. //ICMP message type //Sub code //Time to live //Protocol(TCP. unsigned char tos. unsigned char proto. typedef struct_ihdr { char i_type. unsigned int destIP. //Unique id . unsigned short checksum. unsigned short total_len. unsigned short ident. unsigned int sourceIP. } IpHeader.h> #define ICMP_ECHOREPLY 0 #define ICMP_DESTUNREACH #define ICMP_SRCQUENCH #define ICMP_REDIRECT #define ICMP_ECHO #define ICMP_TIMEOUT #define ICMP_PARMERR #define MAX_HOPS #define ICMP_MIN typedef struct iphdr { unsigned int h_len:4. char i_code. unsigned short i_id.#include<netdb. unsigned int version:4. //Length of the header //Version of IP //Type of service //Total length of the packet //Unique identifier 3 4 5 8 11 12 30 8 unsigned short frag_and_flags.

struct hostent* lpHostlent = NULL.(int*)&nTimeToLive.IP_TTL. return 0. } int decode_resp(char* buf.int ttl) { IpHeader* iphdr = NULL.sizeof(int)). nRet=setsockopt(s. } int set_ttl(int s. if(nRet<0) { perror(“setsockopt in set_ttl:”).int bytes. exit(-1). //Sequence number unsigned long timestamp. } IcmpHeader. if(bytes<iphdrlen + ICMP_MIN) . iphdr = (IpHeader*)buf.int nTimeToLive) { int nRet.progname).IPPROTO_IP. } return 1. unsigned short iphdrlen. #define DEF_PACKET_SIZE #define MAX_PACKET void usage(char* progname) { printf(“usage:%s host-name [max-hops]\n”. iphdrlen = iphdr->h_len* 4.struct sockaddr_in* from.unsigned short i_seq. IcmpHeader* icmphdr = NULL. struct in_addr inaddr = from->sin_addr.

4. return 1.icmphdr->i_type). icmphdr = (IcmpHeader*)(buf + iphdrlen).inet_ntoa(inaddr)). } return 0. return 0.AF_INET). switch(icmphdr->i_type) { case ICMP_ECHOREPLY: //Response from destination lpHostent = gethostbyaddr((const char*)&from->sin_addr. return 1. break.int size) { . break. case ICMP_TIMEOUT: //Response from router along the way printf(“%2d %s\n”. break. if(lpHostent!=NULL) printf(“gethostbyaddr success\n”). return 1.inet_ntoa(from->sin_addr)).ttl.ttl. break. default: printf(“non-echo type %drecvd\n”. } unsigned short checksum(unsigned short* buffer.printf(“Too few bytes from %s\n”.inet_ntoa(inaddr)). case ICMP_DESTUNREACH: //Can’t reach the destination at all printf(“%2d %s reports:Host is unreachable\n”.

} long GetTickCount() { struct tms tm. icmp_hdr = (IcmpHeader*)icmp_data. while(size > 1) { cksum+ =*buffer++. icmp_hdr->i_id = (unsigned short)getpid().= sizeof(unsigned short). size. return(unsigned short)(~cksum). icmp_hdr->i_seq = 0. cksum+ = (cksum >> 16). } int main(int argc. } if(size) cksum+ = *(unsigned char*)buffer.char** argv) . memset(datapart.int datasize) { IcmpHeader* icmp_hdr. } void fill_icmp_data(char* icmp_data. char* datapart. icmp_hdr->i_code = 0. datapart = icmp_data + sizeof(IcmpHeader).datasize – sizeof(IcmpHeader)). return times(&tm). icmp_hdr->i_type = ICMP_ECHO.’E’. cksum = (cksum >> 16)+(cksum & 0xffff).unsigned long cksum = 0. icmp_hdr->i_cksum = 0.

*recvbuf.&timeout. struct sockaddr_in dest.0.ttl = 1.done = 0. char* icmp_data.IPPROTO_ICMP). if(ret == -1) { perror(“setsockopt in send:”). struct timeval timeout.datasize.fromlen = sizeof(from). int bOpt. if(argc ==3) maxhops = MAX_HOPS. return -1. exit(-1). if(argc < 2) usage(argv[0]). } timeout. from.{ int sockRaw.maxhops.sizeof(struct sockaddr_in)).SOCK_RAW.tv_sec = 1. int ret. . if(sockRaw <0) { perror(“socket”).tv_usec = 0. sockRaw = socket(AF_INET.SOL_SOCKET. } Memset(&dest.sin_ family = AF_INET. unsigned short seq_no = 0.sizeof(struct timeval)).SO_RCVTIMEO. dest. ret = setsockopt(sockRaw. struct hostent* hp = NULL. timeout.

0. .ttl++) { int bwrote.datasize). if(hp) memcpy(&dest.argv[1]).ttl). return -1. if((!icmp_data)||(!recvbnf)) { perror(“malloc:”). set_ttl(sockRaw.datasize).sin_addr. datasize+=sizeof(IcmpHeader). for(tt1=1.argv[1].maxhops).s_addr = inet_addr(argv[1])) ==INADDR_NONE) { hp=gethostbyname(argv[1]).hp->h_length). ((IcmpHeader*)icmp_data)->timestamp=GetTickCount(). exit(-1).hp->h_addr. icmp_data=malloc(MAX_PACKET*sizeof(char)). else { printf(“Unable to resolve%s\n”. recvbuf=malloc(MAX_PACKET*sizeof(char)). fill_icmp_data(icmp_data. printf(“\n Tracing route to %s over a maximum of %d hops:\n\n”. ((IcmpHeader*)icmp_data)->i_seq=seq_no++.if((dest. } memset(icmp_data. ((IcmpHeader*)icmp_data)->i_cksum=0.MAX_PACKET).((ttl<maxhops)&&(!done)).sin_addr. ((IcmpHeader*)icmp_data)->i_cksum=checksum((unsigned short*)icmp_data. } } datasize=DEF_PACKET_SIZE.

Bwrote=sendto(sockRaw. } ret=recvfrom(sockRaw.ttl). } done=decode_resp(recvbuf.datasize. return -1./a.org over a maximum of 30 hops: .(struct sockaddr*)&dest. } free(recvbuf).0. if(ret<0) { perror(“recvfrom:”).c [root@localhost~]# .dsgroups.c [root@localhost~]#cc tracert. free(icmp_data).ret.(struct sockaddr*)&from.recvbuf. return 0.out www. If(bwrote<0) { perror(“sendto:”).sizeof(dest)).&from. sleep(1).&fromlen).0. return -1.icmp_data. } Output: [This program runs under superuser only] [root@localhost~]# vi tracert.MAX_PACKET.

struct in_addr h_addr.h> #include<netdb.179 3.11.179 DNS SERVER TO RESOLVE A GIVEN HOST NAME Program #include<stdio. } .249.h> #include<netinet/in."USAGE:nslookup\n").168. 210. struct hostent *host. 192.212.249. int h_type.h> int main(int argc.1.h> #include<arpa/inet. 210. if(argc!=2) { fprintf(stderr.1 2.char**argv) { char h_name.212.

printf("\n IP ADDRESS=%s\n".inet_ntoa(h_addr)).0. } h_addr.host->h_length). printf("\nADDRESS TYPE=%d\n".host->h_name).host->h_addrtype).h> .inet_ntoa(h_addr_list[0])).0.h> #include<netinet/in.0."(mini)nslookup failed on %s\n". printf("\n HOST NAME=%s\n".c [it28@localhost ~]$ cc nandns.36 HOST NAME=90.if((host=gethostbyname(argv[1]))==NULL) { fprintf(stderr.h> #include<netdb.h> #include<stdlib.0.0. } Output [it28@localhost ~]$ vi nandns.0.0.s_addr=*((unsigned long*)host->h_addr_list[0]).argv[1]).36 Download a File From HTTP Server Program #include<stdio. printf("\nADDRESS LENGTH =%d\n".0.h> #include<arpa/inet.36 ADDRESS LENGTH =4 ADDRESS TYPE=2 LIST OF ADDRESS=90./a.out 90.36 IP ADDRESS=90. printf("\nLIST OF ADDRESS=%s\n".c [it28@localhost ~]$ .

req="/". strcpy(hostname.sd. *hostname. FILE *local. l=strlen(cp).cp). } if(cp=strchr(argv[1]. strcpy(req.i.l. l=strlen(argv[1]). *cp. hostname=malloc(l+1).argv[0]). exit(1). *cp='/'.h> #include<sys/codket.char *argv[]) { struct sockaddr_in sock. struct hostent *hp. . char buff[size]. req=malloc(l+1).argv[1]).h> #define size 100 int main(int argc.#include<sys/types.port.nrv. } else { hostname=argv[1]. if(argc!=3) { printf("\n Usage: %s <server> filename". char *req.'/')) { *cp='\0'. int con.

"w"). local=fopen(argv[2].hostname). printf("Buff=%s \n".argv[1]. if(sd<0) { perror("\n Cannot Open Socket"). exit(1). } bzero(&sock.sin_port=htons(80).(struct sockaddr*)&sock.1). } sprintf(buff.1\r\n Host: %s\r\n Connection : Class \r\n\r".&sock). sock. sock.} printf("\n Host = %s \n Req =%s". sd=socket(AF_INET.hostname.i<nrv.req. exit(1). .sizeof(sock)).sizeof(sock)).local). do { nrv=read(sd. con=connect(sd.buff). if(con<0) { perror("\n connection failed ").buff. write(sd.0). if(nrv>0) { for(i=0.sin_family=AF_INET.SOCK_STREAM.buff. con=inet_pton(AF_INET.i++) putc(buff[i]. l=strlen(buff).req).size)."Get HTTP: %s //1.

c [it28@localhost ~]$ . fclose(local).txt . } Output [it28@localhost ~]$ vi download.txt hello.txt host=http: connection failed: Connection refused req=/192.c [it28@localhost ~]$ cc download.208/z:hi. return 0./a.11.168.out http:/192. } while(1). close(sd).11.} else break.208/z:hi.168.

h> int main() { int i=0.HANDLING ZOMBIE Program #include<stdio.pid. if(pid==0) . printf("Ready to fork... pid=fork().\n").

out Ready to fork.../a.. for(i=0. child starts..\n").. } printf("child ends. } } Output [it28@localhost ~]$ vi handling.. I am a child [it28@localhost ~]$ I am a child I am a child I am a child I am a child child ends... sleep(1).\n").i<5. SIMULATION OF RARP .{ printf("child starts.c [it28@localhost ~]$ ..i++) { printf("I am a child \n")..c [it28@localhost ~]$ cc handling.

arp.ip[5]. clrscr().158.h> #include<string.i++) { et[i]=i*3.%d". printf("rarp\n"). else printf("\nENTER IP ADDRESS=192. printf("\nSYSTEM%d->ETHETRMNET ADDRESS=%dCA. } getch().25%d".h> #include<stdlib.i++) { if(i!=arp) printf("\nSYSTEM%d->NO RESPONSE". for(i=0.B%d10.Program: #include<stdio. } Output: . ip[i]=rand()%50.i<5. for(i=0. } arp=rand()%6.et[5].i+1).11.h> int main() { int sys[5].ip[1]).i.i<5. return 0.i+1).

11.B92 10.0). signal(SIGINT.SYSTEM6->ETHETRMNET ADDRESS=950CA.i++) { printf("we are in third year\n").i<5.h> #include<signal.h> int main() { int i. } } . for(i=0.158. sleep(1).30 SYSTEM3->NO RESPONSE SYSTEM4->NO RESPONSE SYSTEM5->NO RESPONSE SIGNAL HANDLING Program #include<stdio.25%d SYSTEM1->NO RESPONSE ENTER IP ADDRESS=192.

c [it28@localhost ~]$ .j.c [it28@localhost ~]$ cc signal.k./a.i++) .i<=4. for(i=1. printf("\n Enter the distance between Hosts:").inter[10][10].h> main() { int d[10][10]. int i.Output [it28@localhost ~]$ vi signal.out we are in third year we are in third year we are in third year we are in third year we are in third year SIMULATION OF OSPF ROUTING PROTOCOL Program #include<stdio.m.

j<=4. for(i=1.j++) { printf("%d\t". } } printf("\n Enter the distance Vector \n").k++) { if(d[i][j]>d[i][k]+(d[k][j])) ". }} for(i=1.{ for(j=0. } } for(i=1.j++) { printf("\n Enter the distance between %d and %d scanf("%d".i++) { for(j=1. for(j=1.j++) { inter [i][j]=0.j<=4.j). for(k=1.i<=4. printf("\n\t H1 \t H2 \t H3 \t H4 \n").k<=4.i).d[i][j]). .i++) { for(j=1.i++) { printf("\n Host %d: \t".&d[i][j]).i<=4.j++) { m=0.i<=4.j<=4.j<=4.i.

i=1.j<=4. } m=m+1.d[i][j]). } printf("\n\n"). printf("\n H1 \t H2 \t H3 \t H4 \t"). for(j=1.{ d[i][j]=d[i][k]+d[k][j]. } . } } } printf("\n Minimum distance host1 another \n"). inter[j][m]=k.j++) { printf("%d\t".

out Enter the distance between Hosts: Enter the distance between 1 and 1 Enter the distance between 1 and 2 Enter the distance between 1 and 3 Enter the distance between 1 and 4 Enter the distance between 2 and 1 Enter the distance between 2 and 2 Enter the distance between 2 and 3 Enter the distance between 2 and 4 Enter the distance between 3 and 1 Enter the distance between 3 and 2 Enter the distance between 3 and 3 Enter the distance between 3 and 4 Enter the distance between 4 and 1 Enter the distance between 4 and 2 Enter the distance between 4 and 3 Enter the distance between 4 and 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 .c [it28@localhost ~]$ cc ospf.c [it28@localhost ~]$ ./a.Output: [it28@localhost ~]$ vi ospf.

The procedures and functions described in this chapter can be found in ~ns/tcl/lib/ns-lib. It provides a set of interfaces for configuring a simulation and for choosing the type of event scheduler used to drive the simulation. A simulation script generally begins by creating an instance of this class and calling various methods to create nodes.tcl. Simulator Initialization When a new simulation object is created in tcl. ~ns/scheduler. topologies. the initialization procedure performs the following operations: _initialize the packet format (calls create_packetformat) _create a scheduler (default to a calendar scheduler) _create a “null agent” (a discard sink used in various places) The packet format initialization sets up field offsets within packets used by the entire simulation. and configure other aspects of the simulation.h}. A subclass of Simulator called OldSim is used to support ns v1 backward compatibility. and. The null agent is created with the following call: .h.Enter the distance Vector H1 Host 1: Host 2: Host 3: Host 4: 1 1 1 1 H2 2 2 2 2 H3 3 3 3 3 H4 4 4 4 4 Minimum distance host1 another H1 1 H2 2 H3 3 H4 4 STUDY OF NETWORK SIMULATORS – NS2 The Class Simulator The overall simulator is described by a Tcl class Simulator. ~ns/heap. {cc. The scheduler runs the simulation in an event – driven manner and may be replaced by alternative schedulers which provide somewhat different semantics.

Presently. and returning to execute the next event. the simulator is single. each of which is implemented using a different data structure: a simple linked list. Schedulers and Events The simulator is an event – driven simulator. and a special type called “real – time”. The scheduler runs by selecting the next earliest event. heap.h: class Event { public: Event* next_. Each of these are described below. /* event list */ Handler* handler_. Simultaneous events are not recorded anymore by schedulers (as it was in earlier versions) and all schedulers should yield the same order of dispatching given the same input. Unit of time used by scheduler is seconds. /* unique ID */ Event() : time_(0). If more than one event are scheduled to execute at the same time. it must be freed by the handler. Two types of objects are derived from the base class Event: packets and “at-events”. executing it to completion. calendar queue (default).e. /* handler to call when event ready */ double time_. */ class Handler { public: virtual void handle(Event* event).threaded. *i. When an event’s scheduled * time arrives. There are presently four schedulers available in the simulator. uid_(0) {} }. and only one event in execution at any given time. if needs to be freed it. The actual definition of an event is found in ~ns/scheduler. it is passed to handle which must consume it. /* * The base class for all event handlers. No partial execution of events or pre – emption is supported. An at-event is . }..Set nullAgent_[new Agent/Null] This agent is generally useful as a sink for dropped packets or as a destination for packets that are not counted or recorded. /* time at which event is ready */ int uid_. An event generally comprises a “firing time” and a handler function. their execution is performed on the first scheduled – first dispatched manner.

410) [15].5 (seconds)(Note that this particular code fragment expects to be encapsulated in an object instance procedure. in which events on the same month/day of multiple years can be recorded in one day. A simple example of how it is used is as follows: … Set ns_[new Simulator] $ns_use-scheduler Heap $ns_at 300. As per the . and informally described in Jain (p. The clock variable for ns is represented by a double. where the appropriate reference to $self is correctly defined). then changes the default scheduler implementation to be heap-based (see below). This structure is superior to the list structure for a large number of events. The list is kept in time – order (earliest to latest). Choosing the next event for execution requires trimming the first entry off the head of the list. This is frequently used in simulation scripts. The Calendar Queue Scheduler The calendar queue scheduler (class Scheduler/Calendar) uses a data structure analogous to a one-year desk calendar. The real – time capability in ns is still under development. as insertion and deletion times are in O(log n) for n events. This implementation in ns v2 is borrowed from the MaRS – 2. and this synchronization is not presently enforced. but is used to introduce an ns simulated network into a real – world topology to experiment with easily – configured network topologies. It is currently implemented as a subclass of the list scheduler. so event insertion and deletion require scanning the list to find the appropriate entry. The Real – Time Scheduler The real – time scheduler (class Scheduler/RealTime) attempts to synchronize the execution of events with real – time. Precision of the scheduler clock used in ns Precision of the scheduler clock can be defined as the smallest time – scale of the simulator that can be correctly represented. The implementation of Calendar queues in ns v2 was contributed by David Wetherall(presently at MIT/LCS). The Head Scheduler The heap scheduler (class Scheduler/Heap) implements the scheduler using a heap structure. as the simulator must be able to keep pace with the real – world packet arrival rate. it is believed that MaRs itself borrowed the code from NetSim[14]. At – events are implemented as events where the handler is effectively an execution of the tcl interpreter. and finally schedules the function $self complete_sim to be executed at time 300. etc. cross – traffic.5 “$self complete_sim” … This tcl code fragment first creates a simulation object. although this lineage has not been completely verified.a tcl procedure execution scheduled to occur at a particular time. This only works for relatively slow network traffic data rates.0 simulator [1]. This implementation preserves event execution in a FIFO manner for simultaneous events. The List Scheduler The list scheduler (class Scheduler/List) implements the scheduler using a simple linked – list structure. It is formally described in [6].

an extension of the vanilla otclsh command shell. Commands: Synopsis: ns<otclfile> <arg> <arg>. $ns_ halt This stops or paused the scheduler. Thus the precision of timeclock in ns can be defined as (1=2(52)). $ns_ at 10.``. $ns_ run This starts the scheduler. The following is a list of simulator commands commonly used in simulation scripts: set ns_ [new Simulator] This command creates an instance of the simulator object.IEEE std for floating numbers. Several examples of OTcl scripts can be found under ns/tcl/ex directory. $ns_halt” or. In effect. sign 1 bit exponent 11 bits mantissa 52 bits Any floating number can be represented in the form (X_2n) where X is the mantissa and n is the exponent. setns_ [$ns_now] The scheduler keeps track of time in a simulation.. . $ns_at <time> <event> This schedulers an <event> (which is normally a piece of code) to be executed at the specified <time>. Anything greater than that might not be vary accurate as you have remaining 12 bits to represent the time change. consisting of 64 bits must allocate the following bits between its sign. exponent and mantissa field. a double. Given 52 bits we can safely say time upto around (2(40)) can be represented with considerable accuracy. e. A simulation is defined by OTcl script (file). The simulator (ns) is invoked via the ns interpreter. This returns scheduler’s notion of current time.. event is removed from scheduler’s list of ready to run events. Description: Basic command to run a simulation script in ns. As simulation runs for longer times the number of remaining bits to represent the time educes thus reducing the accuracy.g $ns_ at $opt(stop) “puts ``NS EXITING.0 “$ftp start” $ns_ cancel <event> Cancels the event. However (2(40)) is a very large number and we donot anticipate any problem regarding precision of time in ns.

links etc created for a given simulation. $ns_ is-started This returns true if simulator has started to run and false if not. this creates nam tracefiles. $ns_ flush-trace Flushes all trace object write buffers. $ns_ after <delay> <event> Scheduling an <event> to be executed after the lapse of time <delay>. Currently Calendar is used as default. node components. Note that this function may not work because of tcl’s string number resolution. $ns_ dumpq Command for dumping events queued in scheduler while scheduler is halted. $ns_ gen-map This dumps information like nodes. otherwise if op is not defined. Define a very simple topology with two nodes that are connected by a link. Calendar. Node Creation using NS2 Aim: To write a NS2 script to procedure 2 nodes and 1 link. $ns_ create_packerformat This sets up simulator’s packet format. one link 1. Procedure: Two nodes.$ns_ create-trace <type> <file> <src> <dst> <optional arg: op> This creates a trace-object of type <type> between <src> and <dst> object and attaches traceobject to <file> for writing trace-outputs. The different types of scheduler available are List. This may be broken for some scenarios (like wireless). $ns_ clearMemTrace Used for memory debugging purposes. $ns_ at-now <args> This is an effect like command “$ns_ at $now $args”. ns tracefiles are created on default. If op is defined as “nam”. Heap and RealTime. . These are additional simulator (internal) helper functions (normally used for developing/changing the ns core code): $ns_ use-scheduler <type> Used to specify the type of scheduler to be used for simulation.

.tcl’. a delay of 10ms and a DropTail queue.0 “finish”’). (Note: You have to insert the code in this section before the line ‘$ns run’. After creating two nodes. Define the two nodes. Connects the two nodes. This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the bandwidth 1Megabit. before the line ‘$ns at 5. 4. nam will be started automatically. 5.2. set n0 [$ns node] set n1 [$ns node] 3. Result: Thus the node has been created using NS2. or even better. IMPLEMENTATION OF RPC Program /*program: remote. 7. Save the file and start the script with ns example1. A new node object is created with the command ‘$ns node’. }=1. assigns them to the handles ‘n0’ and ‘n1’. $ns duplex-link $n0 $n1 1Mb 10ms DropTail 6.x*/ program REMOTE_PROG{ version REMOTE_VERS{ string REMOTE_USER(void)=1.

/*Program: remote server.h" char** remote_users_1_svc(void*argp.tmp.h> #include<string. strcat(buffer. while(!feof(fp)){ fread(&tmp. } } fclose(fp).h> #include<utmp.8s".h> #include"remote.h" void remote_prog_1(char *host) . char tmp_buff[10]. FILE *fp.1.c */ #include<stdio. struct utmp tmp.c*/ #include<stdio. fp=fopen("/var/run/utmp". char buffer[1024].h> #include"remote. } /*Program Name: remote users.tmp_buff). return &result. if(strlen(tmp. buffer[0]='\0'.fp).}=0x32345678. result=buffer.sizeof(struct utmp)."%.struct svc_req*rqstp) { static char*result."rb").ut_name).ut_name)){ sprintf(tmp_buff.

argv[0]). } printf("Users logging on server %s\n%s\n".REMOTE_PROG. exit(1). exit(1). .host. if(clnt==NULL) { clnt_pcreateerror(host)."udp"). #ifndef DEBUG clnt=clnt_create(host. if(result_1==(char**)NULL){ clnt_perror(clnt. if(argc <2){ printf("usage: %s Server_host \n". remote_prog_1(host). char *remote_users_1_arg. #ifndef DEBUG clnt_destroy(clnt). } host=argv[1].char *argv[]) { char *host.{ CLIENT *clnt. char**result_1. #endif /*DEBUG*/ } int main(int argc.REMOTE_VERS.*result_1)."call failed"). } #endif /*DEBUG*/ result_1=remote_users_1_((void*)&remote_users_1_arg.clnt).

h> #include<string. if(strlen(tmp. } } fclose(fp). char buffer[1024].sizeof(struct utmp).c [it28@localhost ~]$ rpcgen remote. struct utmp tmp.h> #include<utmp. char tmp_buff[10].buffer).rb).8s". fp=fopen("/var/run/utmp". strcat(buffer.h> int main(int argc. printf("%s\n"."%.exit(0).x .x [it28@localhost ~]$ vi nanrpc. while(!feof(fp)){ fread(*tmp.c #include<stdio.*/ FILE *fp. } Output [it28@localhost ~]$ vi remote.fp). } //login.1.char*argv[]) { /*static char*result.ut_name).tmp_buff). buffer[0]='\0'.tmp.x [it28@localhost ~]$ vi nanrpc.ut_name)){ sprintf(tmp_buff.

c [it28@localhost ~]$ gcc -o remote_server remote_server.c [it28@localhost ~]$ .c [it28@localhost ~]$ rpcgen -Sc remote.x [it28@localhost ~]$ rpcgen -Ss remote.c remote_svc./a.[it28@localhost ~]$ c [it28@localhost ~]$ rpcgen remote.x >remote_server.x >remote_user.c -Insl gcc: _user: No such file or directory [it28@localhost ~]$ cc login.c remote_clnt.c -Insl [it28@localhost ~]$ gcc -o remote _user remote_user.out pcd rebootrunlevelLOGINLOGINLOGINLOGINLOGINLOGINit28it12it15it28it17cplab32cplab31it15it20it1it5 .