You are on page 1of 32

Ex.No.

1a Date:

Client Server Application program using TCP Sockets

AIM: To write a program to transfer string between client and server using TCP sockets. ALGORITHM: Step 1: Start the program. Step 2: Include the necessary header files. Step 3: To create the socket using socket() functon. Step 4: The TCP- Transmission Control Protocol is one the core protocols of the internet protocol suite.Using TCP,application s on networked hosts can create connection to one,over which they can exchange data or packets. Step 6: The data from the client is read by a read() and write by data using write(). Step 7:The string is transferred by client to server.if the server is ready its sends the sequence of the requested string to the client. Step 8: Print out with the necessary details. Step 9: Stop the program.

SOURCE CODE Server:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/types.h> int main() { int por; printf("\n Enter the por no:"); scanf("%d",&por); int a,n; int sersock,newsock; char str[25],str2[25]; struct sockaddr_in seraddr,cliinfo; socklen_t csize=sizeof(cliinfo); seraddr.sin_family=AF_INET; seraddr.sin_port=htons(por); seraddr.sin_addr.s_addr=htonl(INADDR_ANY); if((sersock=socket(AF_INET,SOCK_STREAM,0))<0) { perror("\n SOCKET CREATION ERROR"); exit(0); } if(bind(sersock,(struct sockaddr*)&seraddr,sizeof(seraddr))<0) { perror("\n BIND ERROR"); exit(0); } if(listen(sersock,1)<0) { perror("\n LISTEN ERROR"); exit(0); } if((newsock=accept(sersock,(struct sockaddr*)&cliinfo,&csize))<0) {

perror("\n ACCEPT ERROR"); exit(0); } else printf("\n SERVER CONNECTED TO %s\n",inet_ntoa(cliinfo.sin_addr)); read(newsock,str,sizeof(str)); do { printf("\n CLIENT:%s\n",str); printf("\n SERVER:"); scanf("%s",str2); write(newsock,str2,sizeof(str2)); listen(newsock,1); read(newsock,str,sizeof(str)); n=strcmp(str,"bye"); a=strcmp(str2,"bye"); } while(n!=0 || a!=0); close(sersock); close(newsock); return 0; } Client: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> int main(int count,char *arg[]) { int por; printf("\n Enter the port no:"); scanf("%d",&por); int a; int clisock; char str[25],str2[25]; struct sockaddr_in cliaddr;

cliaddr.sin_family=PF_INET; cliaddr.sin_port=htons(por); cliaddr.sin_addr.s_addr=inet_addr(arg[1]); if((clisock=socket(PF_INET,SOCK_STREAM,0))<0) { perror("\n SOCKET CREATION ERROR"); exit(0); } if(connect(clisock,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0) { perror("\n CONNECT ERROR"); exit(0); } printf("\n CLIENT CONNECTED TO %s\n",arg[1]); printf("\n CLIENT:"); scanf("%s",str); if(write(clisock,str,sizeof(str))<0) { printf("\n DATA COULD NOT BE SENT"); } do { listen(clisock,1); read(clisock,str,sizeof(str)); printf("\n SERVER:%s",str); printf("\n CLIENT:"); scanf("%s",&str2); write(clisock,str2,sizeof(str2)); a=strcmp(str2,"bye"); } while(a!=0); close(clisock); return 0; }

OUTPUT: SERVER: [Itlocalhost @networkserver ~]$ cc tcpser.c [Itlocalhost@networkserver ~]$ ./a.out Enter the por no:6789 SERVER CONNECTED TO 197.168.2.225 CLIENT:hai SERVER:hello CLIENT:hru SERVER:fine CLIENT:bye SERVER:bye CLIENT: [Itlocalhost @networkserver ~]$ cc tcpcli.c [Itlocalhost @networkserver ~]$. /a.out 197.168.2.225 Enter the port no:6789 CLIENT CONNECTED TO 197.168.2.225 CLIENT:hai SERVER:hello CLIENT:hru SERVER:fine CLIENT:bye

Ex.No.1b

Display the Date and Time of Server to Client Using TCP Sockets

Aim: To print the day and time in the client side using the socket functions.

Server side programming Algorithm STEP 1: Start the program. STEP 2: Declare the variables and structure for the socket.. STEP 3: The socket is binded at the specified port. STEP 4: Using the object the port and address are declared. STEP 5: The listen and accept functions are executed. STEP 6: If the binding is successful it waits for client request. STEP 7: Execute the client program. Client side programming Algorithm STEP 1: Start the program. STEP 2: Declare the variables and structure. STEP 3: Socket us created and connect function is executed. STEP 4: If the connection is successful then server sends the message. STEP 5: The date and time is printed at the client side. STEP 6: Stop the program.

SERVER PROGRAM

#include<netinet/in.h> #include<sys/socket.h> #include<time.h> main( ) { struct sockaddr_in sa; struct sockaddr_in cli; int sockfd,coontfd; int len,ch,port; printf(Enter the port number);

scanf(%d,&port); char str[100]; time_t time_raw_format; sockfd=socket(AF_INET,SOCK_STREAM,0); if(socket<0) { printf(error in socket\n); exit(0); } else printf(Socket Opened); bzero(7sa,sizeof(sa)); sa.sin_port=htons(port); sa.sin_addr.s_addr=htonl(0); if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) { printf(Error in binding\n); } else printf(Binded Successfully); listen(sockfd,50) for(;;) { len=sizeof(ch); conntfd=accept(sockfd,(struct sockaddr*)&cli,&len); printf(Accepted); time( &time_raw_format ); snprintf(str,sizeof(str),%s,ctime(&time_raw_format)); write(conntfd,str,100); } } CLIENT PROGRAM #include<netinet/in.h> #include<sys/socket.h> main() { struct sockaddr_in sa,cli; int n,sockfd; int len,port; printf(Enter the port number); scanf(%d,&port); char buff[100]; sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0)

