KEMBAR78
Network Programming Tasks | PDF | Internet Standards | Computer Programming
0% found this document useful (0 votes)
155 views50 pages

Network Programming Tasks

The document contains 4 programming problems related to networking and inter-process communication. Part 1 asks to implement link state routing using a given network graph. Part 2 asks to implement encryption and decryption of 8-character messages using the S-DES algorithm. Part 3 asks to write a client-server program where the client sends a file name to the server and the server returns the file contents if present. Part 4 asks to modify the client-server program from part 3 to use FIFOs for inter-process communication instead of sockets.

Uploaded by

Rajesh K M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views50 pages

Network Programming Tasks

The document contains 4 programming problems related to networking and inter-process communication. Part 1 asks to implement link state routing using a given network graph. Part 2 asks to implement encryption and decryption of 8-character messages using the S-DES algorithm. Part 3 asks to write a client-server program where the client sends a file name to the server and the server returns the file contents if present. Part 4 asks to modify the client-server program from part 3 to use FIFOs for inter-process communication instead of sockets.

Uploaded by

Rajesh K M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

PART A:

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;
}
}

printf("sending hello packets \n");


for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(cost[i][j]!=0 && cost[i][j]!=inf)
printf("\n sending hello packets from %d to %d node",i,j);
}

sleep(2);

printf("\nsending echo packets\n");


for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(cost[i][j]!=0 && cost[i][j]!=inf)
printf("sending echo packets from %d to %d node \n",i,j);
}

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]);
}
}

printf("\nenter the source \n");


scanf("%d",&s);
for(i=1;i<=n;i++)
{
min=spath(s,i);
printf("min path from %d to %d =%d\n",s,i,min);
}
}

int spath(int s,int d)


{
struct path
{
int len;
enum{tentative,confirmed}label;
}state[10];

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;

void error(char *msg)


{
perror(msg);
exit(0);
}

int main(int argc,char *argv[])


{
int sockfd,n,sl,ch;
char fname[20];
struct sockaddr_in serv_addr;
struct hostent *server;
char host[20],buffer[256],buf[2000];
int portno;

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
}

printf("\nEnter port no :");


scanf("%d",&portno); //this must be same as servers port number
printf("\nClient:Enter path with filename( file is currently at the server) : ");
scanf("%s",fname);

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);
}

bzero((struct sockaddr_in *)&serv_addr,sizeof(serv_addr));


serv_addr.sin_family=AF_INET;
bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port=htons(portno);

if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)


error("\nerror connecting");
sl=strlen(fname);
n=write(sockfd,fname,strlen(fname));
if(n<0)
error("\nclient:error writing to socket");
bzero(buf,2000);
printf("\nclient:From server following file contents are received :- \n\n\n");

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>

void error(char *msg)


{
perror(msg);
exit(1);
}

int main(int argc,char *argv[])


{
int sockfd,newsockfd,portno,clilen,n,slen,pid;
char buffer[256],c[200];
char fname[20];

struct sockaddr_in serv_addr,cli_addr;


FILE *fptr;
printf("\n*******This is server ");
printf("\nEnter server port number : ");
scanf("%d",&portno);

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);

if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)


perror("\nERROR in binding");
printf("\nNow server is up wating for client");

listen(sockfd,1);
clilen=sizeof(cli_addr);

while(1)
{

newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);

printf("\n New client requested it sockfd = %d",newsockfd);


bzero(fname,20);
close(sockfd);
n=read(newsockfd,fname,20);
printf("\nn Requesting file content %s ",fname);
slen=strlen(fname);
if(n<0)
error("\nERROR reading from socket");
fptr=fopen(fname,"r");
if(fptr==NULL)
{
printf("\nSERVER:file not found");
bzero(buffer,20);
strcpy(buffer,"file not found....");
if(send(newsockfd, buffer, strlen(buffer), 0) == -1)
perror("send");

close(newsockfd);
fclose(fptr);
_exit(0);
}

printf("\nserver : Following information is send back to client :- \n\n\n");

while(!feof(fptr))
{
fgets(buffer,79,fptr);
if(send(newsockfd, buffer, strlen(buffer), 0) == -1)
perror("send");
puts(buffer);
}

printf("\nserver :file contents are transfered");


fclose(fptr);
close(newsockfd);
printf("\nserver :Process is going to terminate\n");
exit(0);
} //while(1)

4.Implement the above program using FIFOs as IPC channels.

// 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

void client(int readfd,int writefd)


