Escolar Documentos
Profissional Documentos
Cultura Documentos
Simulate three nodes point-to-point networks with a duplex link between them. Set the
queue size and vary the bandwidth and find the number of packets dropped.
#Create simulator
set ns [new Simulator]
proc finish {} {
global ns ntrace
$ns flush-trace
close $ntrace
exit 0
}
#create node
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
#create link
$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 10Mb 10ms DropTail
2. Apply TCP agent between n0 to n3 and UDP n1 to n3. Apply relevant applications
over TCP and UDP agents changing the parameters and determine the number of
packets sent by TCP/UDP.
set ns [new Simulator]
$ns run
#Run Simulation
$ns run
#Finish Procedure
proc Finish {} {
global ns ntrace namfile
#Create 6 nodes
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
}
#Setup queue between n(2) and n(3) and monitor the queue
$ns queue-limit $n(2) $n(3) 20
$ns simplex-link-op $n(2) $n(3) queuePos 0.5
#Set error model on link n(2) and n(3) and insert the error model
set loss_module [new ErrorModel]
$loss_module ranvar [new RandomVariable/Uniform]
$loss_module drop-target [new Agent/Null]
$ns lossmodel $loss_module $n(2) $n(3)
#Schedule events
$ns at 0.1 "$cbr0 start"
$ns at 1.0 "$ftp0 start"
$ns at 124.0 "$ftp0 stop"
$ns at 124.5 "$cbr0 stop"
$ns at 125.0 "Finish"
#Run Simulation
$ns run
Output:
#ns prog5.tcl
The throughput of FTP is
72556.097560975613 bytes per second
The throughput of CBR is
37363.34405144694 bytes per second
#Finish Procedure
proc Finish {} {
#Dump all trace data and Close the files
global ns ntrace namfile
$ns flush-trace
close $ntrace
close $namfile
#Create 6 nodes
for {set i 0} {$i<6} {incr i} {
set n($i) [$ns node]
}
#Setup queue between n(2) and n(3) and monitor the queue
$ns queue-limit $n(2) $n(3) 20
$ns duplex-link-op $n(2) $n(3) queuePos 0.5
#Schedule Events
$ns at 0.1 "$ftp0 start"
$ns at 0.1 "PlotWindow $tcp0 $winFile0"
$ns at 0.5 "$ftp1 start"
$ns at 0.5 "PlotWindow $tcp1 $winFile1"
$ns at 25.0 "$ftp0 stop"
$ns at 25.1 "$ftp1 stop"
$ns at 25.2 "Finish"
PART B
1. Write a program for error detecting code using CRC-CCITT (16bit)
#include
char m[50],g[50],r[50],q[50],temp[50];
void caltrans(int);
void crc(int);
void calram();
void shiftl();
int main()
{
int n,i=0;
char ch,flag=0;
printf("Enter the frame bits:");
while((ch=getc(stdin))!='\n')
m[i++]=ch;
n=i;
for(i=0;i<16;i++)
m[n++]='0';
m[n]='\0';
printf("Message after appending 16 zeros:%s",m);
for(i=0;i<=16;i++)
g[i]='0';
g[0]=g[4]=g[11]=g[16]='1';g[17]='\0';
printf("\ngenerator:%s\n",g);
crc(n);
printf("\n\nquotient:%s",q);
caltrans(n);
printf("\ntransmitted frame:%s",m);
printf("\nEnter transmitted freme:");
scanf("\n%s",m);
printf("CRC checking\n");
crc(n);
printf("\n\nlast remainder:%s",r);
for(i=0;i<16;i++)
if(r[i]!='0')
flag=1;
else
continue;
if(flag==1)
printf("Error during transmission");
else
printf("\n\nReceived freme is correct");
}
void crc(int n)
{
int i,j;
for(i=0;i
temp[i]=m[i];
for(i=0;i<16;i++)
r[i]=m[i];
printf("\nintermediate remainder\n");
for(i=0;i
{
if(r[0]=='1')
{
q[i]='1';
calram();
}
else
{
q[i]='0';
shiftl();
}
r[16]=m[17+i];
r[17]='\0';
printf("\nremainder %d:%s",i+1,r);
for(j=0;j<=17;j++)
temp[j]=r[j];
}
q[n-16]='\0';
}
void calram()
{
int i,j;
for(i=1;i<=16;i++)
r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
}
void shiftl()
{
int i;
for(i=1;i<=16;i++)
r[i-1]=r[i];
}
void caltrans(int n)
{
int i,k=0;
for(i=n-16;i
m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
m[i]='\0';
}
struct frame
{
int fslno;
char finfo[20];
};
void sort()
{
int i,j,ex;
struct frame temp;
for(i=0;i
{
ex=0;
for(j=0;j
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
ex++;
}
if(ex==0)
break;
}
}
int main()
{
int i;
system("clear");
printf("enter no of frames\n");
scanf("%d",&n);
printf("enter frame sequence no and frame contents\n");
for(i=0;i
scanf("%d%s",&arr[i].fslno,&arr[i].finfo);
sort();
printf("frames in sequence\n");
for(i=0;i
{
printf("01111110 %d lt %s 01111110\n",arr[i].fslno,arr[i].finfo);
}
}
int no;
struct node
{
int a[nodes][4];
}router[nodes];
void init(int r)
{
int i;
for(i=1;i<=no;i++)
{
router[r].a[i][1]=i;
router[r].a[i][2]=999;
router[r].a[i][3]=nul;
}
router[r].a[r][2]=0;
router[r].a[r][3]=r;
}
void inp(int r)
{
int i;
printf("\nEnter dist from the node %d to other nodes",r);
printf("\nPls enter 999 if there is no direct route\n",r);
for(i=1;i<=no;i++)
{
if(i!=r)
{
printf("\nEnter dist to the node %d:",i);
scanf("%d",&router[r].a[i][2]);
router[r].a[i][3]=i;
}
}
}
void display(int r)
{
int i,j;
printf("\n\nThe routing table for node %d is as follows:",r);
for(i=1;i<=no;i++)
{
if(router[r].a[i][2]>=999)
printf("\n\t\t\t %d \t no link \t no hop",router[r].a[i][1]);
else
printf("\n\t\t\t %d \t %d \t\t d",router[r].a[i][1],router[r].a[i]
[2],router[r].a[i][3]);
}
}
void dv_algo(int r)
{
int i,j,z;
for(i=1;i<=no;i++)
{
if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0)
{
for(j=1;j<=no;j++)
{
z=router[r].a[i][2]+router[i].a[j][2];
if(router[r].a[j][2]>z)
{
router[r].a[j][2]=z;
router[r].a[j][3]=i;
}
}
}
}
}
int main()
{
int i,j,x,y;
char choice;
printf("Enter the no. of nodes required (less than 10 pls):");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
init(i);
inp(i);
}
printf("\nThe configuration of the nodes after initialization is as follows:");
for(i=1;i<=no;i++)
display(i);
for(i=1;i<=no;i++)
dv_algo(i);
printf("\nThe configuration of the nodes after computation of paths is as follows:");
for(i=1;i<=no;i++)
display(i);
while(1)
{
printf("\n\nWanna continue (y/n):");
scanf("%c",&choice);
if(choice=='n')
break;
printf("\nEnter the nodes btn which shortest path is to be found:\n");
scanf("%d %d",&x,&y);
printf("\nThe length of the shortest path is %d",router[x].a[y][2]);
}
}
4. Write a program for spanning tree algorithm to find loop less path
#include
#include
#define MAX 20
#define INFINITY 999
enum boolean {false,true};
void prim(int c[][MAX],int t[MAX],int n);
int mincost=0;
int main()
{
int n,c[MAX][MAX],t[2*(MAX-1)];
int i,j;
printf("this pgm implements prim's algom\n");
printf("how many nodes does the graph have?\n");
scanf("%d",&n);
printf("enter cost adjacency matrix\n");
printf("999 indicates no connection\n");
for(i=0;i
for(j=0;j
scanf("%d",&c[i][j]);
prim(c,t,n);
printf("spanning tree\n");
for(i=0;i<2*(n-1);i+=2)
printf("%d%d\n",t[i]+1,t[i+1]+1);
printf("mincost=%d",mincost);
return 0;
}
void prim(int c[][MAX],int t[MAX],int n)
{
int i,j;
enum boolean v[MAX];
int u,s,min,v1,v2;
for(i=0;i
v[i]=false;
v[0]=true;
u=0;
t[u]=1;
s=0;
u++;
while(u
{
min=INFINITY;
for(i=0;i
for(j=1;j
if(v[i]==true&&v[j]==false&&c[i][j]
{
min=c[i][j];
v1=i;
v2=j;
}
mincost=mincost+min;
if(min==INFINITY)
{
printf("graph disconnected:spanning tree impossible\n");
exit(1);
}
v[v2]=true;
u++;
t[s++]=v1;
t[s++]=v2;
}
}
client send the name of a file and server to send back the contents of
the requested file if present.
Client Side:
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("Err:no port no.\nusage:\n./client portno\n ex:./client 7777\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero((char *)&serv,sizeof(serv));
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("server not responding..\n\n\n\ti am to terminate\n");
exit(1);
}
printf("Enter the file with complete path\n");
scanf("%s",&buffer);
if(write(sockfd,buffer,strlen(buffer))<0)
printf("Err writing to socket..\n");
bzero(c,2000);
printf("Reading..\n..\n");
if(read(sockfd,c,1999)<0)
printf("error: read error\n");
printf("client: display content of %s\n..\n",buffer);
fputs(c,stdout);
printf("\n..\n");
return 0;
}
Server Side:
#include
#include
#include
#include
#include
Server Side:
#include
#include
#include
#define fifo1 "fifo1"
#define fifo2 "fifo2"
int main()
{
char p[100],c[100];
int fd1,fd2,fd,i;
for(i=0;i<100;i++)
c[i]=p[i]='\0';
mknod(fifo1,S_IFIFO|0777,0);
mknod(fifo2,S_IFIFO|0777,0);
printf("server online\n waiting for request...\n");
fd1=open(fifo1,O_RDONLY);
read(fd1,p,100);
if((fd=open(p,O_RDONLY))<0)
{
printf("\n server:file %s not found\n",p);
exit(1);
}
printf("\n server:%s found!\n transfering the content");
read(fd,c,1000);
fd2=open(fifo2,O_WRONLY);
write(fd2,c,strlen(c));
printf("server:transfer completed\n");
}
Client Side:
#include
#include
#include
#define fifo1 "fifo1"
#define fifo2 "fifo2"
int main()
{
char p[100],c[100],ch;
int fd1,fd2,fd,i;
for(i=0;i<100;i++)
c[i]=p[i]='\0';
mknod(fifo1,S_IFIFO|0777,0);
mknod(fifo2,S_IFIFO|0777,0);
printf("waiting for server");
fd1=open(fifo1,O_WRONLY);
int main()
{
unsigned long p,q,e,d,n,z,i,C,M;
int len;
char data[100];
system("clear");
printf("Enter the value of P and Q (such that p*q>255 and p not equal to q)\n");
scanf("%lu%lu",&p,&q);
n=p*q;
z=(p-1)*(q-1);
for(i=1;i
{
if((z%i)==0)
continue;
else
break;
}
e=i;
printf("\nEncryption key is :%lu",e);
for(i=1;i
if(((e*i-1)%z)==0)
break;
d=i;
printf("\ndecryption key is :%lu",d);
printf("\npls enter the message:");
scanf("%s",data);
len=strlen(data);
for(i=0;i
{
M=(unsigned long)data[i];
C=modexp(M,e,n);
printf("\nencrypted value and its char representation:%lu\t%c\n",C,C);
M=modexp(C,d,n);
printf("\ndecrypted value and its char representation:%lu\t%c\n",M,M);
}
return 0;
}
int gmatrix[4][7]={0,1,1,1,0,0,0,
1,0,1,0,1,0,0,
1,1,1,0,0,1,0,
1,1,1,0,0,0,1};
char data[5];
int encode[8];
int con(char x);
int main()
{
int i,j;
system("clear");
printf("Enter the Hamming code\n");
scanf("%s",&data);
for(i=0;i<7;i++)
for(j=0;j<4;j++)
encode[i]=encode[i]+con(data[j])*gmatrix[j][i];
puts(" ENCODE");
for(i=0;i<7;i++)
{
encode[i]=encode[i]%2;
printf("%i",encode[i]);
}
puts(" ");
return 0;
}
int con(char x)
{
if(x == '1')
return 1;
else
return 0;
}
Decoding
#include
#include
#include
int hmatrix[3][7]={1,0,0,0,1,1,1,
0,1,0,1,0,1,1,
0,0,1,1,1,0,1};
char edata[8];
int syndrome[3];
int errdig;
int con(char x)
{
if(x=='1')
return 1;
else
return 0;
}
int main()
{
int i,j,err;
system("system");
printf("\nEnter the encoded bit\n");
scanf("%s",edata);
for(i=0;i<3;i++)
for(j=0;j<7;j++)
syndrome[i]=syndrome[i]+con(edata[j])*hmatrix[i][j];
for(i=0;i<3;i++)
syndrome[i]=syndrome[i]%2;
err=4*syndrome[0]+2*syndrome[1]+1*syndrome[2];
printf("\nhi...%d\n",err);
if(0==err)
printf("\nError free data\n");
else
{ switch(err)
{
case 4:errdig=1;
break;
case 3:errdig=4;
break;
case 1:errdig=3;
break;
default:errdig=err;
}
printf("\nError in bit %d----%c",errdig,edata[errdig]);
errdig--;
if('1'==edata[errdig])
edata[errdig]='0';
else
edata[errdig]='1';
}
printf("\nThe data is:");
for(i=3;i<7;i++)
printf("%c",edata[i]);
puts(" ");
}
struct buff
{
int pno;
struct buff *link;
};
main()
{
front=NULL;
rear=NULL;
int n=0,i,timer=0,npacks=0;
for(i=1;i<=buffersize;i++)
{
n++;
insert(n);
npacks++;
}
while(n<20)
{
n++;
if(timer==5)
{
delete();
npacks--;
if(npacks<=10)
insert(n);
timer=0;
}
else
{
printf("\nPacket %d is discarded..\n",n);
timer++;
sleep(1);
}
}
}
insert(int n)
{
struct buff *temp;
if(front==NULL)
{
printf("\nPacket %d is queued..\n",n);
temp=(struct buff*)malloc(sizeof(struct buff));
temp->pno=n;
temp->link=NULL;
front=temp;
rear=temp;
}
else
{
printf("\npacket %d queued..\n",n);
temp=(struct buff*)malloc(sizeof(struct buff));
temp->pno=n;
temp->link=NULL;
rear->link=temp;
rear=temp;
}
}
delete()
{
struct buff *temp;
if(front==NULL)
printf("\nBuffer is empty..\n");
else
{
temp=front;
printf("\nPacket %d id deleted",temp->pno);
front=front->link;
free(temp);
}
}
Given below is a code to simulate the said scenario with ns3. STA sends a packet to the AP; AP
echoes it back to the base station. The AP is located at position (x, y) = (0, 0). STA is at (xDistance,
0) (all distances in meter). Default value of xDistance is set to 10. [Lines #76, #131]
Increase the value of xDistance in the code and find out the maximum distance upto which
two way communication is possible. This can be verified from the output of the code, which
will show the STA has received reply from the AP (indicated by IP address).
If you like playing with code, you can also try these:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
void
PrintAddresses(Ipv4InterfaceContainer container, std::string header)
{
std::cout << header << std::endl;
uint32_t nNodes = container.GetN ();
for (uint32_t i = 0; i < nNodes; ++i)
{
std::cout << container.GetAddress(i, 0) << std::endl;
}
std::cout << std::endl;
}
int
main (int argc, char *argv[])
{
bool verbose = true;
uint32_t nWifi = 2;
/** Change this parameter and verify the output */
double xDistance = 10.0;
CommandLine cmd;
cmd.AddValue ("xDistance", "Distance between two nodes along x-axis", xDistance);
cmd.Parse (argc,argv);
if (verbose)
{
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
// 1. Create the nodes and hold them in a container
NodeContainer wifiStaNodes,
wifiApNode;
wifiStaNodes.Create (nWifi);
wifiApNode = wifiStaNodes.Get (0);
// 2. Create channel for communication
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
WifiHelper wifi = WifiHelper::Default ();
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
# Define options
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 50 ;# max packet in ifq
set val(nn) 50 ;# number of mobilenodes
$ns run