Вы находитесь на странице: 1из 66

TCP ECHO PROGRAM

TCPECHOSERVER.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main (int argc, char **argv) { int sockfd,connfd,len,n; struct sockaddr_in servaddr,cli; char buff[80]; if(argc != 22) { printf("\nplease specify the port number as a command line argument\n"); } sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("socket successfully created..\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(atoi(argv[1])); if((bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))!=0) { printf("socket bind failed...\n"); exit(0); } else printf("socket successfully binded...\n"); if((listen(sockfd,5))!=0) { printf("listen failed...\n"); exit(0); } else printf("server listening..\n"); while(1) { len=sizeof(cli); connfd=accept(sockfd,(struct sockaddr *)&cli,&len); if(connfd<0) {

printf("server accept the failed...\n"); exit(0); } else printf("server accept the client \n"); for(;;) { bzero(buff,80); read(connfd,buff,sizeof(buff)); printf("\nfrom client:%s\t\n to client :%s\n ",buff,buff); write(connfd,buff,sizeof(buff)); if(strncmp("exit",buff,4)==0) { printf("server exit...\n"); break; } } } close(sockfd); } TCPECHOCLIENT.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main(int argc,int **argv) { int sockfd,connfd,n; struct sockaddr_in servaddr,cli; char buff[80]; if(argc!=3) { printf("\nUSAGE: $client <ipaddress> \n"); exit(0); } sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("socket successfully created..\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr(argv[1]); servaddr.sin_port=htons(atoi(argv[2])); #include<stdlib.h> #include<stdio.h> #include<netinet/in.h>

#include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main(int argc,int **argv) { int sockfd,connfd,n; struct sockaddr_in servaddr,cli; char buff[80]; if(argc!=3) { printf("\nUSAGE: $client <ipaddress> \n"); exit(0); } sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("socket successfully created..\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr(argv[1]); servaddr.sin_port=htons(atoi(argv[2])); if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))!=0) { printf("connection with the server failed....\n"); exit(0); } else printf("connected to the server...\n"); for(;;) { bzero(buff,sizeof(buff)); printf("enter the string:"); n=0; while((buff[n++]=getchar())!='\n'); write(sockfd,buff,sizeof(buff)); bzero(buff,sizeof(buff)); read(sockfd,buff,sizeof(buff)); printf("\nfrom server :%s",buff); if((strncmp("exit",buff,4))==0) { printf("client exit..\n"); break; } } close(sockfd); }

OUTPUT Server [chithambaramani@localhost Net]$ cc TCPECHOSERVER.c -o TCPECHOSERVEROUTPUT [chithambaramani@localhost Net]$ ./TCPECHOSERVEROUTPUT 5041 socket successfully created.. socket successfully binded... server listening.. server accept the client from client:hai how are you to client :hai how are you from client:ya fine to client :ya fine server exit... Client [chithambaramani@localhost ~]$ cc TCPECHOCLIENT.c -o TCPECHOCLIENTOUTPUT[chithambaramani@localhost ~]$ ./TCPECHOCLIENTOUTPUT 127.0.0.1 5041 socket successfully created.. connected to the server... enter the string:hai how are you from server :hai how are you enter the string:ya fine from server :ya fine enter the string:exit client exit..

TCP CHAT PROGRAM


TCPCHATSERVER.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main (int argc, char **argv) { int sockfd,connfd,len,n; struct sockaddr_in servaddr,cli; char buff[80]; if(argc != 22) { printf("\nplease specify the port number as a command line argument\n"); } sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("socket successfully created..\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(atoi(argv[1])); if((bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))!=0) { printf("socket bind failed...\n"); exit(0); } else printf("socket successfully binded...\n"); if((listen(sockfd,5))!=0) { printf("listen failed...\n"); exit(0); } else printf("server listening..\n"); while(1) { len=sizeof(cli); connfd=accept(sockfd,(struct sockaddr *)&cli,&len);

if(connfd<0) { printf("server accept the failed...\n"); exit(0); } else printf("server accept the client \n"); for(;;) { bzero(buff,80); read(connfd,buff,sizeof(buff)); printf("\nfrom client:%s\t\n to client : ",buff); bzero(buff,80); n=0; while((buff[n++]=getchar())!='\n'); write(connfd,buff,sizeof(buff)); if(strncmp("exit",buff,4)==0) { printf("server exit...\n"); break; } } } close(sockfd); }

TCPCHATCLIENT.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main(int argc,int **argv) { int sockfd,connfd,n; struct sockaddr_in servaddr,cli; char buff[80]; if(argc!=3) { printf("\nUSAGE: $client <ipaddress> \n"); exit(0); } sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("socket successfully created..\n");

bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr(argv[1]); servaddr.sin_port=htons(atoi(argv[2])); if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))!=0) { printf("connection with the server failed....\n"); exit(0); } else printf("connected to the server...\n"); for(;;) { bzero(buff,sizeof(buff)); printf("enter the string:"); n=0; while((buff[n++]=getchar())!='\n'); write(sockfd,buff,sizeof(buff)); bzero(buff,sizeof(buff)); read(sockfd,buff,sizeof(buff)); printf("\nfrom server :%s",buff); if((strncmp("exit",buff,4))==0) { printf("client exit..\n"); break; } } close(sockfd); }

OUTPUT Server [chithambaramani@localhost Net]$ cc TCPCHATSERVER.c -o TCPCHATSERVEROUTPUT [chithambaramani@localhost Net]$ ./TCPCHATSERVEROUTPUT 5666 socket successfully created.. socket successfully binded... server listening.. server accept the client from client:hai how are you to client : fine .. how about you from client:ya fine to client : how is your college life from client:ya fine to client : exit server exit... Client [chithambaramani@localhost ~]$ cc TCPCHATCLIENT.c -o TCPCHATCLIENTOUTPUT [chithambaramani@localhost ~]$ ./TCPCHATCLIENTOUTPUT 127.0.0.1 5666 socket successfully created.. connected to the server... enter the string:hai how are you from server :fine .. how about you enter the string:ya fine from server :how is your college life enter the string:ya fine from server :exit client exit..

UDP ECHO PROGRAM


UDPECHOSERVER.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main(int argc,char **argv) { int sockfd,len,n; struct sockaddr_in servaddr,cli; char buff[80]; if(argc!=2) { printf("\nplease specify the port number in command line argument\n"); exit(0); } sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("\nSocket creation failed"); exit(0); } else printf("\nSocket successfully created"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(atoi(argv[1])); if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))!=0) { printf("\nSocket binding failed"); exit(0); } else printf("\nSocket successfully binded"); len=sizeof(cli); for(;;) { bzero(buff,80); recvfrom(sockfd,buff,80,0,(struct sockaddr*)&cli,&len); printf("\nFrom Client: %s\n To Client %s \n",buff,buff); sendto(sockfd,buff,80,0,(struct sockaddr*)&cli,len); if(strncmp("exit",buff,4)==0) { printf("Server exit"); break; } }