{
ssize_t n;
ssize_t len;
char buf[MAXLINE];
printf("Enter the file name:");
fgets(buf,MAXLINE,stdin);
len=strlen(buf);
if(buf[len-1]=='\n')
len--;
printf("\nwrite file name %s into pipe ",buf);
write(writefd,buf,len);
printf("\nreading from pipe the contents of the filename and display it");
while((n=read(readfd,buf,MAXLINE))>0)
write(STDOUT_FILENO,buf,n);
}

int main(int argc,char** argv)


{
int readfd,writefd;
writefd=open(FIFO1,O_WRONLY,0);
readfd=open(FIFO2,O_RDONLY,0);
printf("\ncall client");
client(readfd,writefd);
close(readfd);
close(writefd);
unlink(FIFO1);
unlink(FIFO2);
exit(0);
}

//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

void server(int readfd,int writefd)


{
int fd;
ssize_t n;
char buf[MAXLINE+1];

printf("\nreading from pipe the filename ");

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>

#define BUFLEN 512


#define NPACK 10
#define PORT 9930

void diep(char *s)


{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];

if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)


diep("socket");

memset((char *) &si_me, 0, sizeof(si_me));


si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, &si_me, sizeof(si_me))==-1)
diep("bind");

for (i=0; i<NPACK; i++) {


if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1)
diep("recvfrom()");
printf("Received packet from %s:%d\nData: %s\n\n",
inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
}

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>

#define BUFLEN 512


#define NPACK 10
#define PORT 9930

// #define SRV_IP "999.999.999.999"

#define SRV_IP "127.0.0.1"


/* diep(), #includes and #defines like in the server */
void diep(char *s)
{
perror(s);
exit(1);
}

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

/* Receiver/client multicast Datagram example. */


#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
struct sockaddr_in localSock;
struct ip_mreq group;
int sd;
int datalen;
char databuf[1024];
int main(int argc, char *argv[])
{
/* Create a datagram socket on which to receive. */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if(sd < 0)
{
perror("Opening datagram socket error");
exit(1);
}
else
printf("Opening datagram socket....OK.\n");
/* Enable SO_REUSEADDR to allow multiple instances of this */
/* application to receive copies of the multicast datagrams. */
{
int reuse = 1;
if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
{
perror("Setting SO_REUSEADDR error");
close(sd);
exit(1);
}
else
printf("Setting SO_REUSEADDR...OK.\n");
}
/* Bind to the proper port number with the IP address */
/* specified as INADDR_ANY. */
memset((char *) &localSock, 0, sizeof(localSock));
localSock.sin_family = AF_INET;
localSock.sin_port = htons(4321);
localSock.sin_addr.s_addr = INADDR_ANY;
if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock)))
{
perror("Binding datagram socket error");
close(sd);
exit(1);
}
else
printf("Binding datagram socket...OK.\n");
/* Join the multicast group 226.1.1.1 on the local 203.106.93.94 */
/* interface. Note that this IP_ADD_MEMBERSHIP option must be */
/* called for each local interface over which the multicast */
/* datagrams are to be received. */
group.imr_multiaddr.s_addr = inet_addr("226.1.1.1");
// group.imr_interface.s_addr = inet_addr("127.0.0.1");
group.imr_interface.s_addr = INADDR_ANY; //inet_addr("192.168.1.2");

if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)


{
perror("Adding multicast group error");
close(sd);
exit(1);
}
else
printf("Adding multicast group...OK.\n");
/* Read from the socket. */
datalen = sizeof(databuf);
if(read(sd, databuf, datalen) < 0)
{
perror("Reading datagram message error");
close(sd);
exit(1);
}
else
{
printf("Reading datagram message...OK.\n");
printf("The message from multicast server is: \"%s\"\n", databuf);
}
return 0;
}

7.Write a program to implement sliding window protocol between two hosts.

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;

char str[20], frame[20], temp[20], ack[20],sendwin[20];

char *msg="network programming " ; // lab dept of cse sit tumkur";

struct sockaddr_in saddr, caddr;

port=5000;
printf("Enter the port address");

scanf("%d", &port);

std = socket(AF_INET, SOCK_STREAM, 0);

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));

printf("\n msg= %s ",msg);


printf("\n len = %d ",strlen(msg) );

printf("\n len = %d ",strlen(msg) );

printf("Enter the text:");


//scanf("%s", str);

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);

// next data seq no = incoming ack no

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;

struct sockaddr_in saddr, caddr;

port=5000;

printf("Enter the port address:");