{ printf(Error in Socket); exit(0); } else printf(Socket is Opened); bzero(&sa,sizeof(sa)); sa.sin_family=AF_INET; sa.sin_port=htons(); if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) { printf(Error in connection failed); exit(0); } else printf(connected successfully): if(n=read(sockfd,buff,sizeof(buff))<0) { printf(Error in Reading); exit(0); } else { printf(Message Read %s,buff); buff[n]=\0; printf(%s,buff); } }

SERVER SIDE OUTPUT Socket Opened Binded successfully Accepted CLIENT SIDE OUTPUT WITH SERVER TIME Socket is Opened Connected successfully Message Read Mon Jan 22 15:09:19 2007

RESULT: Thus the program for getting the day and time of the server using TCP has been created and verified

Ex.No.1c Date:

Echo client and server Using TCP Sockets

Aim: To Echo the message from server using TCP socket functions. Server side programming Algorithm STEP 1: Start the program. STEP 2: Declare the variables and structure for the socket.. STEP 3: The socket is binded at the specified port. STEP 4: Using the object the port and address are declared. STEP 5: The listen and accept functions are executed. STEP 6: If the binding is successful it waits for client request. STEP 7: Execute the client program. STEP8:Receive the message from client discriptor using read(). STEP9:Display and Echo the received message to the client descriptor using write(). STEP10.Close the all socket descriptors. STEP11.Stop. Client side programming Algorithm STEP 1: Start the program. STEP 2: Declare the variables and structure. STEP 3: Socket us created and connect function is executed. STEP 4: If the connection is successful then Client write the message to the client descriptor STEP 5: Receive the echoed message from server and display that message using read () and putchar (). STEP 6: Stop the program.

Echo Server
#include #include #include #include #include #include #include <stdio.h> <stdlib.h> <sys/types.h> <sys/socket.h> <netinet/in.h> <arpa/inet.h> <unistd.h>