close(sockfd); } UDPECHOCLIENT.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main(int argc,char **argv) { int sockfd,connfd; char buff[80]; int n,len; struct sockaddr_in servaddr,cli; if(argc!=3) { printf("\nplease specify the IP Address and Port Number in command line argument\n"); exit(0); } sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("\nSocket creation failed"); exit(0); } else printf("\nSocket successfully created"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(atoi(argv[2])); if (inet_aton(argv[1], &servaddr.sin_addr)==0) { printf("inet_aton() failed\n"); exit(1); } len=sizeof(servaddr); for(;;) { bzero(buff,80); printf("\nEnter the string "); n=0; while((buff[n++]=getchar())!='\n'); sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,len); bzero(buff,80); recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,&len); printf("\nFrom server:%s\n",buff); if(strncmp("exit",buff,4)==0) { printf("Client exit");

break; } } close(sockfd); }

OUTPUT Server [chithambaramani@localhost Net]$ cc UDPECHOSERVER.c -o UDPECHOSERVEROUTPUT [chithambaramani@localhost Net]$ ./UDPECHOSERVEROUTPUT 5477 Socket successfully created Socket successfully binded From Client: hai To Client hai From Client: how are u To Client how are u From Client: ya fine To Client ya fine Server exit Client [chithambaramani@localhost ~]$ cc UDPECHOCLIENT.c -o UDPECHOCLIENTOUTPUT [chithambaramani@localhost ~]$ ./UDPECHOCLIENTOUTPUT 127.0.0.1 5477 Socket successfully created Enter the string hai From server:hai Enter the string how are u From server:how are u Enter the string ya fine From server:ya fine Enter the string exit Client exit

UDP CHAT PROGRAM


UDPCHATSERVER.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main(int argc,char **argv) { int sockfd,len,n; struct sockaddr_in servaddr,cli; char buff[80]; if(argc!=2) { printf("\nplease specify the port number in command line argument\n"); exit(0); } sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("\nSocket creation failed"); exit(0); } else printf("\nSocket successfully created"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(atoi(argv[1])); if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))!=0) { printf("\nSocket binding failed"); exit(0); } else printf("\nSocket successfully binded"); len=sizeof(cli); for(;;) { bzero(buff,80); recvfrom(sockfd,buff,80,0,(struct sockaddr*)&cli,&len); printf("\nFrom Client: %s\n To Client \n",buff); bzero(buff,80); n=0; while((buff[n++]=getchar())!='\n'); sendto(sockfd,buff,80,0,(struct sockaddr*)&cli,len); if(strncmp("exit",buff,4)==0) { printf("Server exit");

break; } } close(sockfd); } UDPCHATCLIENT.c #include<stdlib.h> #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> void main(int argc,char **argv) { int sockfd,connfd; char buff[80]; int n,len; struct sockaddr_in servaddr,cli; if(argc!=3) { printf("\nplease specify the IP Address and Port Number in command line argument\n"); exit(0); } sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("\nSocket creation failed"); exit(0); } else printf("\nSocket successfully created"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(atoi(argv[2])); if (inet_aton(argv[1], &servaddr.sin_addr)==0) { printf("inet_aton() failed\n"); exit(1); } len=sizeof(servaddr); for(;;) { bzero(buff,80); printf("\nEnter the string "); n=0; while((buff[n++]=getchar())!='\n'); sendto(sockfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,len); bzero(buff,80); recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr*)&servaddr,&len); printf("\nFrom server:%s\n",buff);

if(strncmp("exit",buff,4)==0) { printf("Client exit"); break; } } close(sockfd); }

OUTPUT Server [student@localhost ~]$ cc UDPCHATSERVER.c [student@localhost ~]$ ./a.out 5441 Socket successfully created Socket successfully binded From Client: hai how are u fine To Client ya fine how about u From Client: ya fine ... how is ur college life To Client exit Server exit[student@localhost ~]$ Client [student@localhost ~]$ cc UDPCHATCILENT.c [student@localhost ~]$ ./a.out 127.0.0.1 5441 Socket successfully created Enter the string hai how are u fine From server:ya fine how about u Enter the string ya fine ... how is ur college life From server:exit Client exit[student@localhost ~]$

SLIDING WINDOW PROTOCOL


SLIDINGSERVER.C #include<stdio.h> #include<string.h> #include<sys/types.h> #include<netinet/in.h> #include<arpa/inet.h> #define SIZE 4 int main(int argc,char *argv[]) { int portno,sfd,lfd,len,i,j,k,status,choice; char str[20],frame[20],temp[20],ack[20],fram[20]; struct sockaddr_in saddr,caddr; sfd=socket(AF_INET,SOCK_STREAM,0); if(sfd<0) perror("Error"); bzero(&saddr,sizeof(saddr)); portno=atoi(argv[1]); saddr.sin_family=AF_INET; saddr.sin_addr.s_addr=htonl(INADDR_ANY); saddr.sin_port=htons(portno); if(bind(sfd,(struct sockaddr*)&saddr,sizeof(saddr))<0) perror("Bind error"); listen(sfd,5); len=sizeof(&caddr); lfd=accept(sfd,(struct sockaddr*)&caddr,&len); printf("\nEnter ur choice"); printf("\n1.Go-back ARQ"); printf("\n2.Selective Repeat ARQ"); scanf("%d",&choice); printf("Enter the text:\n"); scanf("%s",str); i=0; while(i<strlen(str)) { memset(frame,0,20); k=0; for(j=i;k<SIZE && i<strlen(str);k++,j++) frame[k]=str[j]; printf("Transmitting frames:"); len=strlen(frame); for(j=0;j<len;j++) { if(j>0) printf("|"); printf("%d",i+j); sprintf(temp,"%d",i+j); } printf("\n"); write(lfd,frame,sizeof(frame)); read(lfd,ack,20); sscanf(ack,"%d",&status);

if(status==1) printf("Transmission sucessful"); else{ printf("Received error in %d\n\n",status); printf("\nRetransmitting frame:"); read(lfd,ack,20); sscanf(ack,"%d",&status); k=0; bzero(fram,sizeof(fram)); switch(choice) { case 1: for(j=status;j<SIZE;j++) { fram[k]=frame[j]; k++; } break; case 2: fram[k]=frame[status]; break; } write(lfd,fram,sizeof(fram)); } i+=SIZE; } write(lfd,"exit",sizeof("exit")); printf("Exiting"); sleep(2); close(lfd); close(sfd); } SLIDINGCLIENT.c #include<stdio.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.h> #include<stdlib.h> int printerror(char *msg) { perror(msg); exit(1); } int main(int argc,char *argv[]) { int sockfd,portno,n,status,c; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256],err[20],err1[222];