scanf("%d", &port);
std = socket(AF_INET, SOCK_STREAM, 0);
if(std<0)
perror("Error");

// lose the pesky "address already in use" error message


if (setsockopt(std, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
{
perror("setsockopt");
// exit(1);
}
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons(port);

lfd = bind(std, (struct sockaddr *)&saddr, sizeof(saddr));

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");

printf("\n enter the length of data frame: ( 4 ) ");


scanf("%d",&len);

printf("\n enter the message: ( 1 0 1 1 ) ");


for(i=0;i<len;i++)
scanf("%d",&a[i]);

//append r(16) degree zero to msg bits


for(i=0;i<16;i++)
a[len++]=0;

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);
}
}

printf("recived message is=");


for(i=0;i<len;i++)
printf("%2d ",a[i]);
div();
for(i=0;i<len;i++)
{
if(a[i]!=0)
{
printf("\n error in recieved data");
goto END;
}
}
printf(" \n data recieved in error free");
END: printf("\n remender is:");
for(i=(len-16);i<len;i++)
printf("%2d",a[i]);
printf("\n");
}

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

set ns [ new Simulator ]


set tf [ open lab1.tr w ]
$ns trace-all $tf
set nf [ open lab1.nam w ]
$ns namtrace-all $nf

# The below code is used to create the nodes.


set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#This is used to give color to the packets.


$ns color 1 "red"
$ns color 2 "blue"
$n0 label "Source/udp0"
$n1 label "Source/udp1"
$n2 label "Router"
$n3 label "Destination/Null"

#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

set udp1 [new Agent/UDP]


$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1

#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

#The below code is used to set the packet size to 500


$cbr1 set packetSize_ 500Mb

#The below code is used to set the interval of the packets,


#i.e., Data rate of the packets. if the data rate is high
#then packets drops are high.
$cbr1 set interval_ 0.005

proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab1.nam &
close $tf
close $nf
exit 0
}

$ns at 0.1 "$cbr0 start"


$ns at 0.1 "$cbr1 start"
$ns at 10.0 "finish"
$ns run

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

awk -f lab1.awk lab1.tr

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.

set ns [new Simulator]


set tf [open lab2.tr w]
$ns trace-all $tf
set nf [open lab2.nam w]
$ns namtrace-all $nf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

# 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"

$ns duplex-link $n0 $n2 100Mb 1ms DropTail


$ns duplex-link $n1 $n2 100Mb 1ms DropTail
$ns duplex-link $n2 $n3 100Mb 1ms DropTail

# 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.

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3

set udp1 [new Agent/UDP]


$ns attach-agent $n1 $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set null3 [new Agent/Null]
$ns attach-agent $n3 $null3

#The below code is used to set the packet size of ftp and
#udp.
#$ftp0 set packetSize_ 500
#$ftp0 set interval_ 0.001

#The below code is used to increase the data rate(if the


#interval is more then the more number of packets goes to
#destination).
#$cbr1 set packetSize_ 500
#$cbr1 set interval_ 0.001

#This code is used give a color red->tcp and blue ->udp.


$tcp0 set class_ 1
$udp1 set class_ 2

# The below code is used connect the agents.


$ns connect $tcp0 $sink3
$ns connect $udp1 $null3
proc finish { } {
global ns nf tf
$ns flush-trace
exec nam lab2.nam &
close $nf
close $tf
exit 0
}

$ns at 0.1 "$cbr1 start"

$ns at 0.2 "$ftp0 start"

$ns at 5.0 "finish"

$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

awk -f lab2.awk lab2.tr


3.Simulate the different types of Internet traffic such as FTP and TELNET over a network
and analyze the throughput.

set ns [new Simulator]


set tf [open lab3.tr w]
$ns trace-all $tf
set nf [open lab3.nam w]
$ns namtrace-all $nf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#set n4 [$ns node]
#set n5 [$ns node]

#$n0 label "Source/FTP"


#$n1 label "Source/Telnet"
#$n3 label "Destination/FTP"
#$n5 label "Desination/Telnet"
#$ns color 1 "red"
#$ns color 2 "orange"

$ns duplex-link $n0 $n2 100Mb 1ms DropTail


$ns duplex-link $n1 $n2 100Mb 1ms DropTail
$ns duplex-link $n2 $n3 100Mb 1ms DropTail
#$ns duplex-link $n2 $n4 100Mb 1ms DropTail
#$ns duplex-link $n4 $n5 100Mb 1ms DropTail

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
set tcp1 [new Agent/TCP]
$ns attach-agent $n1 $tcp1
set telnet1 [new Application/Telnet]
$telnet1 attach-agent $tcp1
set sink5 [new Agent/TCPSink]
$ns attach-agent $n3 $sink5