int main(int argc, char **argv) { int serv_fd, clnt_fd; struct sockaddr_in serv_addr; char c;

// create socket serv_fd = socket(AF_INET, SOCK_STREAM, 0); // prepare for bind serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[1])); serv_addr.sin_addr.s_addr = INADDR_ANY; // bind bind(serv_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); // listen only 1 queue listen(serv_fd, 1); // accept only 1 client clnt_fd = accept(serv_fd, NULL, NULL); // read and write until ^D (from client) while (read(clnt_fd, &c, 1)) { // read from client write(clnt_fd, &c, 1); // write to client } // close connection from client and stop echo server close(clnt_fd); close(serv_fd); return 0; }

Echoclient side
#include #include #include #include #include #include #include <stdio.h> <stdlib.h> <sys/types.h> <sys/socket.h> <netinet/in.h> <arpa/inet.h> <unistd.h>

int main(int argc, char **argv) { int clnt_fd; struct sockaddr_in serv_addr; char c; // create socket clnt_fd = socket(AF_INET, SOCK_STREAM, 0); // prepare for connect serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(argv[2])); serv_addr.sin_addr.s_addr = inet_addr(argv[1]); // connect to echo server connect(clnt_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); // write to echo server

while ((c = (char) getchar()) != EOF) { // read from keyboard write(clnt_fd, &c, 1); // write to echo server read(clnt_fd, &c, 1); // read from echo server putchar((int) c); // write to monitor } close(clnt_fd); return 0; }

TCP Echo Server cc ecs.c ./a.out 5622

OUTPUT: APEC CLIENT SIDE OUTPUT WITH SERVER ECHO MESSAGE

cc ecs.c ./a.out 192.168.13.11 5622


OUTPUT APEC APEC RESULT: Thus the program echo server and client was executed using TCP sockets.

Ex.No.2a Date:

Client and Server Chat Using UDP Sockets

UDP Server Algorithm


1.Step by Step algorithm UDP char program 2.Start the Chat program 3.Create a socket using socket() 4.Bind it with a local protocol 5.Receive the message from client using recvfrom() until the message bye; 6.Response t the client by sendto(). 7. Stop the program

UDP Client Algorithm


1.Start the UDP program 2.Create a socket and connect is with the server 3.Pass the message to server using sendto(). 4.Receive the message from server using recvfrom() 5.Chat with the server until the message bye 6.Finally the terminate the client program 7. Stop the program

Source code UDP Server


#include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> int main(int argc,char **argv) { struct sockaddr_in mysock,newsock; int sockfd; char msg[1024]; int size,val; if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) { perror("no socket"); exit(0); } size=sizeof(struct sockaddr); socklen_t len=sizeof(newsock);

bzero(&mysock,size); mysock.sin_family=AF_INET; mysock.sin_port=htons(3000); mysock.sin_addr.s_addr=htonl(INADDR_ANY); if((bind(sockfd,(struct sockaddr*)&mysock,size))<0) { perror("no bind"); exit(0); } if(strcmp(msg,"bye")==0) exit(0); while(strcmp(msg,"bye")!=0) { if((val=recvfrom(sockfd,msg,1024,0,(struct sockaddr*)&newsock,&len))<0) { perror(" "); exit(0); } printf("\n from client"); printf("%s",msg); if(strcmp(msg,"bye")==0) { printf("\nserver"); scanf("%s",msg); exit(0); } printf("\n server"); scanf("%s",msg); if((sendto(sockfd,msg,val,0,(struct sockaddr*)&newsock,len))<0) { perror("\n no send"); exit(0); } } close(sockfd); return 0; }

Source code UDP Client


#include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> int main(int argc,char **argv) { if(argc<2)

{ printf("insuffcient parameters"); exit(0); } struct sockaddr_in servsock; int sockfd,size; char msg[1024],str[1024]; if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) { perror(""); exit(0); } printf("socket created\n"); size=sizeof(struct sockaddr); socklen_t len=sizeof(servsock); bzero(&servsock,size); servsock.sin_port=htons(3000); servsock.sin_family=AF_INET; servsock.sin_addr.s_addr=inet_addr(argv[1]); if(strcmp(msg,"bye")==0) exit(0); while(strcmp(msg,"bye")!=0) { printf("\n client"); scanf("%s",msg); if((sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr*)&servsock,len))<0) { perror("not send"); exit(0); } if(strcmp(msg,"bye")==0) exit(0); printf("\n from server"); if((recvfrom(sockfd,str,1024,0,NULL,NULL))<0) { perror("not received"); exit(0); } printf("%s",str); } close(sockfd); return 0; } Output UDP Server [it28@localhost]$cc sudp.c [it28@localhost] $./a.out From client:It is from client Server: It is from server system