if(argc<3) { printf("Error:Type %s<servername><portnumber>\n",argv[0]); exit(0); } portno=atoi(argv[2]); sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) { printerror("\n Error opening port"); } bzero(&serv_addr,sizeof(serv_addr)); serv_addr.sin_family=AF_INET; serv_addr.sin_port=htons(portno); inet_pton(AF_INET,argv[1],&serv_addr.sin_addr); if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0) printerror("\nError Connecting"); while(1) { bzero(buffer,256); read(sockfd,buffer,20); if(!strcmp(buffer,"exit")) { printf("\nExiting"); break; } else { if(n<0) printerror("\n error reading from the socket\n"); printf("\n%s",buffer); printf("\n Do U want to report on error:yes(1)/no(0):"); scanf("%d",&c); if(c==1) { write(sockfd,"-1",sizeof("-1")); } if(c==1) { bzero(err,20); printf("Enter the sequence no. when error occured:"); scanf("%s",err); write(sockfd,err,sizeof(err)); bzero(err1,sizeof(err1)); read(sockfd,err1,20); printf("\nReceived the transmitted frame\t%s\n",err1); } else write(sockfd,"1",sizeof("1")); } } }

OUTPUT Server [student@localhost ~]$ cc SLI_SERVER.c [student@localhost ~]$ ./a.out 5444 Enter ur choice 1.Go-back ARQ 2.Selective Repeat ARQ 1 Enter the text: haihaoeasfdsa Transmitting frames:0|1|2|3 Transmission sucessfulTransmitting frames:4|5|6|7 Received error in -1 Retransmitting frame: Transmitting frames:8|9|10|11 Transmitting frames:12 Transmission sucessful [student@localhost ~]$ Client [student@localhost ~]$ cc SLI_CLIENT.c [student@localhost ~]$ ./a.out 127.0.0.1 5444 haih Do U want to report on error:yes(1)/no(0):0 aoea Do U want to report on error:yes(1)/no(0):1 Enter the sequence no. when error occured:1 Received the transmitted frame oea

sfds Do U want to report on error:yes(1)/no(0):0 a Do U want to report on error:yes(1)/no(0):0 Exiting[student@localhost ~]$

FILE TRANSFER PROTOCOL


FTPSERVER.c # include <stdio.h> # include <stdlib.h> # include <sys/socket.h> # include <sys/types.h> # include <netdb.h> # include <string.h> int main(int argc,char **argv ) { int sd,b,cd,i=0; char fname[50],line[1000],temp,temp1; struct sockaddr_in caddr,saddr; FILE *fp; socklen_t clen=sizeof(caddr); if(argc!=2) { printf("\nUSAGE: $client <PORT> \n"); exit(0); } sd=socket(AF_INET,SOCK_STREAM,0); if(sd!=-1) printf("Socket is created\n"); else printf("Socket is not created\n"); saddr.sin_family=AF_INET; saddr.sin_addr.s_addr=htonl(INADDR_ANY); saddr.sin_port=htons(atoi(argv[1])); b=bind(sd,(struct sockaddr *)&saddr,sizeof(saddr)); if(b==0) printf("Binded to client\n"); else printf("Not Binded\n"); listen(sd,5); cd=accept(sd,(struct sockaddr *)&caddr,&clen); recv(cd,fname,sizeof(fname),0); while(i<strlen(fname)) { if(fname[i]=='.') { temp='1'; while(i<=strlen(fname)) { temp1=fname[i]; fname[i]=temp; temp=temp1; i++; } break; } i++; }

printf("\nReceived file name : %s",fname); fp=fopen(fname,"w"); recv(cd,line,sizeof(line),0); while(strcmp(line,"success")!=0) { fputs(line,fp); fputs(line,stdout); bzero(line,sizeof(line)); recv(cd,line,sizeof(line),0); } fclose(fp); printf("\nThe file has been transferred\n"); close(sd); close(cd); return 0; } FTPCLIENT.c # include <stdio.h> # include <stdlib.h> # include <sys/socket.h> # include <sys/types.h> # include <netdb.h> # include <string.h> int main(int argc,char **argv) { int sd,c,s; char fname[50],line[1000]; struct sockaddr_in caddr; FILE *fp; if(argc!=3) { printf("\nUSAGE: $client <ipaddress> <port> \n"); exit(0); } sd=socket(AF_INET,SOCK_STREAM,0); if(sd!=-1) printf("\nSocket is created\n"); else printf("\nSocket is not created\n"); caddr.sin_family=AF_INET; caddr.sin_addr.s_addr=inet_addr(argv[1]); caddr.sin_port=htons(atoi(argv[2])); c=connect(sd,(struct sockaddr *)&caddr,sizeof(caddr)); /* establish a connection with a TCP server */ if(c==0) printf("Connected to server\n"); else printf("Not connected\n"); printf("\nEnter the file name : "); scanf("%s",fname); send(sd,fname,sizeof(fname),0); if((fp = fopen(fname, "rb")) == NULL) {

printf("Sorry, can't open %s", fname); return -1; } while(!feof(fp)) { if(fgets(line,sizeof(line),fp)) { printf("\nsending %s ",line); send(sd,line,sizeof(line),0); } } send(sd,"success",sizeof("success"),0); fclose(fp); close(sd); return 0; }

OUTPUT Server [student@localhost ~]$ cc FTPSERVER.c [student@localhost ~]$ ./a.out 5411 Socket is created Binded to client Received file name : UDPECHOSERVER1.c The file has been transferred Client [student@localhost ~]$ cc FTPCLIENT.c [student@localhost ~]$ ./a.out 127.0.0.1 5411 Socket is created Connected to server Enter the file name : UDPECHOSERVER.c The file has been Received

OPEN SHORTEST PATH FIRST


PROGRAM.c #include<stdio.h> #include<string.h> int main() { int count,src_router,i,j,k,w,v,min; int cost_matrix[100][100],dist[100],last[100]; int flag[100]; printf("\n Enter the no of routers"); scanf("%d",&count); printf("\n Enter the cost matrix values:"); for(i=0;i<count;i++) { for(j=0;j<count;j++) { printf("\n%d->%d:",i,j); scanf("%d",&cost_matrix[i][j]); if(cost_matrix[i][j]<0) cost_matrix[i][j]=1000; } } printf("\n Enter the source router:"); scanf("%d",&src_router); for(v=0;v<count;v++) { flag[v]=0; last[v]=src_router; dist[v]=cost_matrix[src_router][v]; } flag[src_router]=1; for(i=0;i<count;i++) { min=1000; for(w=0;w<count;w++) { if(!flag[w]) if(dist[w]<min){ v=w; min=dist[w]; } } flag[v]=1; for(w=0;w<count;w++) { if(!flag[w]) if(min+cost_matrix[v][w]<dist[w]) { dist[w]=min+cost_matrix[v][w]; last[w]=v; }

} } for(i=0;i<count;i++) { printf("\n%d==>%d:Path taken:%d",src_router,i,i); w=i; while(w!=src_router) { printf("\n<--%d",last[w]); w=last[w]; } if(dist[i]!=1000) printf("\n Shortest path cost:%d",dist[i]); else printf("\n NO Path..."); } }