#$telnet1 set packetSize_ 1000Mb


#$telnet1 set interval_ 0.00001
#The below code is used to connect the tcp agents & sink.

$ns connect $tcp0 $sink3


$ns connect $tcp1 $sink5

#The below code is used to give a color to tcp and telnet


#packets.
#$tcp0 set class_ 1
#$tcp1 set class_ 2

proc finish { } {
global ns nf tf
exec nam lab3.nam &
close $nf
close $tf
exit 0
}

$ns at 0.1 "$ftp0 start"


$ns at 0.1 "$telnet1 start"
$ns at 15 "finish"
$ns run

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

awk -f lab3.awk lab3.tr

4.Simulate an Ethernet LAN using n nodes (6-10), change error rate and data rate and
compare the throughput.

set ns [new Simulator]


set tf [open lab4a.tr w]
$ns trace-all $tf
set nf [open lab4a.nam w]
$ns namtrace-all $nf

$ns color 1 "red"

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

$n1 label "Source/UDP"


$n3 label "Error Node"
$n5 label "Destination"

#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

#The below code is used to connect node n3 of lan1 and n6 of


#lan2.

$ns duplex-link $n3 $n6 100Mb 10ms DropTail


set udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1
set cbr1 [ new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set null5 [new Agent/Null]
$ns attach-agent $n5 $null5
$ns connect $udp1 $null5

$cbr1 set packetSize_ 1000


$cbr1 set interval_ 0.0001 ;# This is the data rate. Change

# this to increase the rate.


$udp1 set class_ 1

# The below code is used to add an error model between the


#nodes n3 and n6.

set err [new ErrorModel]


$ns lossmodel $err $n3 $n6
$err set rate_ 0.2
# This is the error rate. Change this
#rate to add errors between n3 and n6.

proc finish { } {
global nf ns tf
exec nam lab4a.nam &
close $nf
close $tf
exit 0
}

$ns at 5.0 "finish"


$ns at 0.1 "$cbr1 start"
$ns run
In awk file
BEGIN{
#include <stdio.h>
pkt=0;
time=0
}
{
if($1="r" && $3=="8" && $4=="5")
{
pkt=pkt+$6
time=$2
}
}
END{
printf(" Throughput: %fMbps\n\n",(pkt/time)*(8/1000000));
}

In sh file
ns lab4a.tcl

awk -f lab4a.awk lab4a.tr

5.Simulate an Ethernet LAN using n nodes and set multiple traffic nodes and determine the
collision across different nodes.

set ns [new Simulator]


set tf [open lab5.tr w]
$ns trace-all $tf
set nf [open lab5.nam w]
$ns namtrace-all $nf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]

$ns make-lan -trace on "$n0 $n1 $n2 $n3 $n4" 100mb 10ms LL Queue/DropTail Mac/802_3

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2

$ns connect $tcp0 $sink2

set udp2 [new Agent/UDP]


$ns attach-agent $n2 $udp2
set cbr2 [new Application/Traffic/CBR]
$cbr2 attach-agent $udp2
set null [new Agent/Null]
$ns attach-agent $n1 $null

$ns connect $udp2 $null

set tcp1 [new Agent/TCP]


$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink3 [new Agent/Null
$ns attach-agent $n3 $sink3

$ns connect $tcp1 $sink3


$ftp0 set interval_ 0.001
$cbr2 set interval_ 0.001
$ftp1 set interval_ 0.01

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 at 5.0 "finish"

$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

awk -f lab5.awk lab5.tr

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).

set ns [new Simulator]


set tf [open lab6.tr w]
$ns trace-all $tf
set nf [open lab6.nam w]
$ns namtrace-all $nf

set n0 [$ns node]


set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
set n6 [$ns node]

#$n0 label "Ping0"


#$n4 label "Ping4"
#$n5 label "Ping5"
#$n6 label "Ping6"
##$n2 label "Router"
#$ns color 1 "red"
#$ns color 2 "green"

$ns duplex-link $n0 $n2 100Mb 300ms DropTail