From Client: xxxx Server: yyyy Output UDP client [it28@localhost]$ cc cudp.c [it28@localhost]$ ./a.out 192.168.1.111 Socket created Client:It is from client From server:It is from server Client:xxxx From server:yyyy

Result:
Thus the Client Server chat Program using UDP Sockets was written and executed in C Language with the Linux Platform successfully.

Step by step BGP Algorithm The number of nodes is obtained and stored in variable n The distance between all nodes is accepted in the 4 two dimensional array a It is values are printed back in the form of matrix The shortest pat between any 2 nodes is found using the expression a[i][j]=a[i][k]+a[k][j] The distance between nodes to itself is made 0 The output Matrix is printed back to the user Finally terminate Border Gateway protocol program BGP Source code in C language programming CS1305- NETWORK LAB #include<stdio.h> main() { int i,n,j,k,a[10][10],b[10][10]; printf("ENTER THE NO OF NODES"); scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("ENTER THE DISTANCE BETWEEN HOSTS %d,%d :",i+1,j+1); scanf("%d",&a[i][j]); } } printf("THE GIVEN INPUT\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } for(k=0;k<n;k++) { for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(a[i][j]>a[i][k]+a[k][j]) { a[i][j]=a[i][k]+a[k][j]; } } } }

for(i=0;i<n;i++) { for(j=0;j<n;j++) { b[i][j]=a[i][j]; if(i==j) b[i][j]=0; } } printf("OUTPUT MATRIX \n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%d\t",b[i][j]); } printf("\n"); } } Output CS1305- NETWORK LAB [it28@localhost studentwebsite]$cc protocol.c [it28@localhost studentwebsite]$./a.out Enter the no of nodes: 2 ENTER THE DISTANCE BETWEEN HOSTS 1 1:1 ENTER THE DISTANCE BETWEEN HOSTS 1 2:2 ENTER THE DISTANCE BETWEEN HOSTS 2 1:3 ENTER THE DISTANCE BETWEEN HOSTS 2 2:4 The given output 12 34 Output Matrix 02 30

Ex.No.2b Date:

Simple DNS Client and Server Using UDP Sockets

AIM: To write and execute the simple DNS client server program using UDP sockets. DNS Server algorithm
1. Include the required header files 2.Create a socket between server and client. 3. Bind server to the socket. 4. Listen to the message from the client 5.Accept the message from the client 6.Display the host name 7.Resolve the host name 8.Display the output and also sent it to client.

DNS Client algorithm


1.Include the required header files 2.Define port family 3.Create socket using socket() 4.Connect the client to the socket using connect 5.Enter the host name and send to server 6.The result is received and displayed

Program : UDP DNS Server: #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<unistd.h> #include<string.h> #include<netdb.h> #include<sys/types.h> #include<arpa/inet.h> int main(int argc,char **argv) { int sockfd; char mesg2[100]; int n,flag=0; socklen_t len;

char mesg[100],ip[25],host[100],mesgr[100]; FILE *fp; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(9889); bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); bzero (mesg,100); bzero(ip,25); len=sizeof(servaddr); n=recvfrom(sockfd,mesg,100,0,(struct sockaddr*)&servaddr,&len); printf("Requested host name: %s\n",mesg); fp=fopen("dns.txt","r"); while(!feof(fp) && (flag==0)) { fscanf(fp,"%s%s",ip,host); if(strcmp(host,mesg)==0) flag=1; } sendto(sockfd,ip,sizeof(ip),0,(struct sockaddr*)&servaddr,len); } Program : UDP DNS Client: #include<stdio.h> #include<string.h> #include<sys/socket.h> #include<sys/types.h> #include<arpa/inet.h> #include<netinet/in.h> #include<netdb.h> #include<unistd.h> #define ser_port 8032 int main(int argc, char *argv[]) { int a; int clisock,x,b,c; char str[25],host[25];

