0% found this document useful (0 votes)
71 views

Cs2307 - Network Lab: Simple Socket Call Program //server Socket Program

This document contains code for a domain name server program and its client. The server code creates a UDP socket and binds it to port 9889 to receive domain name lookups. It uses gethostbyname() to resolve domain names to IP addresses. The client sends domain name requests to the server socket and the server responds with the IP address. This allows translation of domain names to IP addresses as done by actual DNS servers on the Internet.

Uploaded by

DivyaJagan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Cs2307 - Network Lab: Simple Socket Call Program //server Socket Program

This document contains code for a domain name server program and its client. The server code creates a UDP socket and binds it to port 9889 to receive domain name lookups. It uses gethostbyname() to resolve domain names to IP addresses. The client sends domain name requests to the server socket and the server responds with the IP address. This allows translation of domain names to IP addresses as done by actual DNS servers on the Internet.

Uploaded by

DivyaJagan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

CS2307 NETWORK LAB

SIMPLE SOCKET CALL PROGRAM


//Server socket program
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/un.h>
#define st 3
#define addr "127.0.0.1"
char *strs[st]=
{"This is 1st string from server\n",
"This is 2nd string from server\n",
"This is 3rd string from server\n"};
main()
{
char c;
FILE *fp;
int frmlen,i,s,ns,len;
struct sockaddr_un saun,fsaun;
if((s=socket(AF_UNIX,SOCK_STREAM,0))<0)
{
perror("Server :Socket");
exit(0);
}
saun.sun_family=AF_UNIX;
strcpy(saun.sun_path,addr);
unlink(addr);
len=sizeof(saun.sun_family)+strlen(saun.sun_path);
if(bind(s,&saun,len)<0)
{
perror("Server :Bind");
exit(1);
}
if(listen(s,5)<0)
{
perror("Server : Listen");
exit(1);

}
if((ns=accept(s,&fsaun,&frmlen))<0)
{

perror("Server : Accept");
exit(1);

}
fp=fdopen(ns,"r");
for(i=0;i<st;i++)
{
send(ns,strs[i],strlen(strs[i]),0);
}
for(i=0;i<st;i++)
{
while((c=fgetc(fp))!=EOF)
{
putchar(c);
if(c=='\n')
break;
}
}
close(s);
exit(0);

#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/un.h>
#define st 3
#define addr "127.0.0.1"
char *strs[st]=
{"This is 1st string from client\n",
"This is 2nd string from client\n",
"This is 3rd string from client\n"};
main()
{
char c;
FILE *fp;
int i,s,ns,len;
struct sockaddr_un saun;
if((s=socket(AF_UNIX,SOCK_STREAM,0))<0)
{
perror("Client :Socket");
exit(0);
}
saun.sun_family=AF_UNIX;
strcpy(saun.sun_path,addr);

len=sizeof(saun.sun_family)+strlen(saun.sun_path);
if(connect(s,&saun,len)<0)
{
perror("Client:Bind");
exit(1);
}
fp=fdopen(s,"r");
for(i=0;i<st;i++)
{
while((c=fgetc(fp))!=EOF)
{
putchar(c);
if(c=='\n')
break;
}
}
for(i=0;i<st;i++)
send(s,strs[i],strlen(strs[i]),0);
close(s);
exit(0);

OUTPUT :
[s6itb10@Linuxserver s6itb10]$ cc serverpgm.c
serverpgm.c: In function `main':
serverpgm.c:27: warning: passing arg 2 of `bind' from incompatible pointer type
serverpgm.c:37: warning: passing arg 2 of `accept' from incompatible pointer type
[s6itb10@Linuxserver s6itb10]$ ./a.out
This is 1st string from client
This is 2nd string from client
This is 3rd string from client
[s6itb10@Linuxserver s6itb10]$
[s6itb10@Linuxserver s6itb10]$ cc clientpgm.c
clientpgm.c: In function `main':
clientpgm.c:26: warning: passing arg 2 of `connect' from incompatible pointer type
[s6itb10@Linuxserver s6itb10]$ ./a.out
This is 1st string from server
This is 2nd string from server
This is 3rd string from server
[s6itb10@Linuxserver s6itb10]$

DATE AND TIME SERVER


//Program to display date and time
SERVER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<sys/un.h>
#define MAXLINE 4096
#define LISTENQ 6524
#define SA struct sockaddr
#include "time.h"
int main()
{
int listenfd,connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE];
time_t ticks;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(5000);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
listen(listenfd,LISTENQ);
for(;;)
{
connfd=accept(listenfd,(SA*)NULL,NULL);
ticks=time(NULL);
snprintf(buff,sizeof(buff),"%.24s\r\n",ctime(&ticks));
write(connfd,buff,strlen(buff));
close(connfd);
}
}
CLIENT :
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>

#include<stdlib.h>
#include<netinet/in.h>
#include<sys/un.h>
#include<sys/errno.h>
#define MAXLINE 4096
#define BUFFSIZE 8192
#define SA struct sockaddr
int main(int argc,char **argv)
{
int sockfd,n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;
if(argc!=2)
printf("Usage : ./a.out<>");
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("Socket Error");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(5000);
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0)
printf("inet_pton err for %s",argv[1]);
if(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))<0)
printf("connect error");
while((n=read(sockfd,recvline,MAXLINE))>0)
{
recvline[n]=0;
if(fputs(recvline,stdout)==EOF)
printf("fputs error");
}
if(n<0)
printf("read error");
}

SERVER:
[s6itb10@Linuxserver s6itb10]$ cc dtserver.c
[s6itb10@Linuxserver s6itb10]$ ./a.out
[s6itb10@Linuxserver s6itb10]$
CLIENT:
[s6itb10@Linuxserver s6itb10]$ cc dtclient.c
[s6itb10@Linuxserver s6itb10]$ ./a.out 127.0.0.1
Sat Jan 8 11:35:18 2011
[s6itb10@Linuxserver s6itb10]$

CHAT SERVER (TCP)


//Program to perform chat server
SERVER :
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#define tcpport 7999
#define ipaddr "127.0.0.1"
#define max 50
int main()
{
int serfd,clntfd;
int length;
struct sockaddr_in server,client;
char buffer[max];
serfd=socket(AF_INET,SOCK_STREAM,0);
if(serfd==1)
{
perror("\nSOCKET CREATION FAILED");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(tcpport);
inet_pton(AF_INET,ipaddr,&(server.sin_addr));
if(bind(serfd,(struct sockaddr*)&server,sizeof(struct sockaddr))>0)
{
perror("\nBIND ERROR");
close(serfd);
exit(1);
}
if(listen(serfd,1)<0)
{
perror("\nLISTEN error");
close(serfd);
exit(1);
}
printf("listen success\n");
length=sizeof(client);

clntfd=accept(serfd,(struct sockaddr*)&client,&length);
if(clntfd==-1)
{
perror("ACCEPT FAILED");
exit(1);
}
printf("\nCLIENT CONNECTED");
while(1)
{
bzero(buffer,sizeof(buffer));
if(read(clntfd,buffer,sizeof(buffer),0)<0)
{
perror("\nRead failed");
close(serfd);
exit(1);
}
printf("\n\nReceived message from client is :: %s",buffer);
printf("\nMy reply ::");
fgets(buffer,max-1,stdin);
if(write(clntfd,buffer,sizeof(buffer),0)<=0)
{
perror("server send error");
close(serfd);
exit(1);
}
}

CLIENT :
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdlib.h>
#define tcpport 7999
#define ipaddr "127.0.0.1"
#define max 50
int main()
{
int clntfd;
int length;
struct sockaddr_in server,client;
char buffer[max];

clntfd=socket(AF_INET,SOCK_STREAM,0);
if(clntfd<0)
{
perror("\nSOCKET CREATION FAILED");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(tcpport);
if(inet_pton(AF_INET,ipaddr,&(server.sin_addr))<0)
{
perror("\nERROR");
close(clntfd);
exit(1);
}
if(connect(clntfd,(struct sockaddr*)&server,sizeof(server))<0)
{
perror("\nCONNECT FAILED");
close(clntfd);
exit(1);
}
printf("\nConnected");
while(1)
{
bzero(buffer,sizeof(buffer));
printf("\nEnter your message :");
fgets(buffer,max-1,stdin);
if(write(clntfd,buffer,sizeof(buffer),0)<0)
{
perror("Client send error");
close(clntfd);
exit(1);
}
if(read(clntfd,buffer,sizeof(buffer),0)<=0)
{
perror("\nClient receive error");
close(clntfd);
exit(1);
}
printf("\nReply from server is :: %s",buffer);
}

SERVER:
[s6itb10@Linuxserver s6itb10]$ cc chatserver.c
[s6itb10@Linuxserver s6itb10]$ ./a.out
listen success
CLIENT CONNECTED
Received message from client is :: hi
My reply :: hi
Received message from client is :: How are u?
My reply :: Fine
Received message from client is :: quit
My reply :: bye
[3]+ Stopped
./a.out
[s6itb10@Linuxserver s6itb10]$
CLIENT:
[s6itb10@Linuxserver s6itb10]$ cc chatclient.c
[s6itb10@Linuxserver s6itb10]$ ./a.out
Connected
Enter your message : hi
Reply from server :: hi
Enter your message : How r u?
Reply from server :: fine
Enter your message : quit
Reply from server :: bye
[3]+ Stopped
./a.out
[s6itb10@Linuxserver s6itb10]$

ECHO SERVER
//Program to implement echo server
SERVER:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main(int argc,char **argv)
{
int serv_fd,clnt_fd;
struct sockaddr_in serv_addr;
char c;
//Create socket
serv_fd=socket(AF_INET,SOCK_STREAM,0);
//Prepare for bind
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(atoi(argv[1]));
serv_addr.sin_addr.s_addr=INADDR_ANY;
//bind
bind(serv_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
//listen only 1 queue
listen(serv_fd,1);
//accept onlu 1 client
clnt_fd=accept(serv_fd,NULL,NULL);
//read and write until ^D(from client)
while(read(clnt_fd,&c,1))
{
write(clnt_fd,&c,1);
}
close(clnt_fd);
close(serv_fd);
return 0;
}
CLIENT:
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>

#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
int main(int argc,char **argv)
{
int clnt_fd;
struct sockaddr_in serv_addr;
char c;
//Create socket
clnt_fd=socket(AF_INET,SOCK_STREAM,0);
//Prepare for bind
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(atoi(argv[2]));
serv_addr.sin_addr.s_addr=inet_addr(argv[1]);;
//connect to echo server
connect(clnt_fd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
//write to echo server
while((c=(char)getchar())!=EOF)
{
write(clnt_fd,&c,1);
read(clnt_fd,&c,1);
putchar((int)c);
}
close(clnt_fd);
return 0;
}

SERVER:
[s6itb10@Linuxserver s6itb10]$ cc echoserver.c
[s6itb10@Linuxserver s6itb10]$ ./a.out 3457
[s6itb10@Linuxserver s6itb10]$
CLIENT:
[s6itb10@Linuxserver s6itb10]$ cc echoclient.c
[s6itb10@Linuxserver s6itb10]$ ./a.out 127.0.0.1 3457
client
client
hello
hello
[s6itb10@Linuxserver s6itb10]$

DOMAIN NAME SERVER


//Domain Name System
SERVER:
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<arpa/inet.h>
int main(int argc,char **argv)
{
int sockfd;
char mesg2[100];
int n,flag=0;
socklen_t len;
char mesg[100],ip[25],host[100],mesg1[100];
FILE *fp;
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(9889);
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
bzero(mesg,100);
bzero(ip,25);
len=sizeof(servaddr);
n=recvfrom(sockfd,mesg,100,0,(struct sockaddr*)&servaddr,&len);
printf("Requested host name : %s\n",mesg);
fp=fopen("dns.txt","r");
while(!feof(fp)&&(flag==0))
{
fscanf(fp,"%s%s",ip,host);
if(strcmp(host,mesg)==0)
flag=1;
}
sendto(sockfd,ip,sizeof(ip),0,(struct sockaddr*)&servaddr,len);
}
CLIENT:

#include<stdio.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<sys/types.h>
#include<arpa/inet.h>
int main(int argc,char **argv)
{
int sockfd,n;
char sendline[100],recvline[100];
struct sockaddr_in servaddr;
if(argc!=2)
{
printf("Usage :udpcli");
exit(0);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(9889);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
sockfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
printf("Enter the host name:");
scanf("%s",sendline);
sendto(sockfd,sendline,strlen(sendline),0,(struct
sockaddr*)&servaddr,sizeof(servaddr));
n = recvfrom(sockfd,recvline,100,0,(struct sockaddr*)&servaddr,sizeof(servaddr));
printf("Requested IP address : %s\n",recvline);
}

dns.txt
127.0.0.8
126.48.0.6
128.0.45.82
132.0.89.34
167.0.35.78
134.56.9.56

www.google.com
www.yahoo.com
www.rediffmail.com
www.gmail.com
www.ibm.com
www.yahoomail.com

OUTPUT:
SERVER:
[s6itb10@Linuxserver s6itb10]$ cc dnsserver.c
[s6itb10@Linuxserver s6itb10]$ ./a.out
Requested host name : www.ibm.com
[s6itb10@Linuxserver s6itb10]$
CLIENT:
[s6itb10@Linuxserver s6itb10]$ cc dnsclient.c
dnsclient.c: In function `main':
dnsclient.c:27: warning: passing arg 6 of `recvfrom' makes pointer from integer without a
cast
[s6itb10@Linuxserver s6itb10]$ ./a.out dns.txt
Enter the host name:www.ibm.com
Requested IP address : 167.0.35.78
[s6itb10@Linuxserver s6itb10]$

CHAT SERVER (UDP)


//Chat server using UDP sockets
SERVER:
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int sock;
int addr_len,bytes_read;
char recv_data[1024];
struct sockaddr_in server_addr,client_addr;
if((sock=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("Socket");
exit(1);
}
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(5000);
server_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if(bind(sock,(struct sockaddr*)&server_addr,sizeof(struct sockaddr))==-1)
{
perror("Bind");
exit(1);
}
addr_len=sizeof(struct sockaddr);
printf("\nUDPServer waiting for client on port 5000");
fflush(stdout);
while(1)
{
bytes_read=recvfrom(sock,recv_data,1024,0,(struct
sockaddr*)&client_addr,&addr_len);
recv_data[bytes_read]='\0';
printf("\n(%s , %d) said:
",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
printf("%s",recv_data);
fflush(stdout);

}
return 0;

CLIENT:
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<netdb.h>
int main()
{
int sock;
char send_data[1024];
struct sockaddr_in server_addr;
struct hostent *host;
host=(struct hostent *)gethostbyname((char*)"127.0.0.1");
if((sock=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
perror("Socket");
exit(1);
}
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(5000);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
while(1)
{
printf("Type something(q or Q to quit):");
gets(send_data);
if((strcmp(send_data,"q")==0)||strcmp(send_data,"Q")==0)
break;
else
sendto(sock,send_data,strlen(send_data),0,(struct
sockaddr*)&server_addr,sizeof(struct sockaddr));
}
}

OUTPUT :
SERVER:
[s6itb10@Linuxserver s6itb10]$ cc serversocket.c
[s6itb10@Linuxserver s6itb10]$ ./a.out
UDPServer waiting for client on port 5000
(127.0.0.1 , 32775) said: hi
(127.0.0.1 , 32775) said: How r you?
[s6itb10@Linuxserver s6itb10]$
CLIENT:
[s6itb10@Linuxserver s6itb10]$ cc clientsocket.c
/tmp/ccSuYNxo.o(.text+0xc2): In function `main':
: the `gets' function is dangerous and should not be used.
[s6itb10@Linuxserver s6itb10]$ ./a.out
Type something(q or Q to quit):hi
Type something(q or Q to quit):How r you?
Type something(q or Q to quit):q
[s6itb10@Linuxserver s6itb10]$

SIMULATION OF SLIDING WINDOW PROTOCOL


//Simulation of Sliding Window Protocol
SERVER:
#include<stdio.h>
#include<sys/types.h>
#include<socket.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/time.h>
#include<sys/select.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define MAX 20
#define n 4
int main(int argc,char*argv[])
{
int sockfd,clifd,addrlen;
char buf[MAX];
struct sockaddr_in myaddr;
int sp,cp,ep,inputbuf[MAX],ACK=-1;
sp=0;
cp=sp;
ep=sp+n;
if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
fprintf(stderr,"socket error");
memset(&myaddr,0,sizeof(&myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(atoi(argv[1]));
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
addrlen=sizeof(myaddr);
if(bind(sockfd,(struct sockaddr*)&myaddr,addrlen)<0)
fprintf(stderr,"bind error");
if(listen(sockfd,1)<0)
fprintf(stderr,"listen error");
if((clifd=accept(sockfd,(struct sockaddr*)&myaddr,&addrlen))<0)
fprintf(stderr,"accept error");
while(cp<MAX)
{
while((cp>=sp)&&(cp<=ep))
{
if(read(clifd,buf,sizeof(buf))>0)

inputbuf[cp++]=atoi(buf);
printf("%s received",buf);

}
}
if(cp>0)
{
sprintf(buf,"%d",cp-1);
if(write(clifd,buf,sizeof(buf))!=sizeof(buf))
fprintf(stderr,"write error");
else
printf("sending ack %s\n",buf);
sp=cp;
ep=sp+n;
}

}
return 0;

CLIENT:
#include<sys/time.h>
#include<sys/select.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define MAX 20
#define n 4
int main(int argc,char*argv[])
{
int sockfd,clifd,addrlen;
char buf[MAX];
struct sockaddr_in myaddr;
int i,sp,cp,ep,inputbuf[MAX],ACK=-1;
sp=0;
cp=sp;
ep=sp+n;
for(i=0;i<MAX;i++)
inputbuf[i]=i;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
fprintf(stderr,"socket error");
myaddr.sin_family=AF_INET;
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
myaddr.sin_port=htons(atoi(argv[1]));
addrlen=sizeof(struct sockaddr);

if(connect(sockfd,(struct sockaddr*)&myaddr,addrlen)<0)
fprintf(stderr,"connect error");
while(cp<MAX)
{
while((cp>=sp)&&(cp<=ep))
{
sprintf(buf,"%d",inputbuf[cp++]);
if(write(sockfd,buf,sizeof(buf))!=sizeof(buf))
fprintf(stderr,"write error");
else
printf("sending %s\n",buf);
}
if(read(sockfd,buf,sizeof(buf))>0)
{
ACK=atoi(buf);
printf("ACK %d received",ACK);
sp=ACK+1;
ep=sp+n;
}
}
return 0;

SERVER:
[s6itb10@Linuxserver divya]$ cc slidingser.c
[s6itb10@Linuxserver divya]$ ./a.out 5659
0 received
1 received
2 received
3 received
4 received
sending ack 4
5 received
6 received
7 received
8 received
9 received
sending ack 9
10 received
11 received
12 received
13 received
14 received
sending ack 14
15 received

16 received
17 received
18 received
19 received
sending ack 19
[s6itb10@Linuxserver divya]$
CLIENT:
[s6itb10@Linuxserver divya]$ cc slidingcli.c
[s6itb10@Linuxserver divya]$ ./a.out 5659
sending 0
sending 1
sending 2
sending 3
sending 4
ACK 4 receivedsending 5
sending 6
sending 7
sending 8
sending 9
ACK 9 receivedsending 10
sending 11
sending 12
sending 13
sending 14
ACK 14 receivedsending 15
sending 16
sending 17
sending 18
sending 19
ACK 19 received
[s6itb10@Linuxserver divya]$

REMOTE METHOD INVOCATION


PROGRAM:
//Remote Method Invocation
AddInff.java
import java.rmi.*;
public interface AddInff extends Remote
{
double add(double d1,double d2)throws RemoteException;
double sub(double d1,double d2)throws RemoteException;
double mul(double d1,double d2)throws RemoteException;
double div(double d1,double d2)throws RemoteException;
};
AddImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class AddImpl extends UnicastRemoteObject implements AddInff
{
public AddImpl()throws RemoteException{}
public double add(double d1,double d2)throws RemoteException
{
return d1+d2;
}
public double sub(double d1,double d2)throws RemoteException
{
return d1-d2;
}
public double mul(double d1,double d2)throws RemoteException
{
return d1*d2;
}
public double div(double d1,double d2)throws RemoteException
{
return d1/d2;
}
}
AddServer.java
import java.net.*;
import java.rmi.*;

public class AddServer


{
public static void main(String args[])
{
try
{
AddImpl a1 = new AddImpl();
Naming.rebind("AddServer",a1);
System.out.println("successfully bounded");
}
catch(Exception e)
{}
}
}
AddClient.java
import java.rmi.*;
public class AddClient
{
public static void main(String args[])
{
try
{
String addServerURL="rmi://"+args[0]+"/AddServer";
System.out.println("First number is"+args[1]);
AddInff a2=(AddInff)Naming.lookup(addServerURL);
double d1=Double.valueOf(args[1]).doubleValue();
System.out.println("Second Number is"+args[2]);
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("Sum is"+a2.add(d1,d2));
System.out.println("Difference is"+a2.sub(d1,d2));
System.out.println("Product is"+a2.mul(d1,d2));
System.out.println("Quotient is"+a2.div(d1,d2));
}
catch(Exception e)
{}
}
}

OUTPUT :
Z:\>set path="C:\Program Files\Java\jdk1.6.0_12\bin";
Z:\>cd "Network Lab"
Z:\Network Lab>javac AddClient.java
Z:\Network Lab>javac AddServer.java
Z:\Network Lab>javac AddInff.java
Z:\Network Lab>javac AddImpl.java
Z:\Network Lab>rmic AddImpl
Z:\Network Lab>start rmiregistry
Z:\Network Lab>start java AddServer
Z:\Network Lab>java AddClient localhost 10 3
First number is10
Second Number is3
Sum is13.0
Difference is7.0
Product is30.0
Quotient is3.3333333333333335
Z:\Network Lab>

ROUTING PROTOCOL
PROGRAM :
//Program to perform Routing protocol
#include<stdio.h>
int main()
{
int n;
int i,j,k;
int a[10][10],b[10][10];
printf("\nEnter the number of nodes :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\nEnter the distance between the host %d - %d :",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
}
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)

b[i][j] = a[i][j];
if(i==j)
{
b[i][j]=0;
}

}
}
printf("\nThe output matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

OUTPUT :
[s6itb16@Linuxserver indu]$ cc routing.c
[s6itb16@Linuxserver indu]$ ./a.out
Enter the number of nodes :4
Enter the distance between the host 1 - 1 :5
Enter the distance between the host 1 - 2 :9
Enter the distance between the host 1 - 3 :6
Enter the distance between the host 1 - 4 :4
Enter the distance between the host 2 - 1 :2
Enter the distance between the host 2 - 2 :1
Enter the distance between the host 2 - 3 :8
Enter the distance between the host 2 - 4 :3
Enter the distance between the host 3 - 1 :6
Enter the distance between the host 3 - 2 :1
Enter the distance between the host 3 - 3 :4
Enter the distance between the host 3 - 4 :2
Enter the distance between the host 4 - 1 :5
Enter the distance between the host 4 - 2 :1
Enter the distance between the host 4 - 3 :8
Enter the distance between the host 4 - 4 :2
5
2
6

9
1
1

6
8
4

4
3
2

The output matrix :


0
2
3
3

5
0
1
1

6
8
0
8

4
3
2
0

[s6itb16@Linuxserver indu]$

OPEN SHORTEST PATH FIRST ROUTING PROTOCOL


PROGRAM :
//Program to find the shortest path between two nodes
#include<stdio.h>
int a[5][5],n,i,j;
int main()
{
void getdata();
void shortest();
void display();
printf("\n\nProgram to find the shortest path between two nodes\n");
getdata();
shortest();
display();
}
void getdata()
{
printf("\n\nEnter the number of host in the graph\n");
scanf("%d",&n);
printf("\n\nIf there is no direct path\n");
printf("\n\nAssign the highest distance value 1000\n");
for(i=0;i<n;i++)
{
a[i][j]=0;
for(j=0;j<n;j++)
{
if(i!=j)
{
printf("\n\nEnter 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("\nShortest path is : (%d,%d) - %d \n",i+1,j+1,a[i][j]);
}
}

OUTPUT :
[s6itb10@Linuxserver divya]$ cc ospf.c
[s6itb10@Linuxserver divya]$ ./a.out
Program to find the shortest path between two nodes
Enter the number of host in the graph
4
If there is no direct path
Assign the highest distance value 1000
Enter the distance between (1,2) : 24
Enter the distance between (1,3) : 35
Enter the distance between (1,4) : 26
Enter the distance between (2,1) : 32
Enter the distance between (2,3) : 17
Enter the distance between (2,4) : 25
Enter the distance between (3,1) : 10
Enter the distance between (3,2) : 14
Enter the distance between (3,4) : 9
Enter the distance between (4,1) : 2
Enter the distance between (4,2) : 1
Enter the distance between (4,3) : 1
Shortest path is : (1,2) - 24

Shortest path is : (1,3) - 27


Shortest path is : (1,4) - 26
Shortest path is : (2,1) - 27
Shortest path is : (2,3) - 17
Shortest path is : (2,4) - 25
Shortest path is : (3,1) - 10
Shortest path is : (3,2) - 10
Shortest path is : (3,4) - 9
Shortest path is : (4,1) - 2
Shortest path is : (4,2) - 1
Shortest path is : (4,3) 1
[s6itb10@Linuxserver divya]$

PROGRAM USING NETWORK SIMULATOR V2


SIMPLE NODE CREATION

c:\cygwin\usr\local\simpler.tcl
CODE :
set ns [new Simulator]
$ns color 0 blue
$ns color 1 red
$ns color 2 white
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set f [open out1.tr w]
$ns trace-all $f
set nf [open out1.nam w]
$ns namtrace-all $nf

$ns duplex-link $n0 $n2 5Mb 2ms DropTail


$ns duplex-link $n1 $n2 5Mb 2ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-up
$ns duplex-link-op $n1 $n2 orient right-down
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n2 $n3 queuePos 0.5
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
set udp1 [new Agent/UDP]
$ns attach-agent $n3 $udp1
$udp1 set class_ 1
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
set null1 [new Agent/Null]

$ns attach-agent $n1 $null1


$ns connect $udp0 $null0
$ns connect $udp1 $null1
$ns at 1.0 "$cbr0 start"
$ns at 1.1 "$cbr1 start"
set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 1.2 "$ftp start"
$ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
puts [$cbr0 set packetSize_]
puts [$cbr0 set interval_]
$ns at 3.0 "finish"
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf

}
$ns run

puts "running nam..."


exec nam out1.nam &
exit 0

Procedure for execution of program in ns2

SIMULATION OF DISTANCE VECTOR ROUTING USING NS2


CODE:
#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

TRACE FILE: file name: distance.nam


V -t * -v 1.0a5 -a 0
A -t * -n 1 -p 0 -o 0xffffffff -c 31 -a 1
A -t * -h 1 -m 2147483647 -s 0
n -t * -a 4 -s 4 -S UP -v circle -c black -i black
n -t * -a 0 -s 0 -S UP -v circle -c black -i black
n -t * -a 5 -s 5 -S UP -v circle -c black -i black
n -t * -a 1 -s 1 -S UP -v circle -c black -i black
n -t * -a 6 -s 6 -S UP -v circle -c black -i black
n -t * -a 2 -s 2 -S UP -v circle -c black -i black
n -t * -a 3 -s 3 -S UP -v circle -c black -i black
l -t * -s 0 -d 1 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 1 -d 2 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 2 -d 3 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 3 -d 4 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 4 -d 5 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 5 -d 6 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 6 -d 0 -S UP -r 1000000 -D 0.01 -c black
+ -t 0.000169788487334637 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------- null}
- -t 0.000169788487334637 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------- null}
h -t 0.000169788487334637 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------- null}
+ -t 0.000169788487334637 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------- null}
- -t 0.000169788487334637 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------- null}
h -t 0.000169788487334637 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------- null}
+ -t 0.00710233021858257 -s 2 -d 1 -p rtProtoDV -e 7 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------- null}
- -t 0.00710233021858257 -s 2 -d 1 -p rtProtoDV -e 7 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------- null}
h -t 0.00710233021858257 -s 2 -d 1 -p rtProtoDV -e 7 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------- null}
+ -t 0.00710233021858257 -s 2 -d 3 -p rtProtoDV -e 7 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------- null}
- -t 0.00710233021858257 -s 2 -d 3 -p rtProtoDV -e 7 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------- null}
h -t 0.00710233021858257 -s 2 -d 3 -p rtProtoDV -e 7 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------- null}
r -t 0.0102257884873346 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------- null}
+ -t 0.0102257884873346 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------- null}
- -t 0.0102257884873346 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------- null}
h -t 0.0102257884873346 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------- null}
+ -t 0.0102257884873346 -s 1 -d 2 -p rtProtoDV -e 7 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------- null}
- -t 0.0102257884873346 -s 1 -d 2 -p rtProtoDV -e 7 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------- null}
h -t 0.0102257884873346 -s 1 -d 2 -p rtProtoDV -e 7 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------- null}
r -t 0.0102257884873346 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------- null}
+ -t 0.0102257884873346 -s 6 -d 0 -p rtProtoDV -e 7 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------- null}
- -t 0.0102257884873346 -s 6 -d 0 -p rtProtoDV -e 7 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------- null}
h -t 0.0102257884873346 -s 6 -d 0 -p rtProtoDV -e 7 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------- null}
+ -t 0.0102257884873346 -s 6 -d 5 -p rtProtoDV -e 7 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------- null}
- -t 0.0102257884873346 -s 6 -d 5 -p rtProtoDV -e 7 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------- null}
h -t 0.0102257884873346 -s 6 -d 5 -p rtProtoDV -e 7 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------- null}
r -t 0.0171583302185826 -s 2 -d 1 -p rtProtoDV -e 7 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------- null}

+ -t 0.0171583302185826 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 8 -a 0 -x {1.1 0.2 -1 ------- null}


- -t 0.0171583302185826 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 8 -a 0 -x {1.1 0.2 -1 ------- null}
h -t 0.0171583302185826 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 8 -a 0 -x {1.1 0.2 -1 ------- null}
+ -t 0.0171583302185826 -s 1 -d 2 -p rtProtoDV -e 7 -c 0 -i 9 -a 0 -x {1.1 2.1 -1 ------- null}
- -t 0.0171583302185826 -s 1 -d 2 -p rtProtoDV -e 7 -c 0 -i 9 -a 0 -x {1.1 2.1 -1 ------- null}
h -t 0.0171583302185826 -s 1 -d 2 -p rtProtoDV -e 7 -c 0 -i 9 -a 0 -x {1.1 2.1 -1 ------- null}
r -t 0.0171583302185826 -s 2 -d 3 -p rtProtoDV -e 7 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------- null}
+ -t 0.0171583302185826 -s 3 -d 2 -p rtProtoDV -e 7 -c 0 -i 10 -a 0 -x {3.2 2.1 -1 ------- null}
- -t 0.0171583302185826 -s 3 -d 2 -p rtProtoDV -e 7 -c 0 -i 10 -a 0 -x {3.2 2.1 -1 ------- null}
h -t 0.0171583302185826 -s 3 -d 2 -p rtProtoDV -e 7 -c 0 -i 10 -a 0 -x {3.2 2.1 -1 ------- null}
+ -t 0.0171583302185826 -s 3 -d 4 -p rtProtoDV -e 7 -c 0 -i 11 -a 0 -x {3.2 4.1 -1 ------- null}
- -t 0.0171583302185826 -s 3 -d 4 -p rtProtoDV -e 7 -c 0 -i 11 -a 0 -x {3.2 4.1 -1 ------- null}
h -t 0.0171583302185826 -s 3 -d 4 -p rtProtoDV -e 7 -c 0 -i 11 -a 0 -x {3.2 4.1 -1 ------- null}
r -t 0.0202817884873346 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------- null}
+ -t 0.0202817884873346 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 12 -a 0 -x {0.2 1.1 -1 ------- null}
- -t 0.0202817884873346 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 12 -a 0 -x {0.2 1.1 -1 ------- null}
h -t 0.0202817884873346 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 12 -a 0 -x {0.2 1.1 -1 ------- null}
+ -t 0.0202817884873346 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 13 -a 0 -x {0.2 6.1 -1 ------- null}
- -t 0.0202817884873346 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 13 -a 0 -x {0.2 6.1 -1 ------- null}
h -t 0.0202817884873346 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 13 -a 0 -x {0.2 6.1 -1 ------- null}
r -t 0.0202817884873346 -s 1 -d 2 -p rtProtoDV -e 7 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------- null}
+ -t 0.0202817884873346 -s 2 -d 1 -p rtProtoDV -e 7 -c 0 -i 14 -a 0 -x {2.1 1.1 -1 ------- null}
- -t 0.0202817884873346 -s 2 -d 1 -p rtProtoDV -e 7 -c 0 -i 14 -a 0 -x {2.1 1.1 -1 ------- null}
h -t 0.0202817884873346 -s 2 -d 1 -p rtProtoDV -e 7 -c 0 -i 14 -a 0 -x {2.1 1.1 -1 ------- null}
+ -t 0.0202817884873346 -s 2 -d 3 -p rtProtoDV -e 7 -c 0 -i 15 -a 0 -x {2.1 3.2 -1 ------- null}
- -t 0.0202817884873346 -s 2 -d 3 -p rtProtoDV -e 7 -c 0 -i 15 -a 0 -x {2.1 3.2 -1 ------- null}
h -t 0.0202817884873346 -s 2 -d 3 -p rtProtoDV -e 7 -c 0 -i 15 -a 0 -x {2.1 3.2 -1 ------- null}
r -t 0.0202817884873346 -s 6 -d 0 -p rtProtoDV -e 7 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------- null}
+ -t 0.0202817884873346 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 16 -a 0 -x {0.2 1.1 -1 ------- null}
+ -t 0.0202817884873346 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 17 -a 0 -x {0.2 6.1 -1 ------- null}
r -t 0.0202817884873346 -s 6 -d 5 -p rtProtoDV -e 7 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------- null}
+ -t 0.0202817884873346 -s 5 -d 4 -p rtProtoDV -e 7 -c 0 -i 18 -a 0 -x {5.1 4.1 -1 ------- null}
- -t 0.0202817884873346 -s 5 -d 4 -p rtProtoDV -e 7 -c 0 -i 18 -a 0 -x {5.1 4.1 -1 ------- null}
h -t 0.0202817884873346 -s 5 -d 4 -p rtProtoDV -e 7 -c 0 -i 18 -a 0 -x {5.1 4.1 -1 ------- null}
+ -t 0.0202817884873346 -s 5 -d 6 -p rtProtoDV -e 7 -c 0 -i 19 -a 0 -x {5.1 6.1 -1 ------- null}
- -t 0.0202817884873346 -s 5 -d 6 -p rtProtoDV -e 7 -c 0 -i 19 -a 0 -x {5.1 6.1 -1 ------- null}
h -t 0.0202817884873346 -s 5 -d 6 -p rtProtoDV -e 7 -c 0 -i 19 -a 0 -x {5.1 6.1 -1 ------- null}
- -t 0.0203377884873346 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 16 -a 0 -x {0.2 1.1 -1 ------- null}
h -t 0.0203377884873346 -s 0 -d 1 -p rtProtoDV -e 7 -c 0 -i 16 -a 0 -x {0.2 1.1 -1 ------- null}
- -t 0.0203377884873346 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 17 -a 0 -x {0.2 6.1 -1 ------- null}
h -t 0.0203377884873346 -s 0 -d 6 -p rtProtoDV -e 7 -c 0 -i 17 -a 0 -x {0.2 6.1 -1 ------- null}
r -t 0.0272143302185826 -s 1 -d 0 -p rtProtoDV -e 7 -c 0 -i 8 -a 0 -x {1.1 0.2 -1 ------- null}

LINK STATE ROUTING


CODE:
#Create a simulator object
set ns [new Simulator]
#Tell the simulator to use dynamic routing
$ns rtproto LS
#open the nam trace file
set nf [open links.nam w]
$ns namtrace-all $nf
#Open the output files
set nt [open links.tr w]
$ns trace-all $nt
#Define a 'finish' procedure
proc finish {} {
global ns nf nt
$ns flush-trace
#Close the trace file
close $nf
close $nt

#Call xgraph to display the results


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

TRACE FILE file name: links.tr


V -t * -v 1.0a5 -a 0
A -t * -n 1 -p 0 -o 0xffffffff -c 31 -a 1
A -t * -h 1 -m 2147483647 -s 0
n -t * -a 4 -s 4 -S UP -v circle -c black -i black
n -t * -a 0 -s 0 -S UP -v circle -c black -i black
n -t * -a 5 -s 5 -S UP -v circle -c black -i black
n -t * -a 1 -s 1 -S UP -v circle -c black -i black
n -t * -a 6 -s 6 -S UP -v circle -c black -i black
n -t * -a 2 -s 2 -S UP -v circle -c black -i black
n -t * -a 3 -s 3 -S UP -v circle -c black -i black
l -t * -s 0 -d 1 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 1 -d 2 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 2 -d 3 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 3 -d 4 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 4 -d 5 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 5 -d 6 -S UP -r 1000000 -D 0.01 -c black
l -t * -s 6 -d 0 -S UP -r 1000000 -D 0.01 -c black
+ -t 0.000169788487334637 -s 0 -d 1 -p rtProtoLS -e 100 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------ null}
- -t 0.000169788487334637 -s 0 -d 1 -p rtProtoLS -e 100 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------ null}
h -t 0.000169788487334637 -s 0 -d 1 -p rtProtoLS -e 100 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------ null}
+ -t 0.000169788487334637 -s 0 -d 6 -p rtProtoLS -e 100 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------ null}
- -t 0.000169788487334637 -s 0 -d 6 -p rtProtoLS -e 100 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------ null}
h -t 0.000169788487334637 -s 0 -d 6 -p rtProtoLS -e 100 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------ null}
+ -t 0.00710233021858257 -s 2 -d 1 -p rtProtoLS -e 100 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------null}
- -t 0.00710233021858257 -s 2 -d 1 -p rtProtoLS -e 100 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------null}
h -t 0.00710233021858257 -s 2 -d 1 -p rtProtoLS -e 100 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------null}
+ -t 0.00710233021858257 -s 2 -d 3 -p rtProtoLS -e 100 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------null}
- -t 0.00710233021858257 -s 2 -d 3 -p rtProtoLS -e 100 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------null}
h -t 0.00710233021858257 -s 2 -d 3 -p rtProtoLS -e 100 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------null}

r -t 0.0109697884873346 -s 0 -d 1 -p rtProtoLS -e 100 -c 0 -i 0 -a 0 -x {0.2 1.1 -1 ------null}


+ -t 0.0109697884873346 -s 1 -d 0 -p rtProtoLS -e 20 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------null}
- -t 0.0109697884873346 -s 1 -d 0 -p rtProtoLS -e 20 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------null}
h -t 0.0109697884873346 -s 1 -d 0 -p rtProtoLS -e 20 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------null}
+ -t 0.0109697884873346 -s 1 -d 2 -p rtProtoLS -e 100 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------null}
- -t 0.0109697884873346 -s 1 -d 2 -p rtProtoLS -e 100 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------null}
h -t 0.0109697884873346 -s 1 -d 2 -p rtProtoLS -e 100 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------null}
r -t 0.0109697884873346 -s 0 -d 6 -p rtProtoLS -e 100 -c 0 -i 1 -a 0 -x {0.2 6.1 -1 ------null}
+ -t 0.0109697884873346 -s 6 -d 0 -p rtProtoLS -e 20 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------null}
- -t 0.0109697884873346 -s 6 -d 0 -p rtProtoLS -e 20 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------null}
h -t 0.0109697884873346 -s 6 -d 0 -p rtProtoLS -e 20 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------null}
+ -t 0.0109697884873346 -s 6 -d 5 -p rtProtoLS -e 100 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------null}
- -t 0.0109697884873346 -s 6 -d 5 -p rtProtoLS -e 100 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------null}
h -t 0.0109697884873346 -s 6 -d 5 -p rtProtoLS -e 100 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------null}
r -t 0.0179023302185826 -s 2 -d 1 -p rtProtoLS -e 100 -c 0 -i 2 -a 0 -x {2.1 1.1 -1 ------null}
+ -t 0.0179023302185826 -s 1 -d 2 -p rtProtoLS -e 20 -c 0 -i 8 -a 0 -x {1.1 2.1 -1 ------null}
- -t 0.0179023302185826 -s 1 -d 2 -p rtProtoLS -e 20 -c 0 -i 8 -a 0 -x {1.1 2.1 -1 ------null}
h -t 0.0179023302185826 -s 1 -d 2 -p rtProtoLS -e 20 -c 0 -i 8 -a 0 -x {1.1 2.1 -1 ------null}
+ -t 0.0179023302185826 -s 1 -d 0 -p rtProtoLS -e 100 -c 0 -i 9 -a 0 -x {1.1 0.2 -1 ------null}
- -t 0.0179023302185826 -s 1 -d 0 -p rtProtoLS -e 100 -c 0 -i 9 -a 0 -x {1.1 0.2 -1 ------null}
h -t 0.0179023302185826 -s 1 -d 0 -p rtProtoLS -e 100 -c 0 -i 9 -a 0 -x {1.1 0.2 -1 ------null}
r -t 0.0179023302185826 -s 2 -d 3 -p rtProtoLS -e 100 -c 0 -i 3 -a 0 -x {2.1 3.2 -1 ------null}
+ -t 0.0179023302185826 -s 3 -d 2 -p rtProtoLS -e 20 -c 0 -i 10 -a 0 -x {3.2 2.1 -1 ------null}

- -t 0.0179023302185826 -s 3 -d 2 -p rtProtoLS -e 20 -c 0 -i 10 -a 0 -x {3.2 2.1 -1 ------null}


h -t 0.0179023302185826 -s 3 -d 2 -p rtProtoLS -e 20 -c 0 -i 10 -a 0 -x {3.2 2.1 -1 ------null}
+ -t 0.0179023302185826 -s 3 -d 4 -p rtProtoLS -e 100 -c 0 -i 11 -a 0 -x {3.2 4.1 -1 ------null}
- -t 0.0179023302185826 -s 3 -d 4 -p rtProtoLS -e 100 -c 0 -i 11 -a 0 -x {3.2 4.1 -1 ------null}
h -t 0.0179023302185826 -s 3 -d 4 -p rtProtoLS -e 100 -c 0 -i 11 -a 0 -x {3.2 4.1 -1 ------null}
r -t 0.0211297884873346 -s 1 -d 0 -p rtProtoLS -e 20 -c 0 -i 4 -a 0 -x {1.1 0.2 -1 ------null}
r -t 0.0211297884873346 -s 6 -d 0 -p rtProtoLS -e 20 -c 0 -i 6 -a 0 -x {6.1 0.2 -1 ------null}
r -t 0.0217697884873346 -s 1 -d 2 -p rtProtoLS -e 100 -c 0 -i 5 -a 0 -x {1.1 2.1 -1 ------null}
+ -t 0.0217697884873346 -s 2 -d 1 -p rtProtoLS -e 20 -c 0 -i 12 -a 0 -x {2.1 1.1 -1 ------null}
- -t 0.0217697884873346 -s 2 -d 1 -p rtProtoLS -e 20 -c 0 -i 12 -a 0 -x {2.1 1.1 -1 ------null}
h -t 0.0217697884873346 -s 2 -d 1 -p rtProtoLS -e 20 -c 0 -i 12 -a 0 -x {2.1 1.1 -1 ------null}
+ -t 0.0217697884873346 -s 2 -d 3 -p rtProtoLS -e 100 -c 0 -i 13 -a 0 -x {2.1 3.2 -1 ------null}
- -t 0.0217697884873346 -s 2 -d 3 -p rtProtoLS -e 100 -c 0 -i 13 -a 0 -x {2.1 3.2 -1 ------null}
h -t 0.0217697884873346 -s 2 -d 3 -p rtProtoLS -e 100 -c 0 -i 13 -a 0 -x {2.1 3.2 -1 ------null}
r -t 0.0217697884873346 -s 6 -d 5 -p rtProtoLS -e 100 -c 0 -i 7 -a 0 -x {6.1 5.1 -1 ------null}
+ -t 0.0217697884873346 -s 5 -d 6 -p rtProtoLS -e 20 -c 0 -i 14 -a 0 -x {5.1 6.1 -1 ------null}
- -t 0.0217697884873346 -s 5 -d 6 -p rtProtoLS -e 20 -c 0 -i 14 -a 0 -x {5.1 6.1 -1 ------null}
h -t 0.0217697884873346 -s 5 -d 6 -p rtProtoLS -e 20 -c 0 -i 14 -a 0 -x {5.1 6.1 -1 ------null}
+ -t 0.0217697884873346 -s 5 -d 4 -p rtProtoLS -e 100 -c 0 -i 15 -a 0 -x {5.1 4.1 -1 ------null}
- -t 0.0217697884873346 -s 5 -d 4 -p rtProtoLS -e 100 -c 0 -i 15 -a 0 -x {5.1 4.1 -1 ------null}
h -t 0.0217697884873346 -s 5 -d 4 -p rtProtoLS -e 100 -c 0 -i 15 -a 0 -x {5.1 4.1 -1 ------null}
r -t 0.0280623302185826 -s 1 -d 2 -p rtProtoLS -e 20 -c 0 -i 8 -a 0 -x {1.1 2.1 -1 ------null}
r -t 0.0280623302185826 -s 3 -d 2 -p rtProtoLS -e 20 -c 0 -i 10 -a 0 -x {3.2 2.1 -1 ------null}

r -t 0.0287023302185826 -s 1 -d 0 -p rtProtoLS -e 100 -c 0 -i 9 -a 0 -x {1.1 0.2 -1 ------null}


+ -t 0.0287023302185826 -s 0 -d 1 -p rtProtoLS -e 20 -c 0 -i 16 -a 0 -x {0.2 1.1 -1 ------null}
- -t 0.0287023302185826 -s 0 -d 1 -p rtProtoLS -e 20 -c 0 -i 16 -a 0 -x {0.2 1.1 -1 ------null}
h -t 0.0287023302185826 -s 0 -d 1 -p rtProtoLS -e 20 -c 0 -i 16 -a 0 -x {0.2 1.1 -1 ------null}
+ -t 0.0287023302185826 -s 0 -d 6 -p rtProtoLS -e 100 -c 0 -i 17 -a 0 -x {0.2 6.1 -1 ------null}
- -t 0.0287023302185826 -s 0 -d 6 -p rtProtoLS -e 100 -c 0 -i 17 -a 0 -x {0.2 6.1 -1 ------null}

You might also like