PRACTICAL RECORD CS6411 NETWORKS LABORATORY · 2015-02-06 · department of computer science and...
Transcript of PRACTICAL RECORD CS6411 NETWORKS LABORATORY · 2015-02-06 · department of computer science and...
DEPARTMENT OF COMPUTER SCIENCE
AND ENGINEERING
PRACTICAL RECORD
CS6411
NETWORKS LABORATORY
LIST OF EXPERIMENTS
NAME : ______________________
REGISTER NO : ______________________
SEMESTER : ______________________
YEAR : _________________________
TCP SOCKET DATE AND TIME SERVER
PROGRAM
DATE CLIENT #include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define PORT 5000
int main()
{
struct sockaddr_in servaddr,cli;
int n,sockfd;
int len;
char buff[100];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("error in socket");
exit(0);
}
else
{
printf("socket is created");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13);
if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
printf("error in connection failed");
exit(0);
}
else
{
printf("connected succesfully");
}
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 );
}
}
DATE SERVER
#include<stdio.h>
#include<netinet/in.h>
#define port 5000
int main()
{
struct sockaddr_in servaddr;
struct sockaddr_in cli;
int sockfd,connfd;
int len,ch;
char str[100];
time_t tick;
sockfd =socket (AF_INET,SOCK_STREAM,0);
if(socket<0)
{
printf("error in socket\n");
exit(0);
}
else
{
printf("socket is created\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htons(INADDR_ANY);
servaddr.sin_addr.s_addr=htons(13);
if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(cli))<0)
{
printf("error inbinding\n");
}
else
{
printf("binded succesfully");
}
listen(sockfd-50);
for(;;)
{
len=sizeof(ch);
connfd=accept (sockfd,(struct sockaddr*)&cli,&len);
printf("accepted");
tick=ctime(NULL);
snprintf(str,sizeof(str),"%s",ctime(&tick));
}
}
OUTPUT:
SERVER: Socket is created successfully
Binded Sucessfully
Message read 20 Dec 2011 11:50:11
CLIENT: Socket is created
Connected Sucessfully
Message read 20 Dec 2011 11:50:11
TCP ECHO CLIENT AND SERVER PROGRAM
CLIENT #include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#define MAX_LINE (1000)
int main(int argc,char *argv[])
{
int conn_s;
short int port;
struct sockaddr_in servaddr;
char buffer[MAX_LINE];
char *szAddres;
char *szPort;
char *endptr;
printf("\n remote host address: %s \n remote no:%s \n",argv[1],argv[2]);
szAddress=argv[1];
szPort=argv[2];
port=strtol(szPort,&endptr,0);
if(*endptr)
{
printf("ECHOCLNT:invalid port supplied \n");
exit(EXIT_FAILURE);
}
if((conn_s=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("ECHOCLNT:error creating listening socket\n");
exit(EXIT_FAILURE);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(port);
if(inet_aton(szAddress,servaddr.sin_addr)<=0)
{
printf("ECHOCLNT:invalid remote io address\n");
exit(EXIT_FAILURE);
}
if(connect(conn_s,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
printf("ECHOCLNT:error calling connect()\n");
exit(EXIT_FAILURE);
}
printf("enter the string to echo:");
fgets(buffer,MAX_LINE<stdin);
write(conn_s,buffer,strlen(buffer)+1);
strcpy(buffer,"");
read(conn_s,buffer,20);
printf("Echo response:%s\n",buffer);
return EXIT_SUCCESS;
}
SERVER #include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#define MAX_LINE (1000)
int main(int argc,char *argv[])
{
int list_s;
int conn_s;
short int port;
struct sockaddr_in servaddr;
char buffer[MAX_LINE];
char *endptr;
printf("\n enter server port no to listen");
scanf("%d",&port)
if((list_s=socket(AF_INET,SOCK_STREAM,0))<0)
{
fprintf(stderr,"ECHOSERV:error creating listening socket\n");
exit(EXIT_FAILURE);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(port);
if(bind(list_s,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
fprintf(stderr,"ECHOSERV:error calling bind()\n");
exit(EXIT_FAILURE);
}
if(listen(list_s,5)<0)
{
fprintf(stderr,"ECHOSERV:error calling listen()\n");
exit(EXIT_FAILURE);
}
while(1)
{
if((conn_s=accept(list_s,NULL,NULL)<0)
{
fprintf(stderr,"ECHOSERV:error calling accept()\n");
exit(EXIT_FAILURE);
strcpy(buffer," ");
read(conn_s,buffer,20);
printf("\n message received & echoed %s",buffer);
write(conn_s,buffer,strlen(bufffer));
if(close(conn_s)<0)
{
fprintf(stderr,"ECHOSERV:error calling close()\n");
exit(EXIT_FAILURE);
}
}
}
OUTPUT:
SERVER Enter the server port no 3000
Message received: Echo Hello Student
CLIENT:
Enter the Remote Port no 3000
Enter the string :Hello Student
ECHO response : Hello Student
RAW SOCKET USING PING PROGRAM
PROGRAM
CLIENT #include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<netinet/in.h>
#define MAX 80
#define PORT 23490
#define SA struct sockaddr
struct node{
int type;
int code;
int checksum;
int segno;
int id;
int data;
int size;
};
void func(int sockfd){
int i;
for(i=0;i<6;i++){
struct node *p=(struct node*)malloc(sizeof(struct node));
p->type=(int)rand()%1000;
p->code=(int)rand()%1000;
p->checksum=(int)rand()%1000;
p->segno=(int)rand()%1000;
p->id=(int)rand()%1000;
p->data=(int)rand()%1000;
p->size=(int)rand()%1000;
sleep(1);
time_t x,y;
x=time(&x);
write(sockfd,p,sizeof(struct node));
struct node*l=(struct node*)malloc(sizeof(struct node));
read(sockfd,l,sizeof(struct node));
y=time(&y);
float val=difftime(x,y);
printf("recieving %d bytes of data with icm p_seq %d with TTL %0f ms\n",sizeof(struct
node),i,val/10000000);
}
}
int main()
{
int sockfd,connfd;
struct sockaddr_in servaddr,cli;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1){
printf("socket creation failed\n");
exit(0);
}
else
printf("socket successfully created\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
int t=connect(sockfd,(SA*)&servaddr,sizeof(servaddr));
if(t<0){
printf("connection failed\n");
exit(0);
}
else
printf("connected successfully\n");
func(sockfd);
close(sockfd);
return 0;
}
SERVER #include<stdio.h>
#include<malloc.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
#include<math.h>
#include<string.h>
#define MAX 80
#define PORT 23490
#define SA struct sockaddr
struct data{
int type;
int code;
int checksum;
int segno;
int id;
int data;
int size;
};
void func(int sockfd){
int i;
for(i=0;i<6;i++){
struct data*l;int p;
l=(struct data*)malloc(sizeof(struct data*));
read(sockfd,l,sizeof(struct data));
write(sockfd,l,sizeof(struct data));
}
}
int main(){
int sockfd,connfd;
socklen_t len;
struct sockaddr_in servaddr,clien;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1){
printf("socket creation failed\n");
exit(1);
}
else{
printf("socket successfully created\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(PORT);
if(bind(sockfd,(SA*)&servaddr,sizeof(servaddr))!=0){
printf("socket bind failed\n");
exit(0);
}
else
printf("socket successfully Binded\n");
if(listen(sockfd,5)!=0){
printf("listen failed\n");
exit(0);
}
else{
printf("server listening\n");
}
len=sizeof(clien);
connfd=accept(sockfd,(SA*)&clien,&len);
if(connfd<0){
printf("server accept failed\n");
exit(0);
}
else
printf("server accept at client\n");
func(connfd);
close(sockfd);
}
return 0;
}
OUTPUT:
SERVER Socket is successfully Created
Socket successfully Binded
Server Listenin
Server Accepted Client
CLIENT Socket successfully created
Connected Successfully
Receiving 28 bytes of data with ICMP seq 0 with TTL 13 ms
Receiving 28 bytes of data with ICMP seq 1 with TTL 13 ms
Receiving 28 bytes of data with ICMP seq 2 with TTL 13 ms
Receiving 28 bytes of data with ICMP seq 3 with TTL 13 ms
SLIDING WINDOW PROTOCOL PROGRAM
CLIENT #include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#define portno 7337
#define ipaddr "127.0.3.1"
#define MAX 100
#define MAX_SEQ 20
int main(int argc,char*argv[])
{
int clientfd,sqn_no=0,SWS=4;
int last_ack,ackno,intrans=0,flag;
struct sockaddr_in server;
char buffer[MAX];
clientfd=socket(AF_INET,SOCK_STREAM,0);
if(clientfd<0)
{
perror("unable to create socket");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(portno);
if(inet_pton(AF_INET,"127.0.0.1",&(server.sin_addr))<0)
{
perror("error in client");
exit(1);
}
if(connect(clientfd,(struct sockaddr*)&server,sizeof(server))<0)
{
perror("connect error");
close(clientfd);
exit(1);
}
printf("connected\n");
flag=0;
SWS=4;
sqn_no=1;
intrans=0;
last_ack=1;
while(1)
{
while(flag==0&&SWS>(sqn_no-last_ack))
{
printf("\n current sender
window=(%d,%d,%d,%d)",last_ack,(last_ack+1)%20,(last_ack+2)%20,(last_ack+3)%20);
bzero(buffer,sizeof(buffer));
printf(buffer,"%d",sqn_no);
if(write(clientfd,buffer,sizeof(buffer))<0)
{
printf("send error");
close(clientfd);
exit(1);
}
intrans=intrans+1;
printf("\n sequence no%d sent",sqn_no);
printf("\n no of packet in transmit=%d",intrans);
sqn_no++;
if(sqn_no>=20)
{
flag=1;
sqn_no=(sqn_no)%MAX_SEQ;
}
}
sleep(2);
bzero(buffer,sizeof(buffer));
read(clientfd,buffer,sizeof(buffer));
intrans--;
sscanf(buffer,"%d",&ackno);
printf("\n received ACK%d==%d\n",ackno,last_ack);
if(ackno==last_ack)
{
last_ack++;
if(last_ack>=20)
{
flag=0;
last_ack=(last_ack)%MAX_SEQ;
}
}
}
}
SERVER #include<netinet/in.h>
#include<sys/types.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<string.h>
#define MAXLINE 100
#define ipaddr "127.0.0.1"
#define SERV_PORT 7337
void str_chat(int sockfd)
{
ssize_t n;
int sqn_no;
char line[MAXLINE];
for(;;)
{
if(read(sockfd,line,MAXLINE)<=0)
{
printf("\n canot read from socket");
}
write(sockfd,line,MAXLINE);
}
close(sockfd);
}
int main(int argc,char**argv)
{
int listenfd,connfd;
pid_t childpid;
char buff[100];
socklen_t clilen;
struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
printf("socket created\n");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_port=htons(SERV_PORT);
if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0);
printf("\n no bind error");
listen(listenfd,5);
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr*)&cliaddr,&clilen);
printf("\n server connected");
str_chat(connfd);
while(1)
{
bzero(buff,sizeof(buff));
read(connfd,buff,sizeof(buff));
printf("\n packet%sacknowledged",buff);
write(connfd,buff,sizeof(buff));
}
close(connfd);
return;
}
OUTPUT
Current sender window=(1,2,3,4)
Sequence no 1 sent.
No of packet is transmit=1
Current sender window(1,2,3,4)
Sequence no 2 sent
No of packet in transmit =2
Current sender window=(1,2,3,4)
Sequence no 3 sent.
No of packet is transmit=3
Current sender window(1,2,3,4)
Sequence no 4 sent
PERFORMANCE OF MAC PROTOCOLS
#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
OUTPUT
ROUTING PROTOCOL USING OSPF
PROGRAM
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>
int a[5][5],n,i,j;
int main()
{
void getdata();
void shortest();
void display();
printf("\n\n PROGRAM TO FIND SHORTEST PATH BETWEEN TWO NODES\n");
getdata();
shortest();
display();
}
void getdata()
{
printf("\n \n ENTER THE NUMBER HOST IN THE GRAPH\n");
scanf("%d",&n);
printf("\n\n IF THERE IS NO DIRECT PATH\n");
printf("\n \n ASSIGN THE HIGHEST DIATANCE VALUE 1000\n");
for(i=0;i<n;i++)
{
a[i][j]=0;
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("\n\n ENTER THE DISTANCE BETWEEN (%d,%d):",i+1,j+1);
scanf("%d",&a[i][j]);
if(a[i][j]==0)
a[i][j]=1000;
}
}
}
}
void shortest()
{
int i,j,k;
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][k]+a[k][j]<a[i][j])
a[i][j]=a[i][k]+a[k][j];
}
}
void display()
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i!=j)
{
printf("\n SHORTEST PATH IS:(%d,%d)--%d\n",i+j,j+1,a[i][j]);
}
}
OUTPUT
MODEL GRAPH
Program to find the shortest Path between nodes
Enter the number of host in the graph:3
Enter the distance Between(1,2):12
Enter the distance Between(1,3):23
Enter the distance Between(2,1):12
Enter the distance Between(2,3):32
Enter the distance Between(3,1):45
Enter the distance Between(3,2):67
Shortest path of (1,2)=12
Shortest path of (1,3)=12
Shortest path of (2,1)=12
Shortest path of (2,3)=12
Shortest path of (3,1)=12
Shortest path of (3,2)=12
PERFORMANCE ROUTING PROTOCOLS
PROGRAM
#Create a simulator object
set ns [new Simulator]
#Tell the simulator to use dynamic routing
$ns rtproto DV
$ns macType Mac/802_3
#Open the nam trace file
set nf [open distance.nam w]
$ns namtrace-all $nf
#Open the output files
set f0 [open distance.tr w]
$ns trace-all $f0
#Define a 'finish' procedure
proc finish {} {
global ns f0 nf
$ns flush-trace
#Close the trace file
close $f0
close $nf
#Call xgraph to display the results
exec nam distance.nam &
exit 0
}
#Create seven nodes
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}
#Create links between the nodes
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n(3)
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent and the network dynamics
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
OUTPUT
RPC
PROGRAM:
SERVER #include<Sys/socket.h>
#include<Sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#define MAX_LINE (1000)
int main(int args,char *argv[])
{
int list_s;
int conn_s;
int port;
struct sockaddr_in servaddr;
char buffer[MAX_LINE];
char *endptr;
printf(“\nEnter server port no listen”);
scanf(“%d”,&port);
if((list_s=socket(AF_INET,SOCK_STREAM,0))<0)
{fprintf(stderr,”Echoserv:error creating listening socket\n”);
exit(EXIT_FAILURE);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(port);
if(bind(list_s:(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
{fprintf(stderr,”ECHOSERV:error calling bind()\n”);
exit(EXIT_FAILURE);
}
while(1){
if(listen(list_s,5)<0)
{
fprintf(stderr,”ECHOSERV:error calling accept()\n”);
exit(EXIT_FAILURE);
}
strcpy(buffer,” “);
read(conn_s,buffer,20);
system(buffer);
if(close(conn_s)<0){
fprint(stderr,”ECHOSERV:error calling close()\n”);
exit(EXIT_FAILURE);
}}}
OUTPUT:
Enter server port no to listen 3000
ECHOSERV: error calling close ()
CLIENT
PROGRAM:
#include<Sys/socket.h>
#include<Sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX_LINE (1000)
int main(int argc,char *argv[])
{
int conn_s;
short int port;
struct sockaddr_in servaddr;
char buffer[MAX_LINE];
char *szAddress;
char *szPort;
char *endptr;
printf(“\n Remote host address :%s \nRemote port no:%s \n,argv[i],argv[2]);
szAddress=argv[i];
szPort=argv[2];
Port=strtol(szPort,&endptr,0);
if(*endptr)
printf(“ECHOCLIENT:invalid port supplied,\n”);
exit(EXIT_FAILURE);
}
if(conn_s=socket(AF_INET,SOCK_STREAM,0)<0)
{
printf(“ECHOCLIENT:invalid port supplied,\n”);
exit(EXIT_FAILURE);
}
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family= AF_INET;
servaddr.sin_port=htons(port);
if(inet_aton(szAddress,&servaddr.sin_address)<=0)
{
fprintf(“ECHOCLIENT:invalid remote ip address/n”);
exit(EXIT_FAILURE);
}
if(connect(conn_s,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
{
pri ntf(“ECHOCLIENT:error calling connect()/n”);
exit(EXIT_FAILURE);
}
printf(“Enter the command to be executed:”);
fgets(buffer,MAX_LINE,stdin);
write(conn_s,buffer,strlen(buffer)+1);
return sucess; }
OUTPUT:
Remote host address 127.0.0.1
Remote port no 3000
ECHOCLIENT: error calling connect()
STUDY OF TRANSMISSION CONTROL PROTOCOL (TCP)’s
PERFORMANCE USING NS2
AIM:
To study the Performance of TCP using Simulators like NS2
DESCRIPTION:
NS2:
NS or the network simulator (also popularly called ns-2, in reference to its current generation) is
a discrete event network simulator. ns is popularly used in the simulation of routing and multicast
protocols, among others, and is heavily used in ad-hoc networking research. ns supports an array of
popular network protocols, offering simulation results for wired and wireless networks alike. It can
be also used as limited-functionality network emulator. It is popular in academia for its extensibility
(due to its open source model) and plentiful online documentation. NS is licensed for use under
version 2 of the GNU General Public License.
TCP:
The Transmission Control Protocol (TCP) is one of the core protocols of the Internet
Protocol Suite. TCP is one of the two original components of the suite, complementing the Internet
Protocol (IP) and therefore the entire suite is commonly referred to as TCP/IP. TCP provides the
service of exchanging data reliably directly between two network hosts, whereas IP handles
addressing and routing message across one or more networks. In particular, TCP provides reliable,
ordered delivery of a stream of bytes from a program on one computer to another program on
another computer. TCP is the protocol that major Internet applications rely on, such as the World
Wide Web, e-mail, and file transfer. Other applications, that do not require reliable data stream
service, use a sister protocol, the User Datagram Protocol (UDP) which provides a datagram
service, which emphasizes reduced latency over reliability.
ALGORITHM:
Start the program.
Create the topology containing 6 nodes.
Create the links between the nodes.
Establish TCP connection from source to destination.
The type of file to be transferred is defined and mention the schedule events.
The output will be stored in out.tr
Run the simulation.
Stop the process.
PROGRAM:
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the tr trace file
set tf [open out.tr w]
$ns trace-all $tf
#Define a 'finish' procedure
proc finish {}
{
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n4 1Mb 50ms DropTail
$ns duplex-link $n1 $n4 1Mb 50ms DropTail
$ns duplex-link $n2 $n5 1Mb 1ms DropTail
$ns duplex-link $n3 $n5 1Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 50ms DropTail
#Create a TCP agent and attach it to node n0 as source
set source1 [new Agent/TCP]
$ns attach-agent $n0 $source1
#create a destination and attach it to the node n2
set dest [new Agent/TCPSink]
$ns attach-agent $n2 $dest
#Connect the source and the destination
$ns connect $source1 $dest
# Create a CBR traffic source and attach it to n0
set appl [new Application/FTP]
$appl attach-agent $source1
#Schedule events for the CBR agents
$ns at 0.5 "$appl start"
$ns at 3.5 "$appl stop"
#Call the finish procedure to finish the execution of the program
$ns at 4.0 "finish"
#Run the simulation
$ns run
NETWORK ANIMATOR:
Out.tr:
PACKETS DROPPED AND RECEIVED:
[root@linux ~]# ns tcp.tcl
[root@linux ~]# grep "^r" -c out.tr
1194
[root@linux ~]# grep "^d" -c out.tr
0
[root@linux ~]#
CALCULATION:
* Number of packets sent: 1194
* Number of packets received: 1194
* Number of packets lost or dropped: 0
Efficiency = (Number of packets received) / (Number of packets sent – Number of packets
dropped)
= 1194 / (1194-0)
= 100%
* Throughput = (total time taken) x size of the packet
= 4 x 40
= 160
STUDY CASE OF USER DATAGRAM PROTCOL(UDP)’S
PERFORMANCE USING NS_2
AIM:
To study the Performance of UDP using Simulators like NS2
DESCRIPTION:
NS2:
NS or the network simulator (also popularly called ns-2, in reference to its current generation) is
a discrete event network simulator. ns is popularly used in the simulation of routing and multicast
protocols, among others, and is heavily used in ad-hoc networking research. ns supports an array of
popular network protocols, offering simulation results for wired and wireless networks alike. It can
be also used as limited-functionality network emulator. It is popular in academia for its extensibility
(due to its open source model) and plentiful online documentation. NS is licensed for use under
version 2 of the GNU General Public License.
UDP:
The User Datagram Protocol (UDP) is one of the core members of the Internet Protocol
Suite, the set of network protocols used for the Internet. With UDP, computer applications can send
messages, in this case referred to as datagram, to other hosts on an Internet Protocol (IP) network
without requiring prior communications to set up special transmission channels or data paths. The
protocol was designed by David P. Reed in 1980 and formally defined in RFC 768. UDP uses a
simple transmission model without implicit hand-shaking dialogues for guaranteeing reliability,
ordering, or data integrity. Thus, UDP provides an unreliable service and datagram may arrive out
of order, appear duplicated, or go missing without notice. UDP assumes that error checking and
correction is either not necessary or performed in the application, avoiding the overhead of such
processing at the network interface level. Time-sensitive applications often use UDP because
dropping packets is preferable to waiting for delayed packets, which may not be an option in a real-
time system.[1] If error correction facilities are needed at the network interface level, an application
may use the Transmission Control Protocol (TCP) or Stream Control Transmission Protocol
(SCTP) which are designed for this purpose. UDP's stateless nature is also useful for servers
answering small queries from huge numbers of clients. Unlike TCP, UDP is compatible with packet
broadcast (sending to all on local network) and multicasting (send to all subscribers).
ALGORITHM:
Start the program.
Create the topology containing 6 nodes.
Create the links between the nodes.
Establish UDP connection from source to destination.
The type of file to be transferred is defined and mention the schedule events.
The output will be stored in out.tr
Run the simulation.
Stop the process.
PROGRAM:
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the tr trace file
set tf [open out.tr w]
$ns trace-all $tf
#Define a 'finish' procedure
proc finish {}
{
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create six nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
#Create links between the nodes
$ns duplex-link $n0 $n4 1Mb 50ms DropTail
$ns duplex-link $n1 $n4 1Mb 50ms DropTail
$ns duplex-link $n2 $n5 1Mb 1ms DropTail
$ns duplex-link $n3 $n5 1Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 50ms DropTail
#Create a UDP agent and attach it to node n0 as source
set source1 [new Agent/UDP]
$ns attach-agent $n0 $source1
#create a destination and attach it to the node n2
set dest [new Agent/Null]
$ns attach-agent $n2 $dest
#Connect the source and the destination
$ns connect $source1 $dest
# Create a CBR traffic source and attach it to n0
set appl [new Application/Traffic/CBR]
$appl attach-agent $source1
#Schedule events for the CBR agents
$ns at 0.0 "$appl start"
$ns at 4.5 "$appl stop"
#Call the finish procedure to finish the execution of the program
$ns at 5.0 "finish"
#Run the simulation
$ns run
NETWORK ANIMATOR:
Out.tr:
PACKETS DROPPED AND RECEIVED:
[root@linux ~]# ns udp.tcl
[root@linux ~]# grep "^r" -c out.tr
3600
[root@linux ~]# grep "^d" -c out.tr
0
[root@linux ~]#
CALCULATION:
* Number of packets sent: 3600
* Number of packets received: 3600
* Number of packets lost or dropped: 0
Efficiency = 3600 / (3600-0) x 100
= 100%
* Throughput = (total time taken) x size of the packet
= 5 x 210
= 1050 bytes
DOMAIN NAME SERVICE
PROGRAM
SERVER
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<error.h>
#define max 100
#define port 4001
#define ipaddr "127.0.0.1"
typedef struct sockaddr SA;
struct dns
{
char dname[25];
char ipadr[25];
};
int main()
{
int sfd,cfd,len,i,n;
struct dns r;
struct dns
d[]={{"www.annauniv.edu","10.0.0.1"},{"www.yahoomail.com","20.0.0.1"},{"www.rediffmail.
com","30.0.0.1"}};
n=sizeof(d)/50;
struct sockaddr_in server,client;
char buff[max];
sfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(port);
inet_pton(AF_INET,ipaddr,&server.sin_addr);
bind(sfd,(SA*)&server,sizeof(server));
len=sizeof(client);
listen(sfd,2);
cfd=accept(sfd,(SA*)&client,&len);
bzero(&r,sizeof(struct dns));
read(cfd,&r,sizeof(struct dns));
printf("\n domain name=%s",r.dname);
for(i=0;i<n;i++)
{
if(!strcmp(r.dname,d[i].dname))
{
strcpy(r.ipadr,d[i].ipadr);
write(cfd,&r,sizeof(struct dns));
break;
}
}
if(i==n)
{
strcpy(r.ipadr,"error:domain not found");
write(cfd,&r,sizeof(struct dns));
}
close(cfd);
close(sfd);
}
CLIENT
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<error.h>
#define max 100
#define port 4001
#define ipaddr "127.0.0.1"
typedef struct sockaddr SA;
struct dns
{
char dname[25];
char ipadr[25];
};
int main()
{
int cfd;
char buff[max];
struct sockaddr_in server;
struct dns r;
cfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(port);
inet_pton(AF_INET,ipaddr,&server.sin_addr);
connect(cfd,(SA*)&server,sizeof(server));
printf("\n enter the domain name");
scanf("%s",r.dname);
write(cfd,&r,sizeof(struct dns));
bzero(&r,sizeof(struct dns));
read(cfd,&r,sizeof(struct dns));
printf("ipaddr=%s",r.ipadr);
close(cfd);
}
OUTPUT
SERVER
Enter your domain name
www.annauniv.edu
CLIENT:
Enter the domain name
www.annauniv.edu
IP addr :”102:0:0:1”