OUTPUT [student@localhost ~]$ cc ospfb.c [student@localhost ~]$ ./a.out Enter the no of routers3 Enter the cost matrix values: 0->0:-1 0->1:3 0->2:4 1->0:-1 1->1:-1 1->2:7 2->0:6 2->1:4 2->2:2 Enter the source router:2 2==>0:Path taken:0 <--2 Shortest path cost:6 2==>1:Path taken:1 <--2 Shortest path cost:4 2==>2:Path taken:2 [student@localhost ~]$

DOMAIN NAME SYSTEM DNSSERVER.c #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> void main(int argc ,char **argv) { int cid,sid,aid,n,i=0,j=0,x,m=0; char c,mesg[100],mesg1[100]; char dns[3][20]={"yahoo","rediff","google"}; char ip[3][20]={"0.0.0.154","0.0.0.765","0.0.0.978"}; struct sockaddr_in a; if(argc!=2) { printf("Usage required:<port no>"); exit(0); } bzero(mesg1,sizeof(mesg1)); bzero(mesg,sizeof(mesg)); sid=socket(AF_INET,SOCK_STREAM,0); printf("\nThe value of socket id is %d",sid); if(sid<0) printf("\nError in allocating the socket"); else printf("\nSocket created successfully"); a.sin_family=AF_INET; a.sin_addr.s_addr=htons(INADDR_ANY); a.sin_port=htons(atoi(argv[1])); cid=bind(sid,(struct sockaddr *)&a,sizeof(a)); if(cid<0) printf("\nError in binding"); else printf("\nBinding success"); listen(sid,5); aid=accept(sid,(struct sockaddr *)NULL,NULL); if(aid<0) printf("\nError in establishing connection"); else printf("\nConnection established successfully"); m=0; do { m++; recv(aid,mesg,100,0); printf("\nDomain Name from client:"); puts(mesg); printf("IP Address is sending"); for(i=0;i<3;i++) {

if(strcmp(mesg,dns[i])==0) {x=0;break;} else x=1; } if(x==0) { strcpy(mesg1,ip[i]); } else { m=0; strcpy(mesg1,"Enter the correct DOMAIN NAME"); } send(aid,mesg1,sizeof(mesg1),0); printf("\n"); bzero(mesg1,sizeof(mesg1)); bzero(mesg,sizeof(mesg)); }while(m!=1); close(sid); } DNSCLIENT.c #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> void main(int argc ,char **argv) { int cid,sid,aid,n,i=0,j=0,m=0; char c,mesg[100],mesg1[100]; struct sockaddr_in a; bzero( mesg,sizeof(mesg)); bzero(mesg1,sizeof(mesg1)); if(argc!=3) { printf("\nUsage required <IP_ADDRESS><PORT>"); exit(0); } sid=socket(AF_INET,SOCK_STREAM,0); printf("\nThe value of socket id is %d",sid); if(sid<0) printf("\nError in allocating the socket"); else printf("\nSocket created successfully"); a.sin_family=AF_INET; a.sin_addr.s_addr=inet_addr(argv[1]); a.sin_port=htons(atoi(argv[2])); cid=connect(sid,(struct sockaddr*)&a,sizeof(a)); if(cid<0)

printf("\nError in Connection"); else printf("\nConnection success"); m=0; do { m++; printf("\nEnter the Domain Name(yahoo,google,rediff) :"); scanf("%s",mesg); for(i=0;i<strlen(mesg);i++) mesg1[i]=mesg[i]; send(sid,mesg1,sizeof(mesg1),0); bzero(mesg1,sizeof(mesg1)); recv(sid,mesg1,100,0); printf("\nIP Address from server\n"); puts(mesg1); if(strcmp("Enter the correct DOMAIN NAME",mesg1)==0) m=0; bzero( mesg,sizeof(mesg)); bzero(mesg1,sizeof(mesg1)); }while(m!=1); close(sid); }

OUTPUT Server [student@localhost ~]$ cc DNSERVER.c [student@localhost ~]$ ./a.out 5805 The value of socket id is 3 Socket created successfully Binding success Connection established successfully Domain Name from client:google IP Address is sending [student@localhost ~]$ Client [student@localhost ~]$ cc DNCLIENT.c [student@localhost ~]$ ./a.out 127.0.0.1 5805 The value of socket id is 3 Socket created successfully Connection success Enter the Domain Name(yahoo,google,rediff) :google IP Address from server 0.0.0.978 [student@localhost ~]$

THE HYPERTEXT TRANSFER PROTOCOL HTTPSERVER.c #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<sys/socket.h> int main() { int sd,nsd,i,port; char line[1000]="\0",fname[30]="\0"; struct sockaddr_in ser,cli; FILE *fp; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("\nError in socket creation\n"); return 0; } printf("Enter no\n "); scanf("%d",&port); bzero((char *)&ser,sizeof(ser)); printf("\nPort address is %d\n",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=htonl(INADDR_ANY); if(bind(sd,(struct sockaddr *)&ser,sizeof(ser))) { printf("Binding problem,change the port"); return 0; } i=sizeof(cli); listen(sd,1); printf("Server module\n"); printf("-------------\n"); nsd=accept(sd,(struct sockaddr *)&cli,&i); if(nsd==-1) { printf("Client accept problem"); return 0; } printf("\n Client accepted\n"); recv(nsd,fname,30,0); if((fp = fopen(fname, "r")) == NULL) { printf("Sorry, can't open %s", fname); return -1; } do { fgets(line,sizeof(line),fp); printf("\nsending %s ",line);

send(nsd,line,sizeof(line),0); bzero(line,sizeof(line)); }while(!feof(fp)) ; send(nsd,"EOF",4,0); printf("File transferred destination"); fclose(fp); close(sd); close(nsd); return 0; } HTTPCLIENT.c #include<stdio.h> #include<string.h> #include<sys/types.h> #include<netinet/in.h> #include<sys/socket.h> main() { int sd,i,port; char line[1000],fname[30],file[30]; struct sockaddr_in ser; FILE *fp; if((sd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1) { printf("Socket creation problem"); return 0; } printf("Enter port no:"); scanf("%d",&port); bzero((char *)&ser,sizeof(ser)); printf("port address is %d \n",port); ser.sin_family=AF_INET; ser.sin_port=htons(port); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); if(connect(sd,(struct sockaddr *)&ser,sizeof(ser))==-1) { printf("Connect problem"); return 0; } printf("---------------------------------------------------------\n") ; printf("Enter the path name of source file\n"); scanf("%s",fname); printf("Enter the path name of destination\n"); scanf("%s",file); send(sd,fname,30,0); if((fp = fopen(file, "w")) == NULL) { printf("Sorry, can't open %s", fname); return -1; } do {