$ns duplex-link $n2 $n6 1Mb 300ms DropTail
$ns duplex-link $n5 $n2 100Mb 300ms DropTail
$ns duplex-link $n2 $n4 1Mb 300ms DropTail
$ns duplex-link $n3 $n2 1Mb 300ms DropTail
$ns duplex-link $n1 $n2 1Mb 300ms DropTail
$ns queue-limit $n0 $n2 5
$ns queue-limit $n2 $n6 2
$ns queue-limit $n2 $n4 3
$ns queue-limit $n5 $n2 5

#The below code is used to connect between the ping agents


#to the node n0, n4 , n5 and n6.

set ping0 [new Agent/Ping]


$ns attach-agent $n0 $ping0

set ping4 [new Agent/Ping]


$ns attach-agent $n4 $ping4

set ping5 [new Agent/Ping]


$ns attach-agent $n5 $ping5

set ping6 [new Agent/Ping]


$ns attach-agent $n6 $ping6

#$ping0 set packetSize_ 50000


#$ping0 set interval_ 0.0001
#$ping5 set packetSize_ 60000
#$ping5 set interval_ 0.00001

#$ping0 set class_ 1


#$ping5 set class_ 2

$ns connect $ping0 $ping4


$ns connect $ping5 $ping6

#The below function is executed when the ping agent receives


#a reply from the destination

Agent/Ping instproc recv {from rtt} {


$self instvar node_
puts " The node [$node_ id] received an reply from $from with round trip time of $rtt"
}

proc finish {} {
global ns nf tf
exec nam lab6.nam &
$ns flush-trace
close $tf
close $nf
exit 0
}

$ns rtmodel-at 0.9 down $n2 $n6


$ns rtmodel-at 1.5 up $n2 $n6
$ns at 0.1 "$ping0 send"
$ns at 0.2 "$ping0 send"
$ns at 0.3 "$ping0 send"
$ns at 0.4 "$ping0 send"
$ns at 0.5 "$ping0 send"
$ns at 0.6 "$ping0 send"
$ns at 0.7 "$ping0 send"
$ns at 0.8 "$ping0 send"
$ns at 0.9 "$ping0 send"
$ns at 1.0 "$ping0 send"
$ns at 1.1 "$ping0 send"
$ns at 1.2 "$ping0 send"
$ns at 1.3 "$ping0 send"
$ns at 1.4 "$ping0 send"
$ns at 1.5 "$ping0 send"
$ns at 1.6 "$ping0 send"
$ns at 1.7 "$ping0 send"
$ns at 1.8 "$ping0 send"
$ns at 0.1 "$ping5 send"
$ns at 0.2 "$ping5 send"
$ns at 0.3 "$ping5 send"
$ns at 0.4 "$ping5 send"
$ns at 0.5 "$ping5 send"
$ns at 0.6 "$ping5 send"
$ns at 0.7 "$ping5 send"
$ns at 0.8 "$ping5 send"
$ns at 0.9 "$ping5 send"
$ns at 1.0 "$ping5 send"
$ns at 1.1 "$ping5 send"
$ns at 1.2 "$ping5 send"
$ns at 1.3 "$ping5 send"
$ns at 1.4 "$ping5 send"
$ns at 1.5 "$ping5 send"
$ns at 1.6 "$ping5 send"
$ns at 1.7 "$ping5 send"
$ns at 1.8 "$ping5 send"
$ns at 5.0 "finish"
$ns run

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

awk -f lab6.awk lab6.tr

7.Simulate simple ESS with transmitting nodes in wire-less LAN and determine the
performance with respect to transmission of packets.

set ns [new Simulator]


# set up for hierarchical routing
$ns node-config -addressType hierarchical

AddrParams set domain_num_ 3 ;# number of domains


lappend cluster_num 2 1 1 ;# number of clusters in each domain
AddrParams set cluster_num_ $cluster_num
lappend llevel 1 1 2 1 ;# number of nodes in each cluster
AddrParams set nodes_num_ $llevel ;# of each domain
set tracefd [open lab7.tr w]
set namtrace [open lab7.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace 250 250

#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

puts "The number of data packets sent "


exec grep "^+" lab7.tr | grep "tcp" | cut -d " " -f 3 | grep -c "0" &
puts "The number of packets received at mobile nodes "
exec grep "^r" lab7.tr | grep "tcp" | grep -c "_4_ AGT" &
exit 0
}

# Create topography object


set topo [new Topography]

# define topology
$topo load_flatgrid 250 250

# create God 3 for HA , FA and MH


create-god 3

# hierarchical addresses
set temp {0.0.0 0.1.0}

#create wired nodes


set W0 [$ns node [lindex $temp 0]]
set W1 [$ns node [lindex $temp 1]]