char str2[25]; struct sockaddr_in cliaddr; cliaddr.sin_port=htons(ser_port); cliaddr.sin_family=AF_INET; cliaddr.sin_addr.s_addr=inet_addr(argv[1]); clisock=socket(AF_INET,SOCK_STREAM,0); if(clisock<0) { printf("error no socket\n"); exit(0); } if((x=connect (clisock,(struct sockaddr *)&cliaddr,sizeof(cliaddr)))<0) { printf("\nno connect"); exit(0); } printf("enter the domain name:"); scanf("%s",host); write(clisock,host,strlen(host)+1); read(clisock,str2,sizeof(str2)); printf("The requested ip address is:%s\n",str2); close(clisock); return 0; } Contents of dns.txt:
192.168.1.1 gateway 127.0.0.1 localhost 209.85.231.104 www.google.com 93.137.149.56 www.yahoo.com 125.56.222 17 www.rediff.com 209.85.153.85 www.orkut.com 69.63.181.12 www.facebook.com 208.73.210.28 www.backtracklinux.org

OUTPUT: SERVER:
[it28@localhost]$ cc sdns.c [it28@localhost]$ . /a.out

Requested host name: www.google.com CLIENT:


[it28@localhost]$ cc cdns.c [it28@localhost]$ . /a.out 192.168.13.11

Enter the host name: www.google.com Requesed IP address: 209.85.231.104

RESULT:

Thus the simple DNS client server Program was written and executed using UDP sockets in C Language with Linux Platform.

Simulation of Sliding Window Protocol


#include"stdio.h" #include"conio.h" #include"stdlib.h" void getdata() { FILE *f1; char c; clrscr(); printf("\n\t\tEnter the data\n\t\t"); f1=fopen("sc.txt", "w"); while((c=getchar())!='\n') putc(c, f1); putc('\n', f1); fclose(f1); }

void send() { FILE *f1, *f2; char c; int i, k=0, windowsize=2, randno, k1; f1=fopen("sc.txt", "r"); f2=fopen("ms.txt", "w"); loop: for(i=0; i < windowsize;i++) { c=getc(f1); if(c!='\n') { printf("%c\n", c); putc(c, f2); k++; } else goto end; } fclose(f1); randomize(); randno=random(k);//generates a random number f1=fopen("sc.txt", "a"); putc(randno, f1); fclose(f1);

k1=randno; printf("The Frames of data sent %d\n", k); getch(); f1=fopen("sc.txt", "r"); fseek(f1, k, SEEK_SET); goto loop; end: fclose(f1); fclose(f2); printf("The Frames of data sent %d\n", k); } void disp() { int choice; printf("\n\tChoose an option\n"); printf("\n\t1.Send"); printf("\n\t2.Quit"); scanf("%d", &choice); if(choice==1) send(); } void main() { clrscr(); getdata(); disp(); getch(); }

Implementation of Packet capturing using raw socket in udp

#include "unistd.h" #include "stdio.h" #include "sys/socket.h" #include "netinet/ip.h" #include "netinet/udp.h" #define PCKT_LEN 8192 // The IP header's structure struct ipheader { unsigned char iph_ihl:5, iph_ver:4; unsigned char iph_tos; unsigned short int iph_len; unsigned short int iph_ident; unsigned char iph_flag; unsigned short int iph_offset; unsigned char iph_ttl; unsigned char iph_protocol; unsigned short int iph_chksum; unsigned int iph_sourceip; unsigned int iph_destip; }; // UDP header's structure struct udpheader { unsigned short int udph_srcport; unsigned short int udph_destport; unsigned short int udph_len; unsigned short int udph_chksum; }; // Function for checksum calculation. unsigned short csum(unsigned short *buf, int nwords) { // unsigned long sum; for(sum=0; nwords>0; nwords--) sum += *buf++; sum = (sum >> 16) + (sum &0xffff); sum += (sum >> 16); return (unsigned short)(~sum); } int main(int argc, char *argv[]) { int sd;

char buffer[PCKT_LEN]; // Our own headers' structures struct ipheader *ip = (struct ipheader *) buffer; struct udpheader *udp = (struct udpheader *) (buffer + sizeof(struct ipheader)); // Source and destination addresses: IP and port struct sockaddr_in sin, din; int one = 1; const int *val = &one; memset(buffer, 0, PCKT_LEN); // Create a raw socket with UDP protocol sd = socket(PF_INET, SOCK_RAW, IPPROTO_UDP); if(sd < sin_family =" AF_INET;" sin_family =" AF_INET;" sin_port =" htons(atoi(argv[2]));" sin_port =" htons(atoi(argv[4]));" s_addr =" inet_addr(argv[1]);" s_addr =" inet_addr(argv[3]);">iph_ihl = 5; ip->iph_ver = 4; ip->iph_tos = 16; ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader); ip->iph_ident = htons(54321); ip->iph_ttl = 64; // hops ip->iph_protocol = 17; // UDP ip->iph_sourceip = inet_addr(argv[1]);//Source IP address ip->iph_destip = inet_addr(argv[3]);// destination IP address // Fabricate the UDP header. udp->udph_srcport = htons(atoi(argv[2]));//source port udp->udph_destport = htons(atoi(argv[4]));//destination port udp->udph_len = htons(sizeof(struct udpheader)); // Calculate the checksum ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader)); if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < count =" 1;">iph_len, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("sendto() error"); exit(-1); } else { printf("Count #%u - sendto() is OK.\n", count); sleep(2); } } close(sd); return 0; }

IMPLEMENTATION OF AODV ROUTING PROTOCOL USING NS2

Aim: To Simulate the AODV Routing protocol in wireless environment using NS2.31 with Linux Platform.

Algorithm: 1. 2. 3. 4. 5. 6. Start the program Set the values for each parameter in the wireless environment Start the network simulator Open the trace corresponding trace file Create the god with the value of nn Cinfigure the each node in the environment with the values and size of x and y directions. 7. Set the time. 8. Assign the label for sending node and receiving node. 9. Run the ns 10. Excute the nam window 11. Stop the nam window.

Program:
set set set set set set set set set set set set set set set set set val(chan) val(prop) val(netif) val(mac) val(ifq) val(ll) val(ant) val(ifqlen) val(nn) val(rp) val(x) val(y) val(stop) Channel/WirelessChannel ; Propagation/TwoRayGround ; Phy/WirelessPhy ; Mac/802_11 ; Queue/DropTail/PriQueue ; LL ; Antenna/OmniAntenna ; 50 ; 12 ; AODV ; 500 ; 400 ; 150;

ns [new Simulator] tracefd [open aodv12.tr w] windowVsTime2 [open winao.tr w] namtrace [open simao.nam w]

$ns trace-all $tracefd $ns use-newtrace $ns namtrace-all-wireless $namtrace $val(x) $val(y)

set topo [new Topography] $topo load_flatgrid $val(x) $val(y) create-god $val(nn) set topo [new Topography] $topo load_flatgrid $val(x) $val(y) create-god $val(nn) $ns node-config -adhocRouting $val(rp) \ -llType $val(ll) \ -macType $val(mac) \ -ifqType $val(ifq) \ -ifqLen $val(ifqlen) \ -antType $val(ant) \ -propType $val(prop) \ -phyType $val(netif) \ -channelType $val(chan) \ -topoInstance $topo \ -agentTrace ON \ -routerTrace ON \ -macTrace OFF \ -movementTrace ON \

for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns node] } $node_(0) set X_ 0.0 $node_(0) set Y_ 0.0 $node_(0) set Z_ 0.0 $node_(1) set X_ 0.0 $node_(1) set Y_ 200.0 $node_(1) set Z_ 0.0 $node_(2) set X_ 0.0 $node_(2) set Y_ 400.0 $node_(2) set Z_ 0.0 $node_(3) set X_ 20.0 $node_(3) set Y_ 490.0 $node_(3) set Z_ 0.0 $node_(4) set X_ 100.0 $node_(4) set Y_ 490.0 $node_(4) set Z_ 0.0 $node_(5) set X_ 200.0 $node_(5) set Y_ 490.0 $node_(5) set Z_ 0.0 $node_(6) set X_ 300.0 $node_(6) set Y_ 490.0 $node_(6) set Z_ 0.0