recv(sd,line,sizeof(line),0); fputs(line,fp); fputs(line,stdout); if(strcmp(line,"EOF")==0) break; bzero(line,sizeof(line)); }while(1); printf("\n File received\n"); fclose(fp); close(sd); return 0; }

OUTPUT Server [student@localhost ~]$ cc HTTPSERVER.c [student@localhost ~]$ ./a.out Enter no Enter no 4441 Port address is 4441 Server module ------------Client accepted sending Client [student@localhost ~]$ cc HTTPCLIENT.c [student@localhost ~]$ ./a.out Enter port no:4441 port address is 4441 --------------------------------------------------------Enter the path name of source file AGENT1.c Enter the path name of destination HTTPSERVER.c File received [student@localhost ~]$ File transferred destination[

SIMPLE NETWORK MANAGEMENT PROTOCOL

AGENT1.c #include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> void main() { int sd,sd2,nsd,clilen,sport,len,i; char sendmsg[20],rcvmsg[100]; char oid[5][10]={"client1","client2","client3","client4","client5"}; char wsize[5][5]={"5","10","15","3","6"}; printf("\n I am the Agent 1- Tcp connection \n"); struct sockaddr_in servaddr,cliaddr; printf("ENTER THE PORT NUMBER"); scanf("%d", &sport); sd=socket(AF_INET,SOCK_STREAM,0); if(sd<0) printf("\n CANNOT CREATE SOCKET \n"); else printf(" SOCKET IS CREATED \n"); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(sport); sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr)); if(sd2<0) { printf("%d",sd2); printf("\n CANNOT BIND \n"); } else { printf("%d",sd2); printf("\n BINDED \n"); } listen(sd,5); clilen=sizeof(cliaddr); nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen); if(nsd<0) printf("\n CANNOT ACCEPT"); else printf("\n ACCEPTED"); recv(nsd,rcvmsg,100,0); for(i=0;i<5;i++) { if(strcmp(rcvmsg,oid[i])==0) { send(nsd,wsize[i],100,0); break; } } } AGENT2.c

#include <stdio.h> #include <sys/socket.h> #include <netinet/in.h> void main() { int i,sd,sd2,nsd,clilen,sport,len; char sendmsg[20],rcvmsg[20]; char oid[5][10]={"sys1","sys2","sys3","sys4","sys5"}; char mdate[5] [15]={"01.01.2006","10.10.2006","11.12.2005","17.10.2007","16.9.200 6"}; char time_l_u[5][15]={"9.00am","10.00am","9.00pm","8pm","3am"}; printf("\n I am the agent 2 system \n"); struct sockaddr_in servaddr,cliaddr; printf("ENTER THE PORT NUMBER"); scanf("%d", &sport); sd=socket(AF_INET,SOCK_STREAM,0); if(sd<0) printf("\n CANNOT CREATE SOCKET \n"); else printf(" SOCKET IS CREATED \n"); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(sport); sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr)); if(sd2<0) { printf("%d",sd2); printf("\n CANNOT BIND \n"); } else { printf("%d",sd2); printf("\n BINDED \n"); } listen(sd,5); clilen=sizeof(cliaddr); nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen); if(nsd<0) printf("\n CANNOT ACCEPT"); else printf("\n ACCEPTED"); recv(nsd,rcvmsg,20,0); for(i=0;i<5;i++) { if(strcmp(rcvmsg,oid[i])==0) { send(nsd,mdate[i],20,0); send(nsd,time_l_u[i],20,0); break; } } }

MANAGER.c #include <stdio.h> #include<sys/socket.h> #include<netinet/in.h> void main() { int csd,cport,len,i; char sendmsg[100],rcvmsg[20],oid[100],rmsg[20]; struct sockaddr_in servaddr; printf("\n ENTER THE PORT NUMBER"); scanf("%d",&cport); csd=socket(AF_INET,SOCK_STREAM,0); if (csd<0) printf("\n CANNOT CREATE SOCKET\n"); else printf("SOCKET IS CREATED \n"); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(cport); if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) printf("\n CANNOT CONNECT \n"); else printf("CONNECTED \n"); printf("1. TCP connection \n"); printf("2. System \n"); printf("ENTER THE NO FOR THE TYPE OF INFORMATION NEEDED \n"); scanf("%d",&i); if(i==1) { printf("ENTER THE OBJECT ID FOR CLIENT !\n"); scanf("%s",oid); send(csd,oid,100,0); recv(csd,rmsg,100,0); printf("%s",rmsg); printf("THE WINDOW SIZE FOR %s is %s",oid,rmsg); } else { printf("ENTER THE OBJECT ID FOR THE SYSTEM \n"); scanf("%s",oid); send(csd,oid,100,0); recv(csd,rmsg,20,0); printf("THE MANUFACTURING DATE FOR %s IS %s",oid,rmsg); recv(csd,rmsg,100,0); printf("THE TIME OF LAST UTILIZATION FOR %s IS %s",oid,rmsg); } }

OUTPUT

ACCEPTED[student@localhost ~]$ cc AGENT1.c [student@localhost ~]$ ./a.out I am the Agent 1- Tcp connection ENTER THE PORT NUMBER5600 SOCKET IS CREATED 0 BINDED [student@localhost ~]$ ./a.out I am the agent 2 system ENTER THE PORT NUMBER4444 SOCKET IS CREATED 0 BINDED ACCEPTED[student@localhost ~]$

[student@localhost ~]$ cc MANAGER.c [student@localhost ~]$ ./a.out ENTER THE PORT NUMBER4444 SOCKET IS CREATED CONNECTED 1. TCP connection 2. System ENTER THE NO FOR THE TYPE OF INFORMATION NEEDED 2 ENTER THE OBJECT ID FOR THE SYSTEM sys1 THE MANUFACTURING DATE FOR sys1 IS 01.01.2006THE TIME OF LAST UTILIZATION FOR sys1 IS 9.00am[student@localhost ~]$

ADDRESS RESOLUTION PROTOCOL


ARPSERVER.c #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> void main(int argc ,char **argv) { int cid,sid,aid,n,i=0,j=0,x,m=0; char c,mesg[100],mesg1[100]; char ip[3][20]={"172.16.5.200","172.16.5.57","172.16.5.10"}; char mac[3][20]={"0.0.0.154","0.0.0.765","0.0.0.978"}; struct sockaddr_in a; if(argc!=2) { printf("Usage required:<port no>"); exit(0); } bzero(mesg1,sizeof(mesg1)); bzero(mesg,sizeof(mesg)); sid=socket(AF_INET,SOCK_STREAM,0); printf("\nThe value of socket id is %d",sid); if(sid<0) printf("\nError in allocating the socket"); else printf("\nSocket created successfully"); a.sin_family=AF_INET; a.sin_addr.s_addr=htons(INADDR_ANY); a.sin_port=htons(atoi(argv[1])); cid=bind(sid,(struct sockaddr *)&a,sizeof(a)); if(cid<0) printf("\nError in binding"); else printf("\nBinding success"); listen(sid,5); aid=accept(sid,(struct sockaddr *)NULL,NULL); if(aid<0) printf("\nError in establishing connection"); else printf("\nConnection established successfully"); m=0; do { m++; recv(aid,mesg,100,0); printf("\nIP Address from client:"); puts(mesg); printf("MAC Address is sending"); for(i=0;i<3;i++) {

if(strcmp(mesg,ip[i])==0) {x=0;break;} else x=1; } if(x==0) { strcpy(mesg1,mac[i]); } else { m=0; strcpy(mesg1,"Enter the correct IP Address"); } printf("%s",mesg1); send(aid,mesg1,sizeof(mesg1),0); printf("\n"); bzero(mesg1,sizeof(mesg1)); bzero(mesg,sizeof(mesg)); }while(m!=1); close(sid); } ARPCLIENT.C #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> void main(int argc ,char **argv) { int cid,sid,aid,n,i=0,j=0,m=0; char c,mesg[100],mesg1[100],mac[100],ip[100]; struct sockaddr_in a; bzero( mesg,sizeof(mesg)); bzero(mesg1,sizeof(mesg1)); if(argc!=3) { printf("\nUsage required <IP_ADDRESS><PORT>"); exit(0); } sid=socket(AF_INET,SOCK_STREAM,0); printf("\nThe value of socket id is %d",sid); if(sid<0) printf("\nError in allocating the socket"); else printf("\nSocket created successfully"); a.sin_family=AF_INET; a.sin_addr.s_addr=inet_addr(argv[1]);

