network lab program
TRANSCRIPT
-
7/29/2019 Network Lab Program
1/66
TCP ECHO PROGRAM
TCPECHOSERVER.c
#include
#include#include#include#include#include#includevoid 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 lineargument\n");}sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd==-1){printf("socket creation failed...\n");exit(0);}elseprintf("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);}elseprintf("server listening..\n");while(1){len=sizeof(cli);
connfd=accept(sockfd,(struct sockaddr *)&cli,&len);if(connfd
-
7/29/2019 Network Lab Program
2/66
printf("server accept the failed...\n");exit(0);}elseprintf("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#include#include#include#include
#include#includevoid main(int argc,int **argv){int sockfd,connfd,n;struct sockaddr_in servaddr,cli;char buff[80];if(argc!=3){printf("\nUSAGE: $client \n");exit(0);
}sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd==-1){printf("socket creation failed...\n");exit(0);}elseprintf("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#include#include
-
7/29/2019 Network Lab Program
3/66
#include#include#include#includevoid main(int argc,int **argv){int sockfd,connfd,n;struct sockaddr_in servaddr,cli;char buff[80];if(argc!=3){printf("\nUSAGE: $client \n");exit(0);}sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd==-1){printf("socket creation failed...\n");
exit(0);}elseprintf("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);}elseprintf("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);}
-
7/29/2019 Network Lab Program
4/66
OUTPUT
Server
[chithambaramani@localhost Net]$ cc TCPECHOSERVER.c -oTCPECHOSERVEROUTPUT[chithambaramani@localhost Net]$ ./TCPECHOSERVEROUTPUT 5041socket successfully created..socket successfully binded...server listening..server accept the clientfrom client:hai how are youto client :hai how are youfrom client:ya fineto client :ya fineserver exit...
Client
[chithambaramani@localhost ~]$ cc TCPECHOCLIENT.c -oTCPECHOCLIENTOUTPUT[chithambaramani@localhost ~]$./TCPECHOCLIENTOUTPUT 127.0.0.1 5041socket successfully created..connected to the server...enter the string:hai how are youfrom server :hai how are youenter the string:ya finefrom server :ya fineenter the string:exit
client exit..
-
7/29/2019 Network Lab Program
5/66
TCP CHAT PROGRAM
TCPCHATSERVER.c
#include#include#include#include#include#include#include
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 lineargument\n");}sockfd=socket(AF_INET,SOCK_STREAM,0);if(sockfd==-1){printf("socket creation failed...\n");
exit(0);}elseprintf("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);}elseprintf("socket successfully binded...\n");if((listen(sockfd,5))!=0){printf("listen failed...\n");exit(0);}elseprintf("server listening..\n");while(1)
{len=sizeof(cli);connfd=accept(sockfd,(struct sockaddr *)&cli,&len);
-
7/29/2019 Network Lab Program
6/66
if(connfd
-
7/29/2019 Network Lab Program
7/66
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);}elseprintf("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);}
-
7/29/2019 Network Lab Program
8/66
OUTPUT
Server
[chithambaramani@localhost Net]$ cc TCPCHATSERVER.c -oTCPCHATSERVEROUTPUT[chithambaramani@localhost Net]$ ./TCPCHATSERVEROUTPUT 5666socket successfully created..socket successfully binded...server listening..server accept the clientfrom client:hai how are youto client : fine .. how about youfrom client:ya fineto client : how is your college lifefrom client:ya fineto client : exitserver exit...
Client
[chithambaramani@localhost ~]$ cc TCPCHATCLIENT.c -oTCPCHATCLIENTOUTPUT[chithambaramani@localhost ~]$ ./TCPCHATCLIENTOUTPUT 127.0.0.1 5666socket successfully created..connected to the server...enter the string:hai how are youfrom server :fine .. how about youenter the string:ya fine
from server :how is your college lifeenter the string:ya finefrom server :exitclient exit..
-
7/29/2019 Network Lab Program
9/66
UDP ECHO PROGRAM
UDPECHOSERVER.c
#include#include
#include#include#include#include#includevoid 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 lineargument\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;}}
-
7/29/2019 Network Lab Program
10/66
close(sockfd);}
UDPECHOCLIENT.c
#include#include#include#include#include#include#includevoid 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 commandline argument\n");exit(0);}
sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd==-1){printf("\nSocket creation failed");
exit(0);}elseprintf("\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,(structsockaddr*)&servaddr,&len);printf("\nFrom server:%s\n",buff);if(strncmp("exit",buff,4)==0){printf("Client exit");
-
7/29/2019 Network Lab Program
11/66
break;}
}
close(sockfd);
}
-
7/29/2019 Network Lab Program
12/66
OUTPUT
Server
[chithambaramani@localhost Net]$ cc UDPECHOSERVER.c -oUDPECHOSERVEROUTPUT[chithambaramani@localhost Net]$ ./UDPECHOSERVEROUTPUT 5477Socket successfully createdSocket successfully bindedFrom Client: haiTo Client haiFrom Client: how are uTo Client how are uFrom Client: ya fineTo Client ya fineServer exit
Client
[chithambaramani@localhost ~]$ cc UDPECHOCLIENT.c -oUDPECHOCLIENTOUTPUT[chithambaramani@localhost ~]$ ./UDPECHOCLIENTOUTPUT 127.0.0.1 5477Socket successfully createdEnter the string haiFrom server:haiEnter the string how are uFrom server:how are uEnter the string ya fine
From server:ya fineEnter the string exitClient exit
-
7/29/2019 Network Lab Program
13/66
UDP CHAT PROGRAM
UDPCHATSERVER.c
#include#include
#include#include#include#include#includevoid 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 lineargument\n");exit(0);}sockfd=socket(AF_INET,SOCK_DGRAM,0);if(sockfd==-1){printf("\nSocket creation failed");exit(0);}elseprintf("\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");
-
7/29/2019 Network Lab Program
14/66
break;}}close(sockfd);}
UDPCHATCLIENT.c
#include#include#include#include#include#include#includevoid 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 commandline argument\n");exit(0);}
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==-1){printf("\nSocket creation failed");exit(0);}elseprintf("\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,(structsockaddr*)&servaddr,&len);printf("\nFrom server:%s\n",buff);
-
7/29/2019 Network Lab Program
15/66
if(strncmp("exit",buff,4)==0){printf("Client exit");break;}
}
close(sockfd);
}
-
7/29/2019 Network Lab Program
16/66
OUTPUT
Server
[student@localhost ~]$ cc UDPCHATSERVER.c[student@localhost ~]$ ./a.out 5441
Socket successfully createdSocket successfully bindedFrom Client: hai how are u fine
To Clientya fine how about u
From Client: ya fine ... how is ur college life
To ClientexitServer exit[student@localhost ~]$
Client
[student@localhost ~]$ cc UDPCHATCILENT.c[student@localhost ~]$ ./a.out 127.0.0.1 5441
Socket successfully createdEnter the string hai how are u fine
From server:ya fine how about u
Enter the string ya fine ... how is ur college lifeFrom server:exit
Client exit[student@localhost ~]$
-
7/29/2019 Network Lab Program
17/66
SLIDING WINDOW PROTOCOL
SLIDINGSERVER.C
#include#include
#include#include#include#define SIZE 4int 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
-
7/29/2019 Network Lab Program
18/66
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
-
7/29/2019 Network Lab Program
19/66
if(argc
-
7/29/2019 Network Lab Program
20/66
OUTPUT
Server
[student@localhost ~]$ cc SLI_SERVER.c[student@localhost ~]$ ./a.out 5444
Enter ur choice1.Go-back ARQ2.Selective Repeat ARQ1Enter the text:haihaoeasfdsaTransmitting frames:0|1|2|3Transmission sucessfulTransmitting frames:4|5|6|7Received error in -1
Retransmitting frame:Transmitting frames:8|9|10|11Transmitting frames:12Transmission sucessful[student@localhost ~]$
Client
[student@localhost ~]$ cc SLI_CLIENT.c[student@localhost ~]$ ./a.out 127.0.0.1 5444
haihDo U want to report on error:yes(1)/no(0):0
aoeaDo U want to report on error:yes(1)/no(0):1Enter 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
aDo U want to report on error:yes(1)/no(0):0
Exiting[student@localhost ~]$
-
7/29/2019 Network Lab Program
21/66
FILE TRANSFER PROTOCOL
FTPSERVER.c
# include # include
# include # include # include # include 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 \n");exit(0);}sd=socket(AF_INET,SOCK_STREAM,0);if(sd!=-1)printf("Socket is created\n");elseprintf("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");elseprintf("Not Binded\n");listen(sd,5);cd=accept(sd,(struct sockaddr *)&caddr,&clen);recv(cd,fname,sizeof(fname),0);while(i
-
7/29/2019 Network Lab Program
22/66
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 # include # include # include # include # include 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 \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");elseprintf("Not connected\n");printf("\nEnter the file name : ");scanf("%s",fname);send(sd,fname,sizeof(fname),0);if((fp = fopen(fname, "rb")) == NULL){
-
7/29/2019 Network Lab Program
23/66
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;}
-
7/29/2019 Network Lab Program
24/66
OUTPUT
Server
[student@localhost ~]$ cc FTPSERVER.c[student@localhost ~]$ ./a.out 5411Socket is createdBinded to client
Received file name : UDPECHOSERVER1.cThe file has been transferred
Client
[student@localhost ~]$ cc FTPCLIENT.c[student@localhost ~]$ ./a.out 127.0.0.1 5411
Socket is createdConnected to server
Enter the file name : UDPECHOSERVER.cThe file has been Received
-
7/29/2019 Network Lab Program
25/66
OPEN SHORTEST PATH FIRST
PROGRAM.c
#include
#includeint 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
-
7/29/2019 Network Lab Program
26/66
}}for(i=0;i
-
7/29/2019 Network Lab Program
27/66
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:01:Path taken:12:Path taken:2[student@localhost ~]$
-
7/29/2019 Network Lab Program
28/66
DOMAIN NAME SYSTEM
DNSSERVER.c
#include#include#include#include#include#includevoid 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:");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
-
7/29/2019 Network Lab Program
29/66
if(strcmp(mesg,dns[i])==0){x=0;break;}elsex=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#include#include
#include#include#includevoid 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 ");exit(0);}sid=socket(AF_INET,SOCK_STREAM,0);printf("\nThe value of socket id is %d",sid);if(sid
-
7/29/2019 Network Lab Program
30/66
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
-
7/29/2019 Network Lab Program
31/66
OUTPUT
Server
[student@localhost ~]$ cc DNSERVER.c[student@localhost ~]$ ./a.out 5805
The value of socket id is 3Socket created successfullyBinding successConnection established successfullyDomain Name from client:googleIP 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 3Socket created successfullyConnection successEnter the Domain Name(yahoo,google,rediff) :google
IP Address from server0.0.0.978[student@localhost ~]$
-
7/29/2019 Network Lab Program
32/66
THE HYPERTEXT TRANSFER PROTOCOL
HTTPSERVER.c
#include#include#include#include#include#includeint 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);
-
7/29/2019 Network Lab Program
33/66
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#include#include#include#include
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{
-
7/29/2019 Network Lab Program
34/66
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;}
-
7/29/2019 Network Lab Program
35/66
OUTPUT
Server
[student@localhost ~]$ cc HTTPSERVER.c[student@localhost ~]$ ./a.outEnter noEnter no4441
Port address is 4441Server module-------------
Client accepted
sending File transferred destination[
Client
[student@localhost ~]$ cc HTTPCLIENT.c[student@localhost ~]$ ./a.outEnter port no:4441port address is 4441---------------------------------------------------------Enter the path name of source fileAGENT1.cEnter the path name of destination
HTTPSERVER.cFile received[student@localhost ~]$
SIMPLE NETWORK MANAGEMENT PROTOCOL
-
7/29/2019 Network Lab Program
36/66
AGENT1.c
#include #include #include 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
-
7/29/2019 Network Lab Program
37/66
#include #include #include 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.2006"};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
-
7/29/2019 Network Lab Program
38/66
MANAGER.c
#include #include#includevoid 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
-
7/29/2019 Network Lab Program
39/66
ACCEPTED[student@localhost ~]$ cc AGENT1.c[student@localhost ~]$ ./a.out
I am the Agent 1- Tcp connection
ENTER THE PORT NUMBER5600SOCKET IS CREATED0BINDED
[student@localhost ~]$ ./a.out
I am the agent 2 systemENTER THE PORT NUMBER4444SOCKET IS CREATED0
BINDED
ACCEPTED[student@localhost ~]$
[student@localhost ~]$ cc MANAGER.c[student@localhost ~]$ ./a.out
ENTER THE PORT NUMBER4444
SOCKET IS CREATEDCONNECTED1. TCP connection2. SystemENTER THE NO FOR THE TYPE OF INFORMATION NEEDED2ENTER THE OBJECT ID FOR THE SYSTEMsys1THE MANUFACTURING DATE FOR sys1 IS 01.01.2006THE TIME OF LAST UTI-LIZATION FOR sys1 IS 9.00am[student@localhost ~]$
-
7/29/2019 Network Lab Program
40/66
ADDRESS RESOLUTION PROTOCOL
ARPSERVER.c
#include#include
#include#include#include#includevoid 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:");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
-
7/29/2019 Network Lab Program
41/66
if(strcmp(mesg,ip[i])==0){x=0;break;}elsex=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#include#include#include#include#includevoid 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 ");exit(0);}sid=socket(AF_INET,SOCK_STREAM,0);printf("\nThe value of socket id is %d",sid);if(sid
-
7/29/2019 Network Lab Program
42/66
a.sin_port=htons(atoi(argv[2]));cid=connect(sid,(struct sockaddr*)&a,sizeof(a));if(cid
-
7/29/2019 Network Lab Program
43/66
OUTPUT
Server
[student@localhost ~]$ cc ARPSERVER.c[student@localhost ~]$ ./a.out 5601
The value of socket id is 3Socket created successfullyBinding successConnection established successfullyIP Address from client:172.16.5.57MAC 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 3Socket created successfullyConnection successEnter ur IPAddress:172.16.5.57
Mac Address from server0.0.0.765[student@localhost ~]$
-
7/29/2019 Network Lab Program
44/66
REVERSE ADDRESS RESOLUTION PROTOCOL
RARPSERVER.c
#include#include
#include#include#include#includevoid 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:");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
-
7/29/2019 Network Lab Program
45/66
if(strcmp(mesg,mac[i])==0){x=0;break;}elsex=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#include#include
#include#include#includevoid 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 ");exit(0);}sid=socket(AF_INET,SOCK_STREAM,0);printf("\nThe value of socket id is %d",sid);if(sid
-
7/29/2019 Network Lab Program
46/66
printf("\nError in Connection");else
printf("\nConnection success");m=0;do{m++;printf("\nEnter ur MAC_Address:");scanf("%s",mesg);for(i=0;i
-
7/29/2019 Network Lab Program
47/66
OUTPUT
Server
[student@localhost ~]$ cc RARPSERVER.c[student@localhost ~]$ ./a.out 5701
The value of socket id is 3Socket created successfullyBinding successConnection established successfullyMAC Address from client:0.0.0.765IP 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 3Socket created successfullyConnection successEnter ur MAC_Address:0.0.0.765
IP Address from server172.16.5.57[student@localhost ~]$
-
7/29/2019 Network Lab Program
48/66
STUDY OF NETWORK SIMULATOR PACKAGES NS-2
AIMTo make a descriptive study of the Network Simulator NS-2.
OVERVIEW
Ns is a discrete event simulator targeted at networkingresearch. Ns provides substantial support for simulation of TCP,routing, and multicast protocols over wired and wireless (local andsatellite) networks.
Ns began as a variant of the REAL network simulator in 1989 andhas evolved substantially over the past few years. In 1995 nsdevelopment 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 incollaboration with other researchers including ACIRI. Ns has alwaysincluded substantal contributions from other researchers, includingwireless code from the UCB Daedelus and CMU Monarch projects and SunMicrosystems.
Figure 1. Simplified User's View of NS
As shown in Figure 1, in a simplified user's view, NS is Object-
oriented Tcl (OTcl) script interpreter that has a simulation eventscheduler and network component object libraries, and network setup(plumbing) module libraries (actually, plumbing modules areimplemented as member functions of the base simulator object). Inother words, to use NS, you program in OTcl script language. To setupand run a simulation network, a user should write an OTcl script thatinitiates an event scheduler, sets up the network topology using thenetwork objects and the plumbing functions in the library, and tellstraffic sources when to start and stop transmitting packets throughthe 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 theaddress of an appropriate object. When a user wants to make a newnetwork object, he or she can easily make an object either by writing
-
7/29/2019 Network Lab Program
49/66
a new object or by making a compound object from the object library,and plumb the data path through the object. This may sound likecomplicated job, but the plumbing OTcl modules actually make the jobvery easy. The power of NS comes from this plumbing.
Another major component of NS beside network objects is theevent scheduler. An event in NS is a packet ID that is unique for apacket with scheduled time and the pointer to an object that handlesthe event. In NS, an event scheduler keeps track of simulation timeand fires all the events in the event queue scheduled for the currenttime by invoking appropriate network components, which usually arethe ones who issued the events, and let them do the appropriateaction associated with packet pointed by the event. Networkcomponents communicate with one another passing packets, however thisdoes not consume actual simulation time. All the network componentsthat need to spend some simulation time handling a packet (i.e. needa delay) use the event scheduler by issuing an event for the packet
and waiting for the event to be fired to itself before doing furtheraction handling the packet. For example, a network switch componentthat simulates a switch with 20 microseconds of switching delayissues an event for a packet to be switched to the scheduler as anevent 20 microsecond later. The scheduler after 20 microseconddequeues the event and fires it to the switch component, which thenpasses the packet to an appropriate output link component. Anotheruse of an event scheduler is timer. For example, TCP needs a timer tokeep track of a packet transmission time out for retransmission(transmission of a packet with the same TCP packet number butdifferent NS packet ID). Timers use event schedulers in a similar
manner that delay does. The only difference is that timer measures atime value associated with a packet and does an appropriate actionrelated to that packet after a certain time goes by, and does notsimulate a delay.
NS is written not only in OTcl but in C++ also. For efficiencyreason, NS separates the data path implementation from control pathimplementations. In order to reduce packet and event processing time(not simulation time), the event scheduler and the basic networkcomponent objects in the data path are written and compiled using C+
+. These compiled objects are made available to the OTcl interpreterthrough an OTcl linkage that creates a matching OTcl object for eachof the C++ objects and makes the control functions and theconfigurable variables specified by the C++ object act as memberfunctions and member variables of the corresponding OTcl object. Inthis way, the controls of the C++ objects are given to OTcl. It isalso possible to add member functions and variables to a C++ linkedOTcl object. The objects in C++ that do not need to be controlled ina simulation or internally used by another object do not need to belinked to OTcl. Likewise, an object (not in the data path) can beentirely implemented in OTcl. Figure 2 shows an object hierarchy
example in C++ and OTcl. One thing to note in the figure is that forC++ objects that have an OTcl linkage forming a hierarchy, there is amatching OTcl object hierarchy very similar to that of C++.
-
7/29/2019 Network Lab Program
50/66
Figure 2. C++ and OTcl: The Duality
Figure 3. Architectural View of NS
Figure 3 shows the general architecture of NS. In this figure ageneral user (not an NS developer) can be thought of standing at theleft bottom corner, designing and running simulations in Tcl usingthe simulator objects in the OTcl library. The event schedulers andmost of the network components are implemented in C++ and availableto OTcl through an OTcl linkage that is implemented using tclcl. Thewhole thing together makes NS, which is a OO extended Tcl interpreterwith network simulator libraries.
This section briefly examined the general structure andarchitecture of NS. At this point, one might be wondering about howto obtain NS simulation results. As shown in Figure 1, when asimulation is finished, NS produces one or more text-based outputfiles that contain detailed simulation data, if specified to do so in
the input Tcl (or more specifically, OTcl) script. The data can beused for simulation analysis (two simulation result analysis examplesare presented in later sections) or as an input to a graphicalsimulation display tool called Network Animator (NAM) that isdeveloped as a part of VINT project. NAM has a nice graphical userinterface 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 asthroughput and number of packet drops at each link, although thegraphical information cannot be used for accurate simulationanalysis.
DESIGN
-
7/29/2019 Network Lab Program
51/66
ns was built in C++ and provides a simulation interface throughOTcl, an object-oriented dialect of Tcl. The user describes a networktopology by writing OTcl scripts, and then the main ns programsimulates that topology with specified parameters.
Showing NS split objects model. Object created on OTcl has acorresponding object in C++
ns2 Architecture
-
7/29/2019 Network Lab Program
52/66
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 containingmultiple networked computers, routers and data links, networksimulators are relatively fast and inexpensive. They allow engineersto test scenarios that might be particularly difficult or expensiveto emulate using real hardware- for instance, simulating the effectsof a sudden burst in traffic or a DoS attack on a network service.Networking simulators are particularly useful in allowing designersto test new networking protocols or changes to existing protocols ina controlled and reproducible environment.
Network simulators, as the name suggests are used by researchers
to design various kinds of networks, simulate and then analyze theeffect of various parameters on the network performance. A typicalnetwork simulator like NS2 encompasses a wide range of networkingtechnologies and helps the users to build complex networks from basic
-
7/29/2019 Network Lab Program
53/66
building blocks like variety of nodes and links. With the help ofsimulators one can design hierarchical networks using various typesof nodes like computers, hubs, bridges, routers, optical cross-connects, 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 andthe user can test, analyze various standard results apart fromdevising some novel protocol or strategy for routing etc.There are a wide variety of network simulators, ranging from the verysimple to the very complex. Minimally, a network simulator mustenable a user to represent a network topology, specifying the nodeson the network, the links between those nodes and the traffic betweenthe nodes. More complicated systems may allow the user to specifyeverything about the protocols used to handle network traffic.Graphical applications allow users to easily visualize the workingsof their simulated environment. Text-based applications may provide aless intuitive interface, but may permit more advanced forms ofcustomization. Others, such as GTNets, are programming-oriented,providing a programming framework that the user then customizes tocreate an application that simulates the networking environment to betested.
RESULTThus the descriptive study about the Network Simulator NS2 was
done.
-
7/29/2019 Network Lab Program
54/66
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 fileset nf [open out.nam w]$ns namtrace-all $nf#Define a 'finish' procedureproc finish {} {global ns nf$ns flush-traceclose $nfexec nam out.nam &exit 0}set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 1Mb 10ms DropTailset tcp0 [ new Agent/TCP ]$ns attach-agent $n0 $tcp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $tcp0set 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
-
7/29/2019 Network Lab Program
55/66
OUTPUT:
-
7/29/2019 Network Lab Program
56/66
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 fileset file2 [open out.nam w]$ns namtrace-all $file2#Define a 'finish' procedureproc finish {} {
global ns file2$ns flush-traceclose $file2exec 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 $tcpset 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 connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP#Setup a UDP connectionset udp [new Agent/UDP]$ns attach-agent $n1 $udp
-
7/29/2019 Network Lab Program
57/66
set null [new Agent/Null]$ns attach-agent $n5 $null$ns connect $udp $null$udp set fid_ 2#Setup a CBR over UDP connectionset 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
-
7/29/2019 Network Lab Program
58/66
OUTPUT:
-
7/29/2019 Network Lab Program
59/66
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 fileset file2 [open out.nam w]$ns namtrace-all $file2#Define a 'finish' procedureproc finish {} {
global nsfile2$ns flush-traceclose $file2exec nam out.nam &exit 0
}# Next line should be commented out to have the static routing
$ns rtproto DV#Create six nodesset 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 DropTail
$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail$ns duplex-link $n4 $n5 0.5Mb 10ms 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 connectionset tcp [new Agent/TCP]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n5 $sink$ns connect $tcp $sink$tcp set fid_ 2
#Setup a FTP over TCP connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP
-
7/29/2019 Network Lab Program
60/66
$ns rtmodel-at 1.0 down $n1 $n4$ns rtmodel-at 4.5 up $n1 $n4$ns at 0.1 "$ftp start"$ns at 6.0 "finish"$ns run
OUTPUT:
-
7/29/2019 Network Lab Program
61/66
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 fileset file2 [open out.nam w]$ns namtrace-all $file2
#Define a 'finish' procedureproc finish {} {
global ns file2$ns flush-trace
close $file2exec nam out.nam &exit 0
}
# Next line should be commented out to have the static routing$ns rtproto LS#Create six nodesset 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 DropTail$ns duplex-link $n1 $n2 0.3Mb 10ms DropTail$ns duplex-link $n2 $n3 0.3Mb 10ms DropTail$ns duplex-link $n1 $n4 0.3Mb 10ms DropTail$ns duplex-link $n3 $n5 0.5Mb 10ms DropTail$ns duplex-link $n4 $n5 0.5Mb 10ms 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 connectionset tcp [new Agent/TCP/Newreno]$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink/DelAck]$ns attach-agent $n5 $sink$ns connect $tcp $sink$tcp set fid_ 2
#Setup a FTP over TCP connection
-
7/29/2019 Network Lab Program
62/66
set ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP
$ns rtmodel-at 1.0 down $n1 $n4$ns rtmodel-at 4.5 up $n1 $n4$ns at 0.1 "$ftp start"$ns at 6.0 "finish"$ns run
OUTPUT:
-
7/29/2019 Network Lab Program
63/66
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 fileset file2 [open out.nam w]$ns namtrace-all $file2
#Define a 'finish' procedureproc finish {} {
global ns file2$ns flush-traceclose $file2exec nam out.nam &exit 0
}
# Next line should be commented out to have the static routing$ns rtproto LS#Create six nodesset 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 connectionset udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n5 $null$ns connect $udp $null$udp set fid_ 2#Setup a CBR over UDP connectionset cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000
-
7/29/2019 Network Lab Program
64/66
$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:
-
7/29/2019 Network Lab Program
65/66
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 fileset file2 [open out.nam w]$ns namtrace-all $file2#Define a 'finish' procedureproc finish {} {
global ns file2$ns flush-traceclose $file2exec nam out.nam &exit 0
}
# Next line should be commented out to have the static routing$ns rtproto LS#Create six nodesset 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 connectionset udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n5 $null$ns connect $udp $null$udp set fid_ 2#Setup a CBR over UDP connectionset cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000
-
7/29/2019 Network Lab Program
66/66
$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: