Professional Documents
Culture Documents
1.For the given network graph, write a program to implement Link state routing algorithm
to build a routing table for the given node.
#include<stdio.h>
#include<stdlib.h>
#define inf 999
int spath(int,int);
int cost[10][10],n;
void main()
{
int i,j,min,s,d;
printf("enter the number of nodes \n");
scanf("%d",&n);
printf("enter the cost");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0 && i!=j)
{
cost[i][j]=inf;
}
}
sleep(2);
sleep(2);
printf("\nconstructing link state protocol");
for(i=1;i<=n;i++)
{
printf("****%d****\n",i);
for(j=1;j<=n;j++)
{
if(cost[i][j]!=0 && cost[i][j]!=inf)
printf("%d-->%d\n",j,cost[i][j]);
}
}
int i,u,j,num=2,min=inf;
for(i=1;i<=n;i++)
{
state[i].label=tentative;
state[i].len=cost[s][i];
}
state[s].label=confirmed;
while(num<=n)
{
min=inf;
for(i=1;i<=n;i++)
{
if(state[i].label==tentative && state[i].len<min)
{
min=state[i].len;
u=i;
}
}
state[u].label=confirmed;
num++;
for(j=1;j<=n;j++)
{
if(state[u].len + cost[u][j]<state[j].len && state[j].label==tentative)
{
state[j].len=state[u].len+cost[u][j];
}
}
}
return state[d].len;
}
2.Write a program to encrypt and decrypt messages using S-DES algorithm. The message
should be of minimum of 8 character word.
#include <stdio.h>
int l[4],r[4],keys[2][8],ct[8];
void sbox(int sip[],int p[],int sbno,int i)
{
int sbox[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
int rw,c,sop;
rw = sip[3]+sip[0]*2;
c = sip[2]+sip[1]*2;
sop = sbox[sbno][rw][c]; //sop gives decimal value of S-Box Output
for(;sop!=0;sop/=2)
p[i--]=sop%2;
}
void cmp_fun(int round)
{
int EP[]={4,1,2,3,2,3,4,1},i,epd[8];
int slip[4],srip[4];
int p[4]={0},p4[]={2,4,3,1},np[4];
for(i=0;i<8;i++) // E/P Permutation
epd[i]=r[EP[i]-1];
for(i=0;i<8;i++)//Performing XOR with Key
if(i<4)
slip[i] = epd[i]^keys[round][i]; // Using Key _ 1=>0
else
srip[i-4] = epd[i]^keys[round][i];
sbox(slip,p,0,1);//Calling SBox 1, 0->SBOX 1
sbox(srip,p,1,3);//Calling SBox 1, 1->SBOX 2
for(i=0;i<4;i++) //P4 permutation
np[i]=p[p4[i]-1];
for(i=0;i<4;i++)
l[i] = l[i]^np[i];
}
void left_shift(int keyip[],int nob)
{
int t1,t2,i;
while(nob>0)
{
t1=keyip[0],t2=keyip[5];
for(i=0;i<9;i++)
if(i<4)
keyip[i] =keyip[i+1];
else if(i>4)
keyip[i] = keyip[i+1];
keyip[4]=t1,keyip[9]=t2;
nob--;
}
}
void gen_keys()
{
int key[10],i,keyip[10];
int p10[]={3,5,2,7,4,10,1,9,8,6},p8[]={6,3,7,4,8,5,10,9};
printf("Enter Key : 10 bit binary key with space : like : 1 0 0 1 1 1 1 1 1 1 ) : \n ");
for(i=0;i<10;i++)
scanf("%d", &key[i]);
for(i=0;i<10;i++) // Permutation P10
keyip[i] = key[p10[i]-1];
left_shift(keyip,1); // Left Shifting (Array,No of bts)
printf("\nKey1 :");
for(i=0;i<8;i++){ //Permuting P8 on key1
keys[0][i] = keyip[p8[i]-1];// Key1 Generated!!
printf("%2d",keys[0][i]);
}
left_shift(keyip,2);// Generating Key2 . .
printf("\nKey2 :");
for(i=0;i<8;i++){
keys[1][i] = keyip[p8[i]-1];// Key2 Generated!!
printf("%2d",keys[1][i]);
}
}
void En_De(int pt[],int c)
{
int ip[]={2,6,3,1,4,8,5,7},ipi[]={4,1,3,5,7,2,8,6},t[8],i;
for(i=0;i<8;i++)// Performing Permutation on input bits!!
if(i<4)
l[i]=pt[ip[i]-1];
else
r[i-4] = pt[ip[i]-1];
cmp_fun(c);//Round 0+1 using key 0+1
for(i=0;i<4;i++) //Swapping left & right
r[i]=l[i]+r[i],l[i]=r[i]-l[i],r[i]=r[i]-l[i];
printf("\n\n");
cmp_fun(!c); // Round 1+1 wid key1+1 wid swapped bits
for(i=0;i<8;i++)
if(i<4) t[i]=l[i];
else t[i]=r[i-4];
for(i=0;i<8;i++)
ct[i] = t[ipi[i]-1];
}
void main()
{
int pt[8]={0},i;
printf("Enter plain text binary bits (8 bits each with space like : 1 0 0 1 1 1 0 0 ) :\n");
for(i=0;i<8;i++)
scanf("%d",&pt[i]);
gen_keys(); // Generating Keys key1 & key2
En_De(pt,0);
printf("\nCipher Text : \n");
for(i=0;i<8;i++)
printf("%2d",ct[i]);
//Decrypting - - -
En_De(ct,1);
printf("\nPlain Text (After Decrypting): \n ");
for(i=0;i<8;i++)
printf("%2d",ct[i]);
}
3.Using TCP/IP sockets, write a client – server program, the client sends the file name
and the server sends back the requested text file if present.
client
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include<unistd.h>
int t=1;
if(ch==0)
strcpy(host,"127.0.0.1"); // use inet address of the server. you can optain it using
else
{
printf("\nEnter the server ip address :");
scanf("%s",host); // "ifconfig" at root
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
error("\nerror opening socket\n");
server=gethostbyname(host);
if(server==NULL)
{
fprintf(stderr,"\nerror,no such host\n");
exit(0);
}
while(1)
{
bzero(buf,2000);
if ((n=recv(sockfd, buf, 100, 0)) == -1)
perror("recv");
if (n==0)
break;
puts(buf);
}
close(sockfd);
exit(0);
server
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include <termios.h>
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
error("\nERROR opening socket");
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);
listen(sockfd,1);
clilen=sizeof(cli_addr);
while(1)
{
close(newsockfd);
fclose(fptr);
_exit(0);
}
while(!feof(fptr))
{
fgets(buffer,79,fptr);
if(send(newsockfd, buffer, strlen(buffer), 0) == -1)
perror("send");
puts(buffer);
}
// fifo client
#include<stdio.h>
#include<signal.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
#define MAXLINE 4096
//fifo server
#include<stdio.h>
#include<signal.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
#define MAXLINE 4096
if((n=read(readfd,buf,MAXLINE))==0)
printf("end of file while reading the pathname");
buf[n]='\0';
printf(" %s ",buf);
if((fd=open(buf,O_RDONLY))<0)
{
//snprintf(buf+n,sizeof(buf)-n,"cant open %s\n",strerror(errno));
strcpy(buf,"SERVER ERROR");
n=strlen(buf);
write(writefd,buf,n);
}
else
{
printf("\nreading from pipe the contents of the file and writing to client");
while((n=read(fd,buf,MAXLINE))>0)
{
write(writefd,buf,n);
write(STDOUT_FILENO,buf,n);
}
close(fd);
}
}
int main(int argc,char** argv)
{
int readfd,writefd;
printf("\ncreating pipe1");
if((mkfifo(FIFO1,FILE_MODE)<0)&&(errno!=EEXIST))
printf("cant create %s\n",FIFO1);
printf("\ncreating pipe2");
if((mkfifo(FIFO2,FILE_MODE)<0)&&(errno!=EEXIST))
{
unlink(FIFO1);
printf("cant create %s\n",FIFO2);
}
readfd=open(FIFO1,O_RDONLY,0);
writefd=open(FIFO2,O_WRONLY,0);
server(readfd,writefd);
exit(0);
}
5.Using UDP, write a client – server program, to exchange messages between client and the
server.
Server:
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
close(s);
return 0;
}
Client
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char host[20];
int ch=0;
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
printf("\n************This is client :");
bzero(host,20);
printf("\nEnter \n0 - local host \n1 - ip address : ");
scanf("%d",&ch);
if(ch==0)
strcpy(host,"127.0.0.1"); // use inet address of the server. you can optain it using
else
{
printf("\nEnter the server ip address :");
scanf("%s",host); // "ifconfig" at root
}
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
// if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
if (inet_aton(host, &si_other.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
for (i=0; i<NPACK; i++) {
printf("Sending packet %d\n", i);
sprintf(buf, "This is packet %d\n", i);
if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
diep("sendto()");
}
close(s);
return 0;
}
6.Write a socket program to demonstrate IP multicasting which provides the capability for
an application to send a single IP datagram that a group of hosts in a network can receive.
Server:
# include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
struct in_addr localInterface;
struct sockaddr_in groupSock;
int sd;
char databuf[1024] = "Multicast test message I am The Best ! ";
int datalen = sizeof(databuf);
int main (int argc, char *argv[ ])
{
/* Create a datagram socket on which to send. */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0)
{
perror("Opening datagram socket error");
exit(1);
}
else
printf("Opening the datagram socket...OK.\n");
/* Initialize the group sockaddr structure with a */
/* group address of 225.1.1.1 and port 5555. */
memset((char *) &groupSock, 0, sizeof(groupSock));
groupSock.sin_family = AF_INET;
groupSock.sin_addr.s_addr = inet_addr("226.1.1.1");
groupSock.sin_port = htons(4321);
/* Set local interface for outbound multicast datagrams. */
/* The IP address specified must be associated with a local, */
/* multicast capable interface. */
// localInterface.s_addr = inet_addr("127.0.0.1");
localInterface.s_addr = INADDR_ANY; // inet_addr("192.168.1.2");
if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface,
sizeof(localInterface)) < 0)
{
perror("Setting local interface error");
exit(1);
}
else
printf("Setting the local interface...OK\n");
/* Send a message to the multicast group specified by the*/
/* groupSock sockaddr structure. */
/*int datalen = 1024;*/
if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0)
{perror("Sending datagram message error");}
else
printf("Sending datagram message...OK\n");
return 0;
}
Client
Sender:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
int std, len, choice, i,j,status,sendsize,port,recvsize,temp1;
port=5000;
printf("Enter the port address");
scanf("%d", &port);
if(std<0)
perror("Error");
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr);
saddr.sin_port = htons(port);
connect(std, (struct sockaddr *)&saddr, sizeof(saddr));
i = 0;
sendsize=5;
while(i<strlen(msg))
{
//sender
memset(frame, 0, 20);
strncpy(frame, msg+i, sendsize);
printf("\n\nSending frame = %s , Sending WINDOW: start seqno= %d - end seqno= %d
",frame ,i , i+sendsize );
send(std, frame, strlen(frame), 0) ;
printf("\n sending data and wait for ack");
memset(ack, 0, 20);
recv(std, ack, 100, 0) ;
sscanf(ack, "%d", &status);
printf("\n recvd ack no = %d ",status);
i=status;
// break;
} /*END OF WHILE*/
write(std, "Exit", sizeof("Exit"));
printf("\nExitting!\n");
close(std);
}
reciever
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define RECVBUF 20
int AdvWindow=0;
main()
{
int std, lfd, len, port, i, j, status, choice;
char str[20], str1[20], err[20],advWindow[1024] , ack_str[20];
int ack;
char frame1[20] ;
char frame[20] ;
int sendsize=5;
char *recv_str;
recv_str=malloc(50);
memset(recv_str, 0, 20);
int yes=1;
port=5000;
if(lfd)
perror("Bind Error");
listen(std, 5);
len = sizeof(&caddr);
lfd = accept(std, (struct sockaddr *)&caddr, &len);
//AdvWindow=rand(20);
len=-1;
i=0;
for(;;)
{
//media
memset(frame, 0, 20);
recv(lfd, frame, 100, 0) ;
if(strcmp(frame, "Exit") == 0)
{
printf("\nExitting!\n");
break;
}
int err=rand()%8;
int i5;
if(err < 4 )
{
memset(frame1, 0, 20);
for(i5=0;i5<err;i5++)
frame1[i5]=frame[i5];
recv_str=(char*) strcat(recv_str,frame1);
frame[err]='x';
printf("\n\nIntroduce error at frame= %d Error at = %d , Error full frame recved = %s --
Retransmit ",err , i+err , frame);
i=i+ err;
ack=i;
// continue;
}
//receiver
else
{
printf("\n\nRecving frame (SUCCUSS ) = %s ,Recving WINDOW: start seqno= %d - end
seqno= %d ",frame, i, i+ sendsize);
recv_str=(char*) strcat(recv_str,frame);
i=i+ sendsize;
ack=i;
}
printf("\n Recver : Sending ACK back to sender ack = %d ", ack);
sprintf(ack_str,"%d",ack);
send(lfd, ack_str, strlen(ack_str), 0) ;
} /*END OF FOR*/
printf("\nReceived Final str at Destination = %s \n ", recv_str);
close(std);
}
8.Write a program for error detecting code using CRC-CCITT (16- bits).
#include<stdio.h>
int a[100],b[100],i,j,len,k,count=0;
//generate polynomial :g(x)=x^16+x^12+x^5+1
int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int flag;
int main()
{
void div();
system("clear");
for(i=0;i<len;i++)
b[i]=a[i];
k=len-16;
div();
for(i=0;i<len;i++)
b[i]=b[i]^a[i];
printf("\n data to be transmitted:");
for(i=0;i<len;i++)
printf("%2d",b[i]);
printf("\nwhether you wish to introduce error(1) or not(0) : ");
scanf("%d",&flag);
if(flag==0)
{ for(i=0;i<len;i++)
a[i]=b[i];
}
else
{ for(i=0;i<len;i++)
{ a[i]=b[i]^(rand()%2);
}
}
void div()
{
for(i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(j=i;j<17+i;j++)
a[j]=a[j]^gp[count++];
}
count=0;
}
}
PART B:
1.Simulate a three nodes point – to – point network with duplex links between them. Set the
queue size and vary the bandwidth and find the number of packets dropped
#Vary the below Bandwidth and see the number of packets dropped.
$ns duplex-link $n0 $n2 10Mb 300ms DropTail
$ns duplex-link $n1 $n2 10Mb 300ms DropTail
$ns duplex-link $n2 $n3 1Mb 300ms DropTail
#The below code is used to set the queue size b/w the nodes
$ns set queue-limit $n0 $n2 10
$ns set queue-limit $n1 $n2 10
$ns set queue-limit $n2 $n3 5
#The below code is used to attach an UDP agent to n0, UDP #agent to n1 and null agent to n3.
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
set null3 [new Agent/Null]
$ns attach-agent $n3 $null3
#The below code sets the udp0 packets to red and udp1
#packets to blue color
$udp0 set class_ 1
$udp1 set class_ 2
#The below code is used to connect the agents.
$ns connect $udp0 $null3
$ns connect $udp1 $null3
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab1.nam &
close $tf
close $nf
exit 0
}
lab1.awk file
BEGIN{
#include<stdio.h>
count=0;
}
{
if($1=="d") #d stands for the packets drops.
count++
#Counts number of rows whose first column is d.
}
END{
printf("The Total no of Packets Dropped due to Congestion : %d\n\n", count)
}
lab1.sh file
ns lab1.tcl
2.Simulate a four node point-to-point network with the links connected as follows: n0 – n2,
n1 – n2 and n2 – n3. Apply TCP agent between n0-n3 and UDP between n1- n3. Apply
relevant applications over TCP and UDP agents changing the parameter and determine the
number of packets sent by TCP / UDP.
# The below code is used to set the color and name's to the
#nodes.
#$ns color 1 "red"
#$ns color 2 "blue"
#$n0 label "source/TCP"
#$n1 label "source/UDP"
#$n2 label "Router"
#$n3 label "destination"
# The below code is used to set the color and labels to the
#links.
#$ns duplex-link-op $n0 $n2 color "green"
#$ns duplex-link-op $n0 $n2 label "from 0-2"
#$ns duplex-link-op $n1 $n2 color "green"
#$ns duplex-link-op $n1 $n2 label "from 1-2"
#$ns duplex-link-op $n2 $n3 color "green"
#$ns duplex-link-op $n2 $n3 label "from 2-3"
# The below code is used create TCP and UDP agents and the
# traffic ftp & cbr respectively.
#The below code is used to set the packet size of ftp and
#udp.
#$ftp0 set packetSize_ 500
#$ftp0 set interval_ 0.001
$ns run
In awk file
BEGIN{
#include<stdio.h>
tcp=0;
udp=0;
}
{
if($1 == "r" && $3 == "2" && $4 == "3" && $5 == "tcp")
tcp++;
if($1 =="r" && $3 == "2" && $4 == "3" && $5 == "cbr")
udp++;
}
END{
printf("\n Total number of packets sent by TCP : %d\n",tcp);
printf("\n Total number of packets sent by UDP : %d\n",udp);
}
In sh file
ns lab2.tcl
proc finish { } {
global ns nf tf
exec nam lab3.nam &
close $nf
close $tf
exit 0
}
In awk file
BEGIN{
#include<stdio.h>
ftppack=0
telpack=0
ftptime=0
teltime=0
}
{
if($1=="r"&&$3=="2"&&$4=="3")
{
ftppack=ftppack+$6;
ftptime=$2;
}
if($1=="r"&&$3=="4"&&$4=="5")
{
telpack=telpack+$6;
teltime=$2;
}
}
END{
printf("Throughput of Ftp:%fMbps",(ftppack/ftptime)*(8/1000000));
printf("Throughput of tel:%fMbps",(telpack/teltime)*(8/1000000));
#printf("Throughput of Ftp:%fMbps",(ftppack/ftptime)*(8/1000000));
#printf("Throughput of tel:%fMbps",(telpack/teltime)*(8/1000000));
}
in sh file
ns lab3.tcl
4.Simulate an Ethernet LAN using n nodes (6-10), change error rate and data rate and
compare the throughput.
#The below code is used to create a two Lans (Lan1 and Lan2).
$ns make-lan "$n0 $n1 $n2 $n3" 10Mb 10ms LL Queue/DropTail Mac/802_3
$ns make-lan "$n4 $n5 $n6" 10Mb 10ms LL Queue/DropTail Mac/802_3
proc finish { } {
global nf ns tf
exec nam lab4a.nam &
close $nf
close $tf
exit 0
}
In sh file
ns lab4a.tcl
5.Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and determine the
collision across different nodes.
$ns make-lan -trace on "$n0 $n1 $n2 $n3 $n4" 100mb 10ms LL Queue/DropTail Mac/802_3
proc finish {} {
global ns nf tf
$ns flush-trace
exec nam lab5.nam &
close $tf
close $nf
exit 0
}
$ns at 0.1 "$cbr2 start"
$ns at 1.2 "$ftp1 start"
$ns at 1.3 "$ftp0 start"
$ns run
Awk file
BEGIN{
#include<stdio.h>
count=0
}
{
if($1=="c") {
printf("The [ %s ] Packet occured collision at the node:[ %s ]\n",$5,$3);
count++;
}
}
END{
printf("\n***************************************\n");
printf("\nThe Total Packet Collision %d", count);
printf("\n***************************************\n");
}
Sh file
ns lab5.tcl
6.Simulate the transmission of ping messages over a network topology consisting of 6 nodes
and find the number of packets dropped due to congestion (can use NS2).
proc finish {} {
global ns nf tf
exec nam lab6.nam &
$ns flush-trace
close $tf
close $nf
exit 0
}
Awk file
BEGIN{
#include<stdio.h>
count=0;
}
{
if($1=="d")
count++
}
END{
printf("The Total no of Packets Dropped due toCongestion:%d ", count)
}
Sh file
ns lab6.tcl
7.Simulate simple ESS with transmitting nodes in wire-less LAN and determine the
performance with respect to transmission of packets.
#Finish Procedure
proc finish {} {
global ns tracefd namtrace
close $tracefd
close $namtrace
exec nam lab7.nam &
# following code is optional if you use awk script to show the throughput lab7.awk
# define topology
$topo load_flatgrid 250 250
# hierarchical addresses
set temp {0.0.0 0.1.0}
# Create HA and FA
set HA [$ns node 1.0.0]
set FA [$ns node 2.0.0]
$HA random-motion 0
$FA random-motion 0
# movement of the MH
$MH set Z_ 0.0
$MH set Y_ 2.0
$MH set X_ 2.0
$ns run
Awk file
BEGIN{
#include<stdio.h>
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1=="r"&&$3=="_4_"&&$4=="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1=="r"&&$3=="_2_"&&$4=="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput at n4 : %fMbps\n",
((pack1*8)/(time1*1000000)) )
printf("The Throughput at n2 : %f Mbps",
((pack2 * 8) / (time2*1000000)) )
}
Sh file
ns lab7.tcl
8.Simulate simple ad-hoc network with transmitting nodes performance with respect to
transmission of packets.
set ns [new Simulator]
set tf [open lab8_adhoc.tr w]
$ns trace-all $tf
set topo [new Topography]
$topo load_flatgrid 1000 1000
set nf [open lab8_adhoc.nam w]
$ns namtrace-all-wireless $nf 1000 1000
#GOD or General Operations Director is a ns-2 simulator object, which is used to store global
information about the state of the environment,
# network, or nodes
create-god 3
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
Awk file
BEGIN{
#include<stdio.h>
count1=0
count2=0
pack1=0
pack2=0
time1=0
time2=0
}
{
if($1=="r"&&$3=="_1_"&&$4=="AGT")
{
count1++
pack1=pack1+$8
time1=$2
}
if($1=="r"&&$3=="_2_"&&$4=="AGT")
{
count2++
pack2=pack2+$8
time2=$2
}
}
END{
printf("The Throughput from n0 to n1: %fMbps\n",
((pack1*8)/(time1*1000000)))
printf("The Throughput from n1 to n2: %f Mbps",
((pack2 * 8) /(time2*1000000)))
}
Sh file
ns lab8_adhoc.tcl
awk -f lab8_adhoc.awk lab8_adhoc.tr