a.sin_port=htons(atoi(argv[2])); cid=connect(sid,(struct sockaddr*)&a,sizeof(a)); if(cid<0) printf("\nError in Connection"); else printf("\nConnection success"); m=0; do { m++; printf("\nEnter ur IPAddress:"); scanf("%s",mesg); for(i=0;i<strlen(mesg);i++) mesg1[i]=mesg[i]; send(sid,mesg1,sizeof(mesg1),0); bzero(mesg1,sizeof(mesg1)); recv(sid,mesg1,100,0); printf("\nMac Address from server\n"); puts(mesg1); if(strcmp("Enter the correct IP Address",mesg1)==0) m=0; bzero( mesg,sizeof(mesg)); bzero(mesg1,sizeof(mesg1)); }while(m!=1); close(sid); }

OUTPUT Server [student@localhost ~]$ cc ARPSERVER.c [student@localhost ~]$ ./a.out 5601 The value of socket id is 3 Socket created successfully Binding success Connection established successfully IP Address from client:172.16.5.57 MAC Address is sending0.0.0.765 [student@localhost ~]$ Client [student@localhost ~]$ cc ARPCLIENT.c [student@localhost ~]$ ./a.out 127.0.0.1 5601 The value of socket id is 3 Socket created successfully Connection success Enter ur IPAddress:172.16.5.57 Mac Address from server 0.0.0.765 [student@localhost ~]$

REVERSE ADDRESS RESOLUTION PROTOCOL


RARPSERVER.c #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> void main(int argc ,char **argv) { int cid,sid,aid,n,i=0,j=0,x,m=0; char c,mesg[100],mesg1[100]; char ip[3][20]={"172.16.5.200","172.16.5.57","172.16.5.10"}; char mac[3][20]={"0.0.0.154","0.0.0.765","0.0.0.978"}; struct sockaddr_in a; if(argc!=2) { printf("Usage required:<port no>"); exit(0); } bzero(mesg1,sizeof(mesg1)); bzero(mesg,sizeof(mesg)); sid=socket(AF_INET,SOCK_STREAM,0); printf("\nThe value of socket id is %d",sid); if(sid<0) printf("\nError in allocating the socket"); else printf("\nSocket created successfully"); a.sin_family=AF_INET; a.sin_addr.s_addr=htons(INADDR_ANY); a.sin_port=htons(atoi(argv[1])); cid=bind(sid,(struct sockaddr *)&a,sizeof(a)); if(cid<0) printf("\nError in binding"); else printf("\nBinding success"); listen(sid,5); aid=accept(sid,(struct sockaddr *)NULL,NULL); if(aid<0) printf("\nError in establishing connection"); else printf("\nConnection established successfully"); m=0; do { m++; recv(aid,mesg,100,0); printf("\nMAC Address from client:"); puts(mesg); printf("IP Address is sending"); for(i=0;i<3;i++) {

if(strcmp(mesg,mac[i])==0) {x=0;break;} else x=1; } if(x==0) { strcpy(mesg1,ip[i]); } else { m=0; strcpy(mesg1,"Enter the correct MAC Address"); } send(aid,mesg1,sizeof(mesg1),0); printf("\n"); bzero(mesg1,sizeof(mesg1)); bzero(mesg,sizeof(mesg)); }while(m!=1); close(sid); } RARPClENT.C #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> void main(int argc ,char **argv) { int cid,sid,aid,n,i=0,j=0,m=0; char c,mesg[100],mesg1[100],mac[100],ip[100]; struct sockaddr_in a; bzero( mesg,sizeof(mesg)); bzero(mesg1,sizeof(mesg1)); if(argc!=3) { printf("\nUsage required <IP_ADDRESS><PORT>"); exit(0); } sid=socket(AF_INET,SOCK_STREAM,0); printf("\nThe value of socket id is %d",sid); if(sid<0) printf("\nError in allocating the socket"); else printf("\nSocket created successfully"); a.sin_family=AF_INET; a.sin_addr.s_addr=inet_addr(argv[1]); a.sin_port=htons(atoi(argv[2])); cid=connect(sid,(struct sockaddr*)&a,sizeof(a)); if(cid<0)

printf("\nError in Connection"); else printf("\nConnection success"); m=0; do { m++; printf("\nEnter ur MAC_Address:"); scanf("%s",mesg); for(i=0;i<strlen(mesg);i++) mesg1[i]=mesg[i]; send(sid,mesg1,sizeof(mesg1),0); bzero(mesg1,sizeof(mesg1)); recv(sid,mesg1,100,0); printf("\nIP Address from server\n"); puts(mesg1); if(strcmp("Enter the correct MAC Address",mesg1)==0) m=0; bzero( mesg,sizeof(mesg)); bzero(mesg1,sizeof(mesg1)); }while(m!=1); close(sid); }

OUTPUT Server [student@localhost ~]$ cc RARPSERVER.c [student@localhost ~]$ ./a.out 5701 The value of socket id is 3 Socket created successfully Binding success Connection established successfully MAC Address from client:0.0.0.765 IP Address is sending [student@localhost ~]$ Client [student@localhost ~]$ cc RARPCLIENT.c [student@localhost ~]$ ./a.out 127.0.0.1 5701 The value of socket id is 3 Socket created successfully Connection success Enter ur MAC_Address:0.0.0.765 IP Address from server 172.16.5.57 [student@localhost ~]$