$node_(7) set X_ 400.0 $node_(7) set Y_ 490.0 $node_(7) set Z_ 0.0 $node_(8) set X_ 490.0 $node_(8) set Y_ 490.0 $node_(8) set Z_ 0.0 $node_(9) set X_ 490.0 $node_(9) set Y_ 300.0 $node_(9) set Z_ 0.0 $node_(10) set X_ 490.0 $node_(10) set Y_ 200.0 $node_(10) set Z_ 0.0 $node_(11) set X_ 490.0 $node_(11) set Y_ 40.0 $node_(11) set Z_ 0.0 $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns $ns at at at at at at at at at at at at 10.0 "$node_(0) setdest 80.0 80.0 3.0" 25.0 "$node_(1) setdest 120.0 120.0 5.0" 50.0 "$node_(2) setdest 160.0 160.0 5.0" 100.0 "$node_(3) setdest 200.0 200.0 5.0" 40.0 "$node_(4) setdest 240.0 240.0 3.0" 45.0 "$node_(5) setdest 280.0 280.0 5.0" 80.0 "$node_(6) setdest 320.0 320.0 5.0" 10.0 "$node_(7) setdest 360.0 360.0 5.0" 120.0 "$node_(8) setdest 400.0 300.0 3.0" 115.0 "$node_(9) setdest 340.0 340.0 5.0" 110.0 "$node_(10) setdest 40.0 40.0 5.0" 110.0 "$node_(11) setdest 480.0 45.0 5.0"

set tcp [new Agent/TCP/Newreno] $tcp set class_ 2 set sink [new Agent/TCPSink] $ns attach-agent $node_(0) $tcp $ns attach-agent $node_(11) $sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at 10.0 "$ftp start" $ns at 0.0 "$node_(11) label \"Sending Node\"" set tcp1 [new Agent/TCP/Newreno] $tcp1 set class_ 2 set sink1 [new Agent/TCPSink] $ns attach-agent $node_(5) $tcp1 $ns attach-agent $node_(9) $sink1 $ns connect $tcp1 $sink1 set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 $ns at 10.0 "$ftp start" $ns at 0.0 "$node_(9) label \"receiving Node\""

proc plotWindow {tcpSource file} { global ns set time 0.01 set now [$ns now] set cwnd [$tcpSource set cwnd_] puts $file "$now $cwnd" $ns at [expr $now+$time] "plotWindow $tcpSource $file"} $ns at 10.1 "plotWindow $tcp $windowVsTime2" for {set i 0} {$i < $val(nn)} {incr i} { $ns initial_node_pos $node_($i) 30 } for {set i 0} {$i <$val(nn)} {incr i} { $ns at $val(stop) "$node_($i) reset"; } $ns at $val(stop) "$ns nam-end-wireless $val(stop)" $ns at $val(stop) "stop" $ns at 150.01 "puts\"end simulation\"; $ns halt" proc stop {} { global ns tracefd namtrace $ns flush-trace exec nam simao.nam & exit 0 close $tracefd close $namtrace } $ns run

Out Put: ns aodv12.tcl

Simulation of Data flow among the wired nodes using NS2 with Linux Platform
Aim: To Simulate the Data flow among the wired nodes using NS2.31 with Linux Platform.

Algorithm: 1. 2. 3. 4. 5. Start the program Set the values for each parameter in the wired environment Start the network simulator Open the trace corresponding trace file Configure the position for each node in the environment with the values and size of x and y directions. 6. Set the time. 7. Assign the label for sending node and receiving node. 8. Run the ns 9. Excute the nam window 10. Stop the nam window. Program: #Create a simulator object 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 the NAM trace file close $nf #Execute NAM on the trace file exec nam out.nam & exit 0 }

#Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10 #Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right #Monitor the queue for link (n2-n3). (for NAM) $ns duplex-link-op $n2 $n3 queuePos 0.5

#Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1 #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 $n3 $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_ 1mb $cbr set random_ false

#Schedule events for the CBR and FTP agents $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop" #Detach tcp and sink agents (not really necessary) $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" #Call the finish procedure after 5 seconds of simulation time $ns at 5.0 "finish" #Print CBR packet size and interval puts "CBR packet size = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]" #Run the simulation $ns run Out Put: ns ns-simple.tcl

You might also like