# Configure for ForeignAgent and HomeAgent nodes


$ns node-config -mobileIP ON \
-adhocRouting DSDV \
-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail/PriQueue \
-ifqLen 50 \
-antType Antenna/OmniAntenna \
-propType Propagation/TwoRayGround \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-topoInstance $topo \
-wiredRouting ON \
-agentTrace ON \
-routerTrace OFF \
-macTrace OFF

# 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

# Position (fixed) for base-station nodes (HA & FA).


$HA set X_ 1.0
$HA set Y_ 2.0
$HA set Z_ 0.0

$FA set X_ 250.0


$FA set Y_ 200.0
$FA set Z_ 0.0

# create a mobilenode that would be moving between HA and FA.


# note address of MH indicates its in the same domain as HA.
$ns node-config -wiredRouting OFF

set MH [$ns node 1.0.1]


set node0 $MH
set HAaddress [AddrParams addr2id [$HA node-addr]]
[$MH set regagent_] set home_agent_ $HAaddress

# movement of the MH
$MH set Z_ 0.0
$MH set Y_ 2.0
$MH set X_ 2.0

# MH starts to move towards FA


$ns at 100.0 "$MH setdest 240.0 210.0 20.0"
# goes back to HA
$ns at 200.0 "$MH setdest 2.0 2.0 20.0"

# create links between wired and BaseStation nodes


$ns duplex-link $W0 $W1 5Mb 2ms DropTail
$ns duplex-link $W1 $HA 5Mb 2ms DropTail
$ns duplex-link $W1 $FA 5Mb 2ms DropTail

$ns duplex-link-op $W0 $W1 orient down


$ns duplex-link-op $W1 $HA orient left-down
$ns duplex-link-op $W1 $FA orient right-down

# setup TCP connections between a wired node and the MobileHost

set tcp1 [new Agent/TCP]


$tcp1 set class_ 2
set sink1 [new Agent/TCPSink]
$ns attach-agent $W0 $tcp1
$ns attach-agent $MH $sink1
$ns connect $tcp1 $sink1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1

$ns initial_node_pos $node0 20

$ns at 100.0 "$ftp1 start"


$ns at 200.0 "$node0 reset"

$ns at 200.0 "$HA reset";


$ns at 200.0 "$FA reset";

$ns at 201.0 "finish"

$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

awk -f lab7.awk lab7.tr

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

$ns node-config -adhocRouting DSDV \


-llType LL \
-macType Mac/802_11 \
-ifqType Queue/DropTail \
-ifqLen 50 \
-phyType Phy/WirelessPhy \
-channelType Channel/WirelessChannel \
-propType Propagation/TwoRayGround \
-antType Antenna/OmniAntenna \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON

#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]

$n0 label "tcp0"


$n1 label "sink1/tcp1"
$n2 label "sink2"

#The below code is used to give the initial node positions.


$n0 set X_ 50
$n0 set Y_ 50
$n0 set Z_ 0
$n1 set X_ 100
$n1 set Y_ 100
$n1 set Z_ 0
$n2 set X_ 600
$n2 set Y_ 600
$n2 set Z_ 0

# the simulation time = 0.1 seconds


# coordinate = 50,50 at a speed of 15 meters per second
# the node n0 will move in this speed towards the coordinate specified

$ns at 0.1 "$n0 setdest 50 50 15"


$ns at 0.1 "$n1 setdest 100 100 25"
$ns at 0.1 "$n2 setdest 600 600 25"

set tcp0 [new Agent/TCP]


$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set sink1 [new Agent/TCPSink]
$ns attach-agent $n1 $sink1

$ns connect $tcp0 $sink1

set tcp1 [new Agent/TCP]


$ns attach-agent $n1 $tcp1
set ftp1 [new Application/FTP]
$ftp1 attach-agent $tcp1
set sink2 [new Agent/TCPSink]
$ns attach-agent $n2 $sink2

$ns connect $tcp1 $sink2

$ns at 5 "$ftp0 start"


$ns at 5 "$ftp1 start"

#The below code is used to provide the node movements.


#node n1 will start moving to 550,550 at speed = 15m/s at simulation time = 100

$ns at 100 "$n1 setdest 550 550 15"


$ns at 190 "$n1 setdest 70 70 15"
proc finish {} {
global ns nf tf
$ns flush-trace
exec nam ess.nam &
close $tf
exit 0
}
$ns at 250 "finish"
$ns run

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

You might also like