STUDY OF NETWORK SIMULATOR PACKAGES NS-2 AIM


To make a descriptive study of the Network Simulator NS-2.

OVERVIEW
Ns is a discrete event simulator targeted at networking research. Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks. Ns began as a variant of the REAL network simulator in 1989 and has evolved substantially over the past few years. In 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UCB, and USC/ISI. Currently ns development is support through DARPA with SAMAN and through NSF with CONSER, both in collaboration with other researchers including ACIRI. Ns has always included substantal contributions from other researchers, including wireless code from the UCB Daedelus and CMU Monarch projects and Sun Microsystems.

Figure 1. Simplified User's View of NS As shown in Figure 1, in a simplified user's view, NS is Objectoriented Tcl (OTcl) script interpreter that has a simulation event scheduler and network component object libraries, and network setup (plumbing) module libraries (actually, plumbing modules are implemented as member functions of the base simulator object). In other words, to use NS, you program in OTcl script language. To setup and run a simulation network, a user should write an OTcl script that initiates an event scheduler, sets up the network topology using the network objects and the plumbing functions in the library, and tells traffic sources when to start and stop transmitting packets through the event scheduler. The term "plumbing" is used for a network setup, because setting up a network is plumbing possible data paths among network objects by setting the "neighbor" pointer of an object to the address of an appropriate object. When a user wants to make a new network object, he or she can easily make an object either by writing

a new object or by making a compound object from the object library, and plumb the data path through the object. This may sound like complicated job, but the plumbing OTcl modules actually make the job very easy. The power of NS comes from this plumbing. Another major component of NS beside network objects is the event scheduler. An event in NS is a packet ID that is unique for a packet with scheduled time and the pointer to an object that handles the event. In NS, an event scheduler keeps track of simulation time and fires all the events in the event queue scheduled for the current time by invoking appropriate network components, which usually are the ones who issued the events, and let them do the appropriate action associated with packet pointed by the event. Network components communicate with one another passing packets, however this does not consume actual simulation time. All the network components that need to spend some simulation time handling a packet (i.e. need a delay) use the event scheduler by issuing an event for the packet and waiting for the event to be fired to itself before doing further action handling the packet. For example, a network switch component that simulates a switch with 20 microseconds of switching delay issues an event for a packet to be switched to the scheduler as an event 20 microsecond later. The scheduler after 20 microsecond dequeues the event and fires it to the switch component, which then passes the packet to an appropriate output link component. Another use of an event scheduler is timer. For example, TCP needs a timer to keep track of a packet transmission time out for retransmission (transmission of a packet with the same TCP packet number but different NS packet ID). Timers use event schedulers in a similar manner that delay does. The only difference is that timer measures a time value associated with a packet and does an appropriate action related to that packet after a certain time goes by, and does not simulate a delay. NS is written not only in OTcl but in C++ also. For efficiency reason, NS separates the data path implementation from control path implementations. In order to reduce packet and event processing time (not simulation time), the event scheduler and the basic network component objects in the data path are written and compiled using C+ +. These compiled objects are made available to the OTcl interpreter through an OTcl linkage that creates a matching OTcl object for each of the C++ objects and makes the control functions and the configurable variables specified by the C++ object act as member functions and member variables of the corresponding OTcl object. In this way, the controls of the C++ objects are given to OTcl. It is also possible to add member functions and variables to a C++ linked OTcl object. The objects in C++ that do not need to be controlled in a simulation or internally used by another object do not need to be linked to OTcl. Likewise, an object (not in the data path) can be entirely implemented in OTcl. Figure 2 shows an object hierarchy example in C++ and OTcl. One thing to note in the figure is that for C++ objects that have an OTcl linkage forming a hierarchy, there is a matching OTcl object hierarchy very similar to that of C++.

Figure 2. C++ and OTcl: The Duality

Figure 3. Architectural View of NS Figure 3 shows 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 objects in the OTcl library. The event schedulers and most of the network components are implemented in C++ and available to OTcl through an OTcl linkage that is implemented using tclcl. The whole thing together makes NS, which is a OO extended Tcl interpreter with network simulator libraries. This section briefly examined the general structure and architecture of NS. At this point, one might be wondering about how to obtain NS simulation results. As shown in Figure 1, when a simulation is finished, NS produces one or more text-based 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 (two simulation result analysis examples are presented in later sections) or as an input to a graphical simulation display tool called Network Animator (NAM) that is developed as a part of VINT project. NAM has a nice graphical user interface similar to that of a CD player (play, fast forward, rewind, pause and so on), and also has a display speed controller. Furthermore, it can graphically present information such as throughput and number of packet drops at each link, although the graphical information cannot be used for accurate simulation analysis.

DESIGN

ns was built in C++ and provides a simulation interface through OTcl, an object-oriented dialect of Tcl. The user describes a network topology by writing OTcl scripts, and then the main ns program simulates that topology with specified parameters.

Showing NS split objects model. Object created on OTcl has a corresponding object in C++

ns2 Architecture

ns2 Simulation Flowchart

USES OF NETWORK SIMULATORS


Network simulators serve a variety of needs. Compared to the cost and time involved in setting up an entire test bed containing multiple networked computers, routers and data links, network simulators are relatively fast and inexpensive. They allow engineers to test scenarios that might be particularly difficult or expensive to emulate using real hardware- for instance, simulating the effects of a sudden burst in traffic or a DoS attack on a network service. Networking simulators are particularly useful in allowing designers to test new networking protocols or changes to existing protocols in a controlled and reproducible environment. Network simulators, as the name suggests are used by researchers to design various kinds of networks, simulate and then analyze the effect of various parameters on the network performance. A typical network simulator like NS2 encompasses a wide range of networking technologies and helps the users to build complex networks from basic

building blocks like variety of nodes and links. With the help of simulators one can design hierarchical networks using various types of nodes like computers, hubs, bridges, routers, optical crossconnects, multicast routers, mobile units, MSAUs etc.

TYPES OF NETWORK SIMULATORS


Various types of Wide Area Network (WAN) technologies like TCP, ATM, IP etc and Local Area Network (LAN) technologies like Ethernet, token rings etc. can all be simulated with a typical simulator and the user can test, analyze various standard results apart from devising some novel protocol or strategy for routing etc. There are a wide variety of network simulators, ranging from the very simple to the very complex. Minimally, a network simulator must enable a user to represent a network topology, specifying the nodes on the network, the links between those nodes and the traffic between the nodes. More complicated systems may allow the user to specify everything about the protocols used to handle network traffic. Graphical applications allow users to easily visualize the workings of their simulated environment. Text-based applications may provide a less intuitive interface, but may permit more advanced forms of customization. Others, such as GTNets, are programming-oriented, providing a programming framework that the user then customizes to create an application that simulates the networking environment to be tested.

RESULT
Thus the descriptive study about the Network Simulator NS2 was done.

PROGRAM.tcl set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set nf [open out.nam w] $ns namtrace-all $nf #Define a 'finish' procedure proc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & exit 0 } set n0 [$ns node] set n1 [$ns node] $ns duplex-link $n0 $n1 1Mb 10ms DropTail set tcp0 [ new Agent/TCP ] $ns attach-agent $n0 $tcp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $tcp0 set sink0 [ new Agent/TCPSink ] $ns attach-agent $n1 $sink0 $ns connect $tcp0 $sink0 $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop" $ns at 5.0 "finish" $ns run

OUTPUT:

PROGRAM.tcl set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc finish {} { global ns file2 $ns flush-trace close $file2 exec nam out.nam & exit 0 } #Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns simplex-link $n2 $n3 0.3Mb 100ms DropTail $ns simplex-link $n3 $n2 0.3Mb 100ms DropTail $ns duplex-link $n3 $n4 0.5Mb 40ms DropTail $ns duplex-link $n3 $n5 0.5Mb 30ms DropTail #Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns simplex-link-op $n2 $n3 orient right $ns simplex-link-op $n3 $n2 orient left $ns duplex-link-op $n3 $n4 orient right-up $ns duplex-link-op $n3 $n5 orient right-down #Setup a TCP connection set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n4 $sink $ns connect $tcp $sink $tcp set fid_ 1 $tcp set window_ 8000 $tcp set packetSize_ 552 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp

set null [new Agent/Null] $ns attach-agent $n5 $null $ns connect $udp $null $udp set fid_ 2 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 0.01mb $cbr set random_ false $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 124.0 "$ftp stop" $ns at 124.5 "$cbr stop" $ns at 125.0 "finish" $ns run

OUTPUT:

PROGRAM.tcl set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc finish {} { global nsfile2 $ns flush-trace close $file2 exec nam out.nam & exit 0 } # Next line should be commented out to have the static routing $ns rtproto DV #Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n1 0.3Mb 10ms $ns duplex-link $n1 $n2 0.3Mb 10ms $ns duplex-link $n2 $n3 0.3Mb 10ms $ns duplex-link $n1 $n4 0.3Mb 10ms $ns duplex-link $n3 $n5 0.5Mb 10ms $ns duplex-link $n4 $n5 0.5Mb 10ms DropTail DropTail DropTail DropTail DropTail DropTail

#Give node position (for NAM) $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n1 $n2 orient right $ns duplex-link-op $n2 $n3 orient up $ns duplex-link-op $n1 $n4 orient up-left $ns duplex-link-op $n3 $n5 orient left-up $ns duplex-link-op $n4 $n5 orient right-up #Setup a TCP connection set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n5 $sink $ns connect $tcp $sink $tcp set fid_ 2 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP

$ns $ns $ns $ns $ns

rtmodel-at 1.0 down $n1 $n4 rtmodel-at 4.5 up $n1 $n4 at 0.1 "$ftp start" at 6.0 "finish" run

OUTPUT:

PROGRAM.tcl set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc finish {} { global ns file2 $ns flush-trace close $file2 exec nam out.nam & exit 0 } # Next line $ns rtproto #Create six set n0 [$ns set n1 [$ns set n2 [$ns set n3 [$ns set n4 [$ns set n5 [$ns should be commented out to have the static routing LS nodes node] node] node] node] node] node] DropTail DropTail DropTail DropTail DropTail DropTail

#Create links between the nodes $ns duplex-link $n0 $n1 0.3Mb 10ms $ns duplex-link $n1 $n2 0.3Mb 10ms $ns duplex-link $n2 $n3 0.3Mb 10ms $ns duplex-link $n1 $n4 0.3Mb 10ms $ns duplex-link $n3 $n5 0.5Mb 10ms $ns duplex-link $n4 $n5 0.5Mb 10ms

#Give node position (for NAM) $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n1 $n2 orient right $ns duplex-link-op $n2 $n3 orient up $ns duplex-link-op $n1 $n4 orient up-left $ns duplex-link-op $n3 $n5 orient left-up $ns duplex-link-op $n4 $n5 orient right-up #Setup a TCP connection set tcp [new Agent/TCP/Newreno] $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink/DelAck] $ns attach-agent $n5 $sink $ns connect $tcp $sink $tcp set fid_ 2 #Setup a FTP over TCP connection

set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP $ns $ns $ns $ns $ns rtmodel-at 1.0 down $n1 $n4 rtmodel-at 4.5 up $n1 $n4 at 0.1 "$ftp start" at 6.0 "finish" run

OUTPUT:

PROGRAM.tcl set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc finish {} { global ns file2 $ns flush-trace close $file2 exec nam out.nam & exit 0 } # Next line should be commented out to have the static routing $ns rtproto LS #Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns simplex-link $n2 $n3 0.3Mb 100ms DropTail $ns simplex-link $n3 $n2 0.3Mb 100ms DropTail $ns duplex-link $n3 $n4 0.5Mb 40ms DropTail $ns duplex-link $n3 $n5 0.5Mb 30ms DropTail #Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns simplex-link-op $n2 $n3 orient right $ns simplex-link-op $n3 $n2 orient left $ns duplex-link-op $n3 $n4 orient right-up $ns duplex-link-op $n3 $n5 orient right-down #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 20 #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n5 $null $ns connect $udp $null $udp set fid_ 2 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000

$cbr set rate_ 0.01mb $cbr set random_ false $ns at 0.1 "$cbr start" $ns at 124.5 "$cbr stop" # next procedure gets two arguments: the name of the # tcp source node, will be called here "tcp", # and the name of output file. $ns at 125.0 "finish" $ns run OUTPUT:

PROGRAM.tcl set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set file2 [open out.nam w] $ns namtrace-all $file2 #Define a 'finish' procedure proc finish {} { global ns file2 $ns flush-trace close $file2 exec nam out.nam & exit 0 } # Next line should be commented out to have the static routing $ns rtproto LS #Create six nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] set n4 [$ns node] set n5 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns simplex-link $n2 $n3 0.3Mb 100ms DropTail $ns simplex-link $n3 $n2 0.3Mb 100ms DropTail $ns duplex-link $n3 $n4 0.5Mb 40ms DropTail $ns duplex-link $n3 $n5 0.5Mb 30ms DropTail #Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns simplex-link-op $n2 $n3 orient right $ns simplex-link-op $n3 $n2 orient left $ns duplex-link-op $n3 $n4 orient right-up $ns duplex-link-op $n3 $n5 orient right-down #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 20 #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n5 $null $ns connect $udp $null $udp set fid_ 2 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000

$cbr set rate_ 0.01mb $cbr set random_ false $ns at 0.1 "$cbr start" $ns at 124.5 "$cbr stop" # next procedure gets two arguments: the name of the # tcp source node, will be called here "tcp", # and the name of output file. $ns at 125.0 "finish" $ns run OUTPUT:

Вам также может понравиться