68
CS9216 NETWORKING LAB(C/C++) LIST OF EXPERIMENTS 1. Socket Programming a. TCP Sockets b. UDP Sockets c. Applications using Sockets 2. Simulation of Sliding Window Protocol 3. Simulation of Routing Protocols 4. Development of applications such as DNS/ HTTP/ E mail/ Multi - user Chat 5. Simulation of Network Management Protocols 6. Study of Network Simulator Packages such as opnet, ns2, etc.

Networking lab

  • View
    2.168

  • Download
    6

Embed Size (px)

DESCRIPTION

CSC

Citation preview

CS9216 NETWORKING LAB(C/C++)

LIST OF EXPERIMENTS

1. Socket Programming

a. TCP Sockets

b. UDP Sockets

c. Applications using Sockets

2. Simulation of Sliding Window Protocol

3. Simulation of Routing Protocols

4. Development of applications such as DNS/ HTTP/ E – mail/ Multi -

user Chat

5. Simulation of Network Management Protocols

6. Study of Network Simulator Packages – such as opnet, ns2, etc.

HARDWARE REQUIREMENTS

Processor :Pentium IV

Hard disk :20 GB

RAM :256 MB

Monitor :VGA and high resolution monitor

SOFTWARE REQUIREMENTS

Operating system :LINUX

Language :C/C++

Ex.no:1 CLIENT-SERVER CHAT PROGRAM

Date: USING TCP

AIM

To write a C program for implementing Client-Server Chat using TCP.

ALGORITHM

SERVER

Step 1: Start the program.

Step 2: Create an unnamed socket for the server using the

parameters AF_INET as domain and the SOCK_STREAM as type.

Step 3: Name the socket using bind( ) system call with the

parameters server_sockfd and the server address(sin_addr and sin_sport).

Step 4: Create a connection queue and wait for clients using the listen( ) system call

with the number of clients request as parameters.

Step 5: Accept the connection using accept( ) system call when client requests for

connection.

Step 6: Get the message which has to be sent to the client and check that it is not

equal to ‘Bye’.

Step 7: If the message is not equal to ‘Bye’ then write the message to the client and

Goto step 6.

Step 8: If the message is ‘Bye’ then terminate the Process.

Step 9: Stop the program execution.

CLIENT

Step 1: Start the program.

Step 2: Create an unnamed socket for client using socket( ) system.

Step 3: Call with parameters AF_INET as domain and SOCK_STREAM as type.

Step 4: Name the socket using bind( ) system call.

Step 5: Now connect the socket to server using connect( ) system call.

Step 6: Read the message from the server socket and compare it with ‘Bye’.

Step 7: If the message is not equal to ‘Bye’ then print the message to the server

output device and repeat the steps 6 & 7.

Step 8: Get the message from the client side.

Step 9: Write the message to server sockfd and goto step 4.

Step 10:If the message is equal to ‘Bye’then print good bye message and terminate

the process.

Step 11:Stop the process.

CLIENT SERVER CHAT USING TCP

SERVER

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

int sd,sd2,nsd,clilen,sport,len;

char sendmsg[20],rcvmsg[20];

struct sockaddr_in servaddr,cliaddr;

printf("Enter the Server port");

printf("\n_____________________\n");

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

printf(" Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

printf("\nReceived Messages\n");

do

{

recv(nsd,rcvmsg,20,0);

printf("%s",rcvmsg);

fgets(sendmsg,20,stdin);

len=strlen(sendmsg);

sendmsg[len-1]='\0';

send(nsd,sendmsg,20,0);

wait(20);

}

while(strcmp(sendmsg,"bye")!=0);

}

CLIENT

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len;

char sendmsg[20],revmsg[20];

struct sockaddr_in servaddr;

printf("Enter the port\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

printf("Can't Create\n");

else

printf("Scocket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Can't Connect\n");

else

printf("Connected\n");

do

{

fgets(sendmsg,20,stdin);

len=strlen(sendmsg);

sendmsg[len-1]='\0';

send(csd,sendmsg,20,0);

wait(20);

recv(csd,revmsg,20,0);

printf("%s",revmsg);

}

while(strcmp(revmsg,"bye")!=0);

}

OUTPUT:

Client Side

[1me16@localhost ~]$ cc tcpclient.c

[1me16@localhost ~]$. /a.out

Enter the port

6523

Socket is Created…

Connected……

Hello

Server Side

[1me16@localhost ~]$ cc tcpserver.c.c

[1me16@localhost ~]$. /a.out

Enter the server port…

6543

Socket is Created

Binded

Accepted

Received Messages…. Hello

RESULT

Thus the C program for chat using TCP is executed and the output is verified

successfully

Ex.no:2 CLIENT-SERVER CHAT USING UDP

Date:

AIM

To write a C program for implementing chat program using UDP.

ALGORITHM

SERVER

Step 1: Start the program.

Step 2: Create an unnamed socket for the server using the

parameters AF_INET as domain and the SOCK_DGRAM as type.

Step 3: Name the socket using bind( ) system call with the

parameters server_sockfd and the server

address(sin_addr and sin_sport).

Step 4: The server gets the message from the client.

Step 5: Prints the message.

Step 6: Stop the program execution.

CLIENT

Step 1: Start the program.

Step 2: Create an unnamed socket for client using socket

Step 3: Call with parameters AF_INET as domain an

SOCK_DGRAM as type.

Step 4: Name the socket using bind( ) system call.

Step 5: The Sendto( ) system call is used to deliver the

Message to the server.

Step 6: Stop the program execution.

CLIENT SERVER CHAT USING UDP

SERVER

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

main()

{

struct sockaddr_in sadd,cadd;

int id,a,b,len,port;

char rbuff[100];

id=socket(PF_INET,SOCK_DGRAM,0);

if(id<0)

printf("Can't Create\n");

else

printf("Created\n");

printf("Enter the port Address\n");

printf("____________________\n");

scanf("%d",&port);

sadd.sin_family=PF_INET;

sadd.sin_addr.s_addr=htonl(INADDR_ANY);

sadd.sin_port=htons(port);

b=bind(id,(struct sockaddr*)&sadd,sizeof(sadd));

if(b<0)

printf("Can't Bind");

else

printf("Binded\n");

printf("~~~~~~\n");

len=sizeof(cadd);

if(recvfrom(id,rbuff,sizeof(rbuff),0,(struct sockaddr*)&cadd,&len)<0)

printf("Received Error\n");

else

printf("Server received =%s\n",rbuff);

close(id);

}

CLIENT

#include<stdio.h>

#include<sys/socket.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

struct sockaddr_in sadd,cadd;

int id,len,n,c,s,b,port;

char str[100],serstr[100];

id=socket(PF_INET,SOCK_DGRAM,0);

if(id<0)

printf("Can't Create\n");

else

printf("Socket is Created\n");

printf("Enter the IP address\n");

scanf("%s",serstr);

printf("Enter the port Address\n");

scanf("%d",&port);

cadd.sin_family=PF_INET;

cadd.sin_addr.s_addr=inet_addr(serstr);

cadd.sin_port=htons(port);

printf("Enter the Data\n");

scanf("%s",str);

b=bind(id,(struct sockaddr*)&cadd,sizeof(cadd));

if(sendto(id,str,sizeof(str),0,(struct sockaddr*)&cadd,sizeof(cadd))<0)

printf("Transmit Error");

else

printf("Server Transmitted=%s\n",str);

close(id);

}

OUTPUT:

Client Side

[1me16@localhost ~]$ cc udpclient.c

[1me16@localhost ~]$. /a.out

Socket is Created…

Enter the IP Address

172.15.170.104

Enter the port address

6543

Enter the data

Hello

Server transmitted = hello

Server Side

[1me16@localhost ~]$ cc udpserver.c

[1me16@localhost ~]$. /a.out

Created

Enter the port address

………….

6543

Binded

…………….

RESULT

Thus the C program for chat using UDP is executed and the output is verified

successfully.

Ex. No. 3a PRINTING THE CLIENT ADDRESS

Date: AT THE SERVER END

AIM

To write a C program for printing the client address at the server end.

ALGORITHM

SERVER

Step 1: Start the program.

Step 2: Create an unnamed socket for the server using the

parameters AF_INET as domain and the SOCK_STREAM as type.

Step 3: Name the socket using bind( ) system call with the

parameters server_sockfd and the server address(sin_addr and sin_sport).

Step 4: Create a connection queue and wait for clients

using the listen( ) system call with the number of clients request as

parameters.

Step 5: Accept the connection using accept( ) system call when client requests for

connection.

Step 6: If the descriptor is less than zero,then the connection is not established and

the stop the process.

Step 7: Print the IP address sent by the client to the server.

Step 8: Stop the program execution.

CLIENT

Step 1: Start the program.

Step 2: Create an unnamed socket for client using socket( ) system.

Step 3: Call with parameters AF_INET as domain and SOCK_STREAM as type.

Step 4: Name the socket using bind( ) system call.

Step 5: Now connect the socket to server using connect( ) system call.

Step 6: If the descriptor is less than zero, then the

connection is not established.

Step 7: If there is no connection,then terminate the process.

Step 8: Else send the IP address to the server.

Step 9: Stop the process.

PRINTING THE CLIENT ADDRESS AT THE SERVER END

SERVER

#include<stdio.h>

#include<sys/socket.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

int sd,sd2,nsd,clilen,sport,len;

char sendmsg[20],rcvmsg[20];

struct sockaddr_in servaddr,cliaddr;

printf("Enter the Port\n");

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=inet_addr("172.15.170.104");

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

printf(" Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

printf("The Client Address is %s",inet_ntoa(cliaddr.sin_addr.s_addr));

}

CLIENT

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len;

char sendmsg[20],revmsg[20];

struct sockaddr_in servaddr;

printf("Enter the port\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

printf("Can't Create\n");

else

printf("Scocket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=inet_addr("172.15.170.104");

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Can't Connect\n");

else

printf("Connected\n");

}

OUTPUT:

Client Side

[1me16@localhost ~]$ cc addressclient.c

[1me16@localhost ~]$. /a.out

Enter the port:

6543

Socket is Created...

Server Side

[1me16@localhost ~]$ cc addressserver.c

[1me16@localhost ~]$. /a.out

Enter the port:

6543

Created...

Binded……

RESULT

Thus the C program for printing the client IP Address at the server end is executed

and the output is verified successfully.

Ex.no: 3b DATE-TIME SERVER

Date:

AIM

To write a C program for implementing the simple TCP client-server where the

server acts as a Date-Time server.

ALGORITHM

SERVER

Step 1 :Start the program

Step 2 :Create an unnamed socket for the server using parameters AF_INET as

domain and SOCK_STREAM as type.

Step 3 : Declare the time variables t.

Step 4 :Get the server port number.

Step 5 :Register the host address to the system by using bind() system call in server

side.

Step 6 :Create a connection queue and wait for clients using listen() system call with

The number of clients requests as parameter.

Step 7 :Accept the connection using accept( ) system call when the client

request for connection.

Step 8 :Stop the Program execution.

CLIENT

Step 1 :Start the program.

Step 2 :Create an unnamed socket for the client using parameters AF_INET as

domain and SOCK_STREAM as type.

Step 3 :Get the client port number.

Step 4 :Now connect the socket to server using connect( ) system call.

Step 5 :The recv() system call gets the response of Date-Time request from the

server.

Step 6 :Print the date and time

Step 7 :Stop the program.

DISPLAYING TIME AT THE CLIENT END

SERVER

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

#include<time.h>

main()

{

int sd,sd2,nsd,clilen,sport,len;

char sendmsg[20],rcvmsg[20];

time_t t;

struct sockaddr_in servaddr,cliaddr;

printf("Enter the Port no\n");

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

time(&t);

strcpy(sendmsg,ctime(&t));

printf("%s",sendmsg);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

printf(" Can't Bind\n");

else

printf("\n Binded \n");

listen(sd,5);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

send(nsd,sendmsg,100,0);

}

CLIENT

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdlib.h>

#include<time.h>

main()

{

int csd,cport,len;

char revmsg[100];

struct sockaddr_in servaddr;

printf("Enter the port\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

printf("Can't Create\n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Connection error\n");

recv(csd,revmsg,100,0);

printf("%s\n",revmsg);

}

OUTPUT:

Client Side

[1me16@localhost ~]$ cc dateclient.c

[1me16@localhost ~]$. /a.out

Enter the port:

8543

Socket is Created...

Connected…

Server Side

[1me16@localhost ~]$ cc dateserver.c

[1me16@localhost ~]$. /a.out

Enter the port:

8543

Fri Sep 17 03:05:27 2010

Socket is Created...

RESULT

Thus the C program for printing Date and time is executed and the output is verified

successfully.

Ex.no:3c FILE TRANSFER USING TCP

Date:

AIM: To write a C program for transferring a file using TCP.

ALGORITHM:

SERVER:

Step 1:Start the program.

Step 2:Create an unnamed socket for the server using parameters AF_INET as

domain and SOCK_STREAM as type.

Step 3:Get the server port number.

Step 4:Register the host address to the system by using bind() system call in server

side.

Step 5:Create a connection queue and wait for clients using listen() system call with

the number of clients requests as parameter.

Step 6:Create a Child process using fork( ) system call.

Step 7:If the process identification number is equal to zero accept the connection

using accept( ) system call when the client request for connection.

Step 8:If pid is not equal to zero then exit the process.

Step 9:Stop the Program execution.

CLIENT:

Step 1:Start the program.

Step 2:Create an unnamed socket for the client using parameters AF_INET as

domain and SOCK_STREAM as type.

Step 3:Get the client port number.

Step 4:Now connect the socket to server using connect( ) system call.

Step 5:Enter the file name.

Step 6:The file is transferred from client to server using send ( ) function.

Step 7:Print the contents of the file in a new file.

Step 8:Stop the program.

FILE TRANSFER PROTOCOL

USING TCP

SERVER #include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

FILE *fp;

int sd,newsd,ser,n,a,cli,pid,bd,port,clilen;

char name[100],fileread[100],fname[100],ch,file[100],rcv[100];

struct sockaddr_in servaddr,cliaddr;

printf("Enter the port address: ");

scanf("%d",&port);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(port);

a=sizeof(servaddr);

bd=bind(sd,(struct sockaddr*)&servaddr,a);

if(bd<0)

printf(" Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

newsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(newsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

n=recv(newsd,rcv,100,0);

rcv[n]='\0';

fp=fopen(rcv,"r");

if(fp==NULL)

{

send(newsd,"error",5,0);

close(newsd);

}

else

{

while(fgets(fileread,sizeof(fileread),fp))

{

if(send(newsd,fileread,sizeof(fileread),0)<0)

{

printf("Can't send\n");

}

sleep(1);

}

if(!fgets(fileread,sizeof(fileread),fp))

{

send(newsd,"completed",999999999,0);

}

return(0);

}

}

CLIENT

#include<stdio.h>

#include<sys/socket.h>

#include<netinet/in.h>

main()

{

FILE *fp;

int csd,n,ser,s,cli,cport,newsd;

char name[100],rcvmsg[100],rcvg[100],fname[100];

struct sockaddr_in servaddr;

printf("Enter the port");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

{

printf("Error...");

exit(0);

}

else

printf("Socket is Created...\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Error in Connection...\n");

else

printf("Connected...\n");

printf("Enter the existing file name: ");

scanf("%s",name);

printf("\nEnter the new filename: ");

scanf("%s",fname);

fp=fopen(fname,"w");

send(csd,name,sizeof(name),0);

while(1)

{

s=recv(csd,rcvg,100,0);

rcvg[s]='\0';

if(strcmp(rcvg,"error")==0)

printf("File is not Available...\n");

if(strcmp(rcvg,"completed")==0) {

printf("file is transferred...\n");

fclose(fp);

close(csd);

break;

}

else

fputs(rcvg,stdout);

fprintf(fp,"%s",rcvg);

}

}

OUTPUT:

Server Side

[1me16@localhost ~]$ cc ftpclient.c

[1me16@localhost ~]$. /a.out

Enter the port address:

8663

Socket is Created

Binded

Connected…

Client Side

[1me16@localhost ~]$ cc ftpserver.c

[1me16@localhost ~]$. /a.out

Socket is Created..

Connected

Enter the existing file name: net

Enter the new file name: network

Welcome to Network Lab

File is transferred...

RESULT

Thus the C program for transferring file from one machine to another machine

using TCP is executed and the output is verified successfully.

EX.NO:4 SIMULATION OF SLIDING WINDOW

PROTOCOL

DATE:

AIM

To write a C program for the simulation of Sliding Window Protocol.

ALGORITHM

SENDER

Step 1: Start the program.

Step 2: Create a Socket for the Sender and bind it with the receiver.

Step 3: Set the size of the window.

Step 4: Send the frames upto the size of the window to the receiver

Step 5: If any of the frames are lost then retransmit those frames to the receiver

Step 6: Stop the execution.

RECEIVER

Step 1: Start the program.

Step 2: Create the Socket for the Receiver, bind it and listen for the frames from the

sender.

Step 3: If all the frames are successfully received, send the acknowledgement for the last

frame

to the sender.

Step 4: If any of the frames is lost, then send the acknowledgement of the last frame,

which was

successfully received.

Step 5: Keep on receiving and acknowledging the frames until the sender sends.

Step 6: Stop the program execution.

SLIDING WINDOW PROTOCOL

SERVER #include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

#include<sys/socket.h>

main()

{

int a,bd,sd,newsd,port,clilen;

char lost[20],sendmsg[20],recvmsg[20];

struct sockaddr_in servaddr,cliaddr;

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

printf("Enter the port no\n");

scanf("%d",&port);

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(port);

a=sizeof(servaddr);

bd=bind(sd,(struct sockaddr*)&servaddr,a);

if(bd<0)

printf(" Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

newsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(newsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

printf("Enter the lost frame\n");

scanf("%s",lost);

send(newsd,lost,20,0);

recv(newsd,recvmsg,20,0);

printf("\n Frame %s is successfully received",recvmsg);

}

CLIENT

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

int i,sd,n,port;

char sendmsg[100],recvmsg[100];

struct sockaddr_in servaddr;

printf("Enter the port\n");

scanf("%d",&port);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create\n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(port);

if(connect(sd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Can't Connect\n");

else

printf("Connected\n");

printf("Enter the no of frames\n");

scanf("%d",&n);

printf("\nThe frames all\n");

for(i=1;i<=n;i++)

printf("Frame %d\n",i);

recv(sd,recvmsg,20,0);

printf("\n Lost frame %s is retransmitted ",recvmsg);

strcpy(sendmsg,recvmsg);

send(sd,sendmsg,20,0);

}

OUTPUT:

Server:

[1me16alhost~]: VI swserver.c

[1me16alhost~]: CC swserver.c

[1me16alhost~]: /a.out

Enter the port address

8543

Socket is created

Binded

Accepted

Enter the lost frame: 3

Frame tcpserver.c is successfully transmitted

Client:

[1me16alhost~]: VI swclient.c

[1me16alhost~]: CC swclient.c

[1me16alhost~]: /a.out

Enter the client port no

8543

Socket is created

Connected…………..

Enter the no of frames: 4

The frames all

Frame 1/n Frame 2/n Frame 3/n Frame 4/n

RESULT

Thus the C program for the simulation of Sliding Window Protocol has been

executed and the output is verified successfully.

EX.NO:5 DOMAIN NAME SYSTEM

DATE:

AIM

To write a C program for the simulation of Domain Name System

ALGORITHM

SERVER

Step 1: Start the program.

Step 2: Create the Socket for the Server.

Step 3: Bind the Socket to the Port.

Step 4: Listen for the incoming client connection.

Step 5: Receive the IP address from the client to be resolved.

Step 6: Get the domain name from the client.

Step 7: Check the existence of the domain in the server.

Step 8: If domain matches then send the corresponding address to the client.

Step 9: Stop the program execution.

CLIENT

Step 1: Start the program.

Step 2: Create the Socket for the client.

Step 3: Connect the Socket to the server.

Step 4: Send the hostname to the server to be resolved.

Step 5: If the server responds the print the address and terminates the process.

DOMAIN NAME SYSTEM

SERVER #include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

int sd,sd2,nsd,clilen,sport,len,i;

char sendmsg[20],recvmsg[20];

char ipid[20][20]={"172.15.64.66","172.15.44.55","172.15.33.44","172.15.22.33"};

char hostid[20][20]={"www.yahoo.com","www.google.com","www.hotmail.com"};

struct sockaddr_in servaddr,cliaddr;

printf("DNS Server Side\n");

printf("Enter the Port\n");

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

printf("Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

recv(nsd,recvmsg,20,0);

for(i=0;i<4;i++)

{

if(strcmp(recvmsg,hostid[i])==0)

{

send(nsd,ipid[i],20,20);

break; }}}

CLIENT #include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len;

char sendmsg[20],recvmsg[20];

struct sockaddr_in servaddr;

printf("DNS Client Side\n");

printf("Enter the Client port\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

printf("Can't Create\n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Can't Connect\n");

else

printf("Connected\n");

printf("Enter the host address\n");

scanf("%s",sendmsg);

send(csd,sendmsg,20,0);

recv(csd,recvmsg,20,20);

printf("The Coresponding IP Address is\n");

printf("%s",recvmsg);

}

OUTPUT:

Server:

[1me16alhost~]: VI dnsserver.c

[1me16alhost~]: CC dnsserver.c

[1me16alhost~]: /a.out

Enter the port no

8543

Socket is created

Binded

Accepted

Client:

[1me16alhost~]: VI dnsclient.c

[1me16alhost~]: CC dnsclient.c

[1me16alhost~]: /a.out

Enter the client port no

8543

Socket is created

Connected…………..

Enter the host address

www.yahoo.com

The corresponding IP Address is

172.15.64.66

RESULT

Thus the C program for the simulation of Domain Name System has been executed

and the output is verified successfully.

EX.NO:6 SIMULATION OF ROUTING PROTOCOLS

DATE:

AIM

To Simulate Shortest Path Routing Algorithm

ALGORITHM

Step 1: Start the Program

Step 2: Create a distance list, a previous vertex list, a visited list, and a current vertex.

Step 3: All the values in the distance list are set to infinity except the starting vertex

which is set to zero.

Step 4: All values in visited list are set to false.

Step 5: All values in the previous list are set to a special value signifying that they are

undefined.

Step 6: Current node is set as the starting vertex.

Step 7: Mark the current vertex as visited.

Step 8: Update distance and previous lists based on those vertices which can be

immediately reached from the current vertex.

Step 9: Update the current vertex to the unvisited vertex that can be reached by the

shortest path from the starting vertex.

Step 10: Repeat (from step 6) until all nodes are visited.

Step 11: Stop the program execution.

SIMULATION OF OSPF ROUTING PROTOCOL

#include<iostream.h>

#include<conio.h>

class dij

{

private:

int graph[15][15],source,no;

int set[15],predessor[15],mark[15],pathestimate[15];

public:

int minimum();

void read();

void initialize();

void printpath(int);

void algorithm();

void output();

};

void dij::read()

{

cout<<"Enter the no of vertices: ";

cin>>no;

cout<<"Enter the Adjacent matrices";

for(int i=1;i<=no;i++)

{

cout<<"Enter the weight for row: "<<i;

for(int j=1;j<=no;j++)

{

cin>>graph[i][j];

}

}

cout<<"Enter the source Vector: ";

cin>>source;

}

void dij::initialize()

{

for(int i=1;i<=no;i++)

{

mark[i]=0;

pathestimate[i]=999;

predessor[i]=0;

}

pathestimate[source]=0;

}

void dij::algorithm()

{

initialize();

int count=0,i,u;

while(count<no)

{

u=minimum();

set[++count]=u;

mark[u]=1;

for(int i=1;i<=no;i++)

{

if(graph[u][i]>0)

{

if(mark[i]!=1)

{

if(pathestimate[i]>pathestimate[u]+graph[u][i])

{

predessor[i]=u;

pathestimate[i]=pathestimate[u]+graph[u][i];

}

}

}

} //for loop

} //while loop

}

void dij::printpath(int i)

{

cout<<endl;

if(i==source)

{

cout<<source;

}

else if(predessor[i]==0)

cout<<"No path from"<<source<<"to"<<i;

else

{

printpath(predessor[i]);

cout<<"....."<<i;

}

}

void dij::output()

{

for(int i=1;i<=no;i++)

{

printpath(i);

if(pathestimate[i]!=999)

cout<<"->("<<pathestimate[i]<<")\n";

}

cout<<endl;

}

int dij::minimum()

{

int min=999,i,t;

for(i=1;i<=no;i++)

{

if(mark[i]!=1)

{

if(min>=pathestimate[i])

{

min = pathestimate[i];

t=i;

}

}

}

return t;

}

void main()

{

clrscr();

dij d;

d.read();

d.algorithm();

d.output();

getch();

}

OUTPUT

Enter the no of vertices: 3

Enter the Adjacent matrices

Enter the weight for row: 1

0

1

3

Enter the weight for row: 2

2

1

1

Enter the weight for row: 3

2

1

1

Enter the source vector: 1

1 -> (0)

1……. 2- > (1)

1…… 2 ……. 3 -> (1)

RESULT:

Thus the Program for simulating Shortest Path Routing Algorithm is executed

and the output is verified successfully.

EX.NO:6b UNIFORM RESOURCE LOCATOR (URL)

DATE:

AIM

To retrieve the data using Uniform Resource Locators

ALGORITHM

Step 1: Start the Program.

Step 2: Create an object for the URL class.

Step 3: Specify the address from which the data is to be retrieved inside the URL class

Step 4: Open the URL connection

Step 5: Request the content Length, modified date etc. using appropriate the methods.

Step 6: Display the contents to the user

Step 7: Stop the program

UNIFORM RESOURCE LOCATOR

#include<iostream.h>

#include<stdio.h>

#include<dos.h>

#include<conio.h>

#include<string.h>

void main()

{

char c;

int len=0;

struct date d;

struct time t;

clrscr();

FILE *fp;

getdate(&d);

gettime(&t);

fp=fopen("welcome.html","r");

printf("Content type = text/html\n");

printf("\n Date : %d-%d-%d Time : %d:%d:%d

\n",d.da_day,d.da_mon,d.da_year,t.ti_hour,t.ti_min,t.ti_sec);

c=fgetc(fp);

cout<<c;

len++;

while((c=fgetc(fp))!=-1)

{

if(c!='\n'||' ');

len++;

cout<<c;

}

printf("\n Content length is %d",len);

getch();

}

OUTPUT:

CONTENT TYPE=text/html

Date:03-11-2010 time:15:13:28

<html>

<head>

<title>welcome</title>

<body bgcolor=”blue”>

<h1>welcome to network lab-1</h>

</body>

</html>

content length is 109

RESULT

Thus the program for retrieving the data using URL is executed and the output is

verified successfully.

Ex.No:8 MULTICLIENT-SERVER CHAT

Date:

AIM: To write a C program for implementing Client-Server Chat using TCP.

ALGORITHM:

SERVER:

Step 1: Start the program.

Step 2: Create an unnamed socket for the server using the parameters AF_INET as

domain and the SOCK_STREAM as type.

Step 3: Name the socket using bind ( ) system call with the parameters server_sockfd and

the server address (sin_addr and sin_sport).

Step 4: Create a connection queue and wait for clients using the listen( ) system call with

the number of clients request as parameters.

Step 5: Get the client‟s id as input from the user to communicate. If the client‟s id is 0

then go to step 10 otherwise go to step 6.

Step 6: Accept the connection using accept ( ) system call when client requests for

connection.

Step 7: Get the message which has to be sent to the client and check that it is not equal to

„Bye‟.

Step 8: If the message is not equal to „Bye‟ then write the message to the client and Goto

step 6.

Step 9: If the message is „Bye‟ then terminates the connection with current client and Go

to

step 5.

Step 10: Stop the program execution.

CLIENT:

Step 1: Start the program.

Step 2: Create an unnamed socket for client using socket ( ) system.

Step 3: Call with parameters AF_INET as domain and SOCK_STREAM as type.

Step 4: Name the socket using bind( ) system call.

Step 5: Now connect the socket to server using connect ( ) system call.

Step 6: Read the message from the server socket and compare it with „Bye‟.

Step 7: If the message is not equal to „Bye‟ then print the message to the server output

device and repeat the steps 6 & 7.

Step 8: Get the message from the client side.

Step 9: Write the message to server sockfd and goto step 4.

Step 10: If the message is equal to „Bye‟then print good bye message and terminate the

process.

Step 11: Stop the process.

MULTI USER CHAT

SERVER

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

int i,sd,sd2,nsd,clilen,sport,len;

char sendmsg[20],rcvmsg[20];

struct sockaddr_in servaddr,cliaddr;

printf(“Enter the port no:\n”);

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

printf("Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

do

{

printf("Enter the client no to communicate\n");

scanf("%d",&i);

if(i==0)

exit(0);

printf("Client %d is connected\n",i);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

do

{

recv(nsd,rcvmsg,20,0);

printf("%s",rcvmsg);

fgets(sendmsg,20,stdin);

len=strlen(sendmsg);

sendmsg[len-1]='\0';

send(nsd,sendmsg,20,0);

wait(20);

}while(strcmp(sendmsg,"bye")!=0);

}while(i!=0);

}

CLIENT - 1

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len;

char sendmsg[20],revmsg[20];

struct sockaddr_in servaddr;

printf("Enter the port no:\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

printf("Can't Create\n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Can't Connect\n");

else

printf("Connected\n");

do

{

fgets(sendmsg,20,stdin);

len=strlen(sendmsg);

sendmsg[len-1]='\0';

send(csd,sendmsg,20,0);

wait(20);

recv(csd,revmsg,20,0);

printf("%s",revmsg);

}

while(strcmp(revmsg,"bye")!=0);

}

CLIENT - 2

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len;

char sendmsg[20],revmsg[20];

struct sockaddr_in servaddr;

printf("Enter the port no:\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

printf("Can't Create\n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Can't Connect\n");

else

printf("Connected\n");

do

{

fgets(sendmsg,20,stdin);

len=strlen(sendmsg);

sendmsg[len-1]='\0';

send(csd,sendmsg,20,0);

wait(20);

recv(csd,revmsg,20,0);

printf("%s",revmsg);

}

while(strcmp(revmsg,"bye")!=0);

}

OUTPUT:

SERVER SIDE:

[1me2@localhost ~]$ vi Multiuserserver.c

[1me2@localhost ~]$ cc Multiuserserverc

[1me2@localhost ~]$ ./a.out

Enter the port no

8543

socket is created

Binded

Enter the client to communicate: 1

Client 1 is connected

Accepted

hiiiii

Byeeeeee

Enter the client no to communicate: 2

client 2 is connected

Accepted

hiiiiiiiiii

hello

Enter the client no to communicate: 0

CLIENT SIDE 1:

[1me2@localhost ~]$ vi multiuserclient1.c

[1me2@localhost ~]$ cc multiuserclient1.c

[1me2@localhost ~]$ ./a.out

Enter the port no

8543

Socket is created

Connected

hiiiiii

Byeeeee

CLIENT SIDE –2:

[1me2@localhost ~]$ vi multiuserclient2.c

[1me2@localhost ~]$ cc multiuserclient2.c

[1me2@localhost ~]$ ./a.out

Enter the port no

8543

Socket is created

Connected

Hiiiiiiiii

hello

RESULT

Thus the C program for chat multiclient-serve chat program using tcp has been

executed successfully.

Ex. No:9 SIMULATION OF SIMPLE NETWORK

Date: MANAGEMENT PROTOCOLS

AIM:

To write a C program for simulation of Simple Network management Protocols.

ALGORITHM:

MANAGER:

Step 1: Start the program.

Step 2: Create an unnamed socket for client using socket ( ) system.

Step 3: Call with parameters AF_INET as domain and SOCK_STREAM as type.

Step 4: Name the socket using bind ( ) system call.

Step 5: Now connect the socket to agent using connect ( ) system call.

Step 6: Get the input for the type of information needed from the agent.

Step 7: If the input is equal to „TCP connection‟ then goto next step else If it is equal to

„system‟ Goto step 9.

Step 8: Read the input for the object, send it and receive the details of the TCP

connection of that object from the agent. Go to step 10.

Step 9: Read the input for the object, send it and receive the details of the system from

the agent. Go to step 10.

Step 10: Receive the message, print and terminate the process.

Step 11: Stop the process.

AGENTS

Step 1: Start the program.

Step 2: Create an unnamed socket for the server using the parameters AF_INET as

domain and the SOCK_STREAM as type.

Step 3: Name the socket using bind( ) system call with the parameters server_sockfd

and the manager address(sin_addr and sin_sport).

Step 4: Create a connection queue and wait for manager using the listen ( ) system call

with the number of manager request as parameters.

Step 5: Accept the connection using accept( ) system call when manager requests for

connection.

Step 6: Receive the message from the manager. If the request is for „TCP connections‟

then send the details of the requested object, else if the request is for „System‟ then send

the details of the requested system.

Step 7: Stop the program execution.

SIMPLE NETWORK MANAGEMENT PROTOCOL

AGENT1

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

int i,sd,sd2,nsd,clilen,sport,len;

char sendmsg[20],recvmsg[100];

char oid[5][10]={"client1","client2","client3","cleint4","client5"};

char wsize[5][5]={"5","10","15","3","6"};

struct sockaddr_in servaddr,cliaddr;

printf("I'm the Agent - TCP Connection\n");

printf("\nEnter the Server port");

printf("\n_____________________\n");

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

printf(" Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

recv(nsd,recvmsg,100,0);

for (i=0;i<5;i++)

{

if(strcmp(recvmsg,oid[i])==0)

{

send(nsd,wsize[i],100,0);

break;

}

}

}

AGENT 2

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<string.h>

main()

{

int i,sd,sd2,nsd,clilen,sport,len;

char sendmsg[20],recvmsg[100];

char oid[5][10]={"System1","System2","System3","System4","System5"};

char mdate[5][15]={"1-10-095","10-03-08","14.03.81","11.07.07","17.12.77"};

char time[5][15]={"9am","10pm","11am","12.30pm","11.30am"};

struct sockaddr_in servaddr,cliaddr;

printf("Enter the Server port");

printf("\n_____________________\n");

scanf("%d",&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)

printf("Can't Create \n");

else

printf("Socket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(sport);

sd2=bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));

if(sd2<0)

printf(" Can't Bind\n");

else

printf("\n Binded\n");

listen(sd,5);

clilen=sizeof(cliaddr);

nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)

printf("Can't Accept\n");

else

printf("Accepted\n");

recv(nsd,recvmsg,100,0);

for(i=0;i<5;i++)

{

if(strcmp(recvmsg,oid[i])==0)

{

send(nsd,mdate[i],100,0);

send(nsd,time[i],100,0);

break;

}

}

}

MANAGER

#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

main()

{

int csd,cport,len,i;

char sendmsg[20],rcvmsg[100],rmsg[100],oid[100];

struct sockaddr_in servaddr;

printf("Enter the port\n");

scanf("%d",&cport);

csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)

printf("Can't Create\n");

else

printf("Scocket is Created\n");

servaddr.sin_family=AF_INET;

servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)

printf("Can't Connect\n");

else

printf("Connected\n");

printf("\n 1.TCP Connection\n");

printf("\n 2. System \n");

printf("Enter the number for the type of informtion needed....\n");

scanf("%d",&i);

if(i==1)

{

printf("Enter the Object ID for Client\n");

scanf("%s",oid);

send(csd,oid,100,0);

recv(csd,rmsg,100,0);

printf("\n The window size of %s is %s",oid,rmsg);

}

else

{

printf("\nEnter the Object ID for the System\n");

scanf("%s",oid);

send(csd,oid,100,0);

recv(csd,rmsg,100,0);

printf("\nThe Manufacturing date for %s is %s",oid,rmsg);

recv(csd,rmsg,100,0);

printf("\nThe time of Last Utilization for %s is %s",oid,rmsg);

}

}

OUTPUT:

AGENT1:

[1me14@localhost ~]$ vi Agent1.c

[1me14@localhost ~]$ cc Agent1.c

[1me14@localhost ~]$ ./a.out

I'm the Agent - TCP Connection

Enter the Server port

_____________________

8543

Socket is created

Binded

Accepted

MANGER:

[1me14@localhost ~]$ vi Manager.c

[1me14@localhost ~]$ cc Manger.c

[1me14@localhost ~]$ ./a.out

Enter the port

8543

Socket is Created

Connected

1.TCP Connection

2. System

Enter the number for the type of information needed: 1

Enter the Object ID for Client: Client1

The window size of client1 is 5

AGENT2:

[1me14@localhost ~]$ vi Agent2.c

[1me14@localhost ~]$ cc Agent2.c

[1me14@localhost ~]$ ./a.out

Enter the Server port

_____________________

8543

Socket is Created

Binded

Accepted

MANGER:

[1me14@localhost ~]$ vi Manager.c

[1me14@localhost ~]$ cc Manger.c

[1me14@localhost ~]$ ./a.out

Enter the port

8543

Socket is Created

Connected

1.TCP Connection

2. System

Enter the number for the type of informtion needed: 2

Enter the Object ID for Client: System3

The Manufacturing date for system3 is 14.03.81

The time of last utilization for system3 is 11am

RESULT

Thus the C program for simple network management protocols has been executed

successfully.

EMAIL

#include <stdlib.h>

#include <string.h>

#define cknull(x) if((x)==NULL) {perror(""); exit(EXIT_FAILURE);}

#define cknltz(x) if((x)<0) {perror(""); exit(EXIT_FAILURE);}

#define LIST_LEN 4

//char *f="sam.txt";

void email_it(char *filename);

main()

{

char fname[15];

printf("enter the filename\n");

scanf("%s",fname);

email_it(fname);

}

void email_it(char *filename)

{

char tmp[256]={0x0};

char fpBuffer[400]={0x0};

char email_list[LIST_LEN][256]={{"[email protected]"},{0x0}};

int i=0;

for(i=0;*email_list[i]>0x0;i++)

{

cknull(strcpy(tmp, email_list[i]));

cknltz(sprintf (fpBuffer,"mail -s '%s %s' %s < %s",

"Please Review:", filename, tmp,filename));

if(system (fpBuffer)==(-1))

{

perror("email failure");

exit(EXIT_FAILURE);

}

}

}

OUTPUT:

[1me2@localhost ~]$ vi email.c

[1me2@localhost ~]$ cc email.c

[1me2@localhost ~]$ ./a.out

Enter the file name: sample.c

[1me2@localhost ~]$/home/1me1/dead.letter….saved message in

/home/1me1/dead.letter…..

RESULT

Thus the program for developing E-mail application is executed and the output is

verified successfully.

STUDY OF NETWORK SIMULATOR PACKAGES

Ex. No: STUDY OF NS2

Date:

AIM: To study about NS2 - Network Simulator

INTRODUCTION: NS is a discrete event simulator targeted at networking research. NS provides

substantial support for simulation of TCP, routing, and multicast protocols over wired

and wireless (local and satellite) networks. NS began as a variant of the REAL network

simulator in 1989 and has evolved substantially over the past few years. In 1995 ns

development was supported by DARPA through the VINT project at LBL, Xerox PARC,

UCB, and USC/ISI. Currently ns development is support through DARPA with SAMAN

and through NSF with CONSER, both in collaboration with other researchers including

ACIRI. NS has always included substantial contributions from other researchers,

including wireless code from the UCB Daedelus and CMU Monarch projects and Sun

Microsystems.

The network simulator ns-2 is a widely accepted discrete event network simulator,

actively used for wired and wireless network simulations. It has a highly detailed model

of the lower layers (Physical and MAC) of wireless IEEE 802.11 networks.

Ns-2 has also an emulation feature, i.e. the ability to introduce the simulator into a

live network and to simulate a desired network between real applications in real-time.

Within the scope of this project we developed some methods and extensions to

the ns-2 to combine wireless network simulation and network emulation.

OVERVIEW: NS is an event driven network simulator developed at UC Berkeley that simulates

variety of IP networks. It implements network protocols such as TCP and UDP, traffic

source behavior such as FTP, Telnet, Web, CBR and VBR, router queue management

mechanism such as Drop Tail, RED and CBQ, routing algorithms such as Dijkstra, and

more. NS also implements multicasting and some of the MAC layer protocols for LAN

simulations. The NS project is now a part of the VINT project that develops tools for

simulation results display, analysis and converters that convert network topologies

generated by well-known generators to NS formats. Currently, NS (version 2) written in

C++ and OTcl (Tcl script language with Object-oriented extensions developed at MIT) is

available. This document talks briefly about the basic structure of NS, and explains in

detail how to use NS mostly by giving examples. Most of the figures that are used in

describing the NS basic structure and network components are from the 5th VINT/NS

Simulator Tutorial/Workshop slides and the NS Manual (formerly called "NS Notes and

Documentation"), modified little bit as needed.

Figure 1. Simplified User's View of NS

As shown in Figure 1, in a simplified user's view, NS is Object-oriented Tcl

(OTcl) script interpreter that has a simulation event scheduler and network component

object libraries, and network setup (plumbing) module libraries (actually, plumbing

modules are implemented as member functions of the base simulator object). In other

words, to use NS, you program in OTcl script language. To setup and run a simulation

network, a user should write an OTcl script that initiates an event scheduler, sets up the

network topology using the network objects and the plumbing functions in the library,

and tells traffic sources when to start and stop transmitting packets through the event

scheduler. The term "plumbing" is used for a network setup, because setting up a network

is plumbing possible data paths among network objects by setting the "neighbor" pointer

of an object to the address of an appropriate object. When a user wants to make a new

network object, he or she can easily make an object either by writing a new object or by

making a compound object from the object library, and plumb the data path through the

object. This may sound like complicated job, but the plumbing OTcl modules actually

make the job very easy. The power of NS comes from this plumbing.

Another major component of NS beside network objects is the event scheduler.

An event in NS is a packet ID that is unique for a packet with scheduled time and the

pointer to an object that handles the event. In NS, an event scheduler keeps track of

simulation time and fires all the events in the event queue scheduled for the current time

by invoking appropriate network components, which usually are the ones who issued the

events, and let them do the appropriate action associated with packet pointed by the

event. Network components communicate with one another passing packets, however this

does not consume actual simulation time. All the network components that need to spend

some simulation time handling a packet (i.e. need a delay) use the event scheduler by

issuing an event for the packet and waiting for the event to be fired to itself before doing

further action handling the packet. For example, a network switch component that

simulates a switch with 20 microseconds of switching delay issues an event for a packet

to be switched to the scheduler as an event 20 microsecond later. The scheduler after 20

microseconds dequeues the event and fires it to the switch component, which then passes

the packet to an appropriate output link component. Another use of an event scheduler is

timer.

NS is written not only in OTcl but in C++ also. For efficiency reason, NS

separates the data path implementation from control path implementations. In order to

reduce packet and event processing time (not simulation time), the event scheduler and

the basic network component objects in the data path are written and compiled using

C++. These compiled objects are made available to the OTcl interpreter through an OTcl

linkage that creates a matching OTcl object for each of the C++ objects and makes the

control functions and the configurable variables specified by the C++ object act as

member functions and member variables of the corresponding OTcl object. In this way,

the controls of the C++ objects are given to OTcl. It is also possible to add member

functions and variables to a C++ linked OTcl object. The objects in C++ that do not need

to be controlled in a simulation or internally used by another object do not need to be

linked to OTcl. Likewise, an object (not in the data path) can be entirely implemented in

OTcl. Figure 2 shows an object hierarchy example in C++ and OTcl. One thing to note in

the figure is that for C++ objects that have an OTcl linkage forming a hierarchy, there is a

matching OTcl object hierarchy very similar to that of C++.

Figure 2. C++ and OTcl: The Duality

Figure 3. Architectural View of NS

Figure 3 shows the general architecture of NS. In this figure a general user (not an

NS developer) can be thought of standing at the left bottom corner, designing and

running simulations in Tcl using the simulator objects in the OTcl library. The event

schedulers and most of the network components are implemented in C++ and available to

OTcl through an OTcl linkage that is implemented using tclcl. The whole thing together

makes NS, which is a OO extended Tcl interpreter with network simulator libraries.

This section briefly examined the general structure and architecture of NS. At this

point, one might be wondering about how to obtain NS simulation results.

As shown in Figure 1, when a simulation is finished, NS produces one or more

text-based output files that contain detailed simulation data, if specified to do so in the

input Tcl (or more specifically, OTcl) script. The data can be used for simulation analysis

(two simulation result analysis examples are presented in later sections) or as an input to

a graphical simulation display tool called Network Animator (NAM) that is developed as

a part of VINT project. NAM has a nice graphical user interface similar to that of a CD

player (play, fast forward, rewind, pause and so on), and also has a display speed

controller. Furthermore, it can graphically present information such as throughput and

number of packet drops at each link, although the graphical information cannot be used

for accurate simulation analysis.

This section shows a simple NS simulation script and explains what each line does.

Example 3 is an OTcl script that creates the simple network configuration and runs the

simulation scenario in Figure 4.

Figure 4. A Simple Network Topology and Simulation Scenario

This network consists of 4 nodes (n0, n1, n2, n3) as shown in above figure. The

duplex links between n0 and n2, and n1 and n2 have 2 Mbps of bandwidth and 10 ms of

delay. The duplex link between n2 and n3 has 1.7 Mbps of bandwidth and 20 ms of

delay. Each node uses a DropTail queue, of which the maximum size is 10. A "tcp" agent

is attached to n0, and a connection is established to a tcp "sink" agent attached to n3. As

default, the maximum size of a packet that a "tcp" agent can generate is 1KByte. A tcp

"sink" agent generates and sends ACK packets to the sender (tcp agent) and frees the

received packets. A "udp" agent that is attached to n1 is connected to a "null" agent

attached to n3. A "null" agent just frees the packets received.

Example 3. A Simple NS Simulation Script

The following is the explanation of the script above. In general, an NS script starts

with making a Simulator object instance.

set ns [new Simulator]: generates an NS simulator object instance, and assigns it

to variable ns (italics is used for variables and values in this section).

o Initialize the packet format (ignore this for now)

o Create a scheduler (default is calendar scheduler)

o Select the default address format (ignore this for now)

The "Simulator" object has member functions that do the following:

Create compound objects such as nodes and links (described later)

Connect network component objects created (ex. attach-agent)

Set network component parameters (mostly for compound objects)

Create connections between agents (ex. make connection between a "tcp" and

"sink")

Specify NAM display options

Most of member functions are for simulation setup and scheduling, however some of

them are for the NAM display. The "Simulator" object member function implementations

are located in the "ns-2/tcl/lib/ns-lib.tcl" file.

$ns color fid color: is to set color of the packets for a flow specified by the flow id

(fid). This member function of "Simulator" object is for the NAM display, and

has no effect on the actual simulation.

$ns namtrace-all file-descriptor: This member function tells the simulator to

record simulation traces in NAM input format. It also gives the file name that the

trace will be written to later by the command $ns flush-trace. Similarly, the

member function trace-all is for recording the simulation trace in a general

format.

proc finish {}: is called after this simulation is over by the command $ns at 5.0

"finish". In this function, post-simulation processes are specified.

set n0 [$ns node]: The member function node creates a node. A node in NS is

compound object made of address and port classifiers (described in a later

section). Users can create a node by separately creating an address and a port

classifier objects and connecting them together. However, this member function

of Simulator object makes the job easier. To see how a node is created, look at the

files: "ns-2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-node.tcl".

$ns duplex-link node1 node2 bandwidth delay queue-type: creates two simplex

links of specified bandwidth and delay, and connects the two specified nodes. In

NS, the output queue of a node is implemented as a part of a link, therefore users

should specify the queue-type when creating links. In the above simulation script,

DropTail queue is used. If the reader wants to use a RED queue, simply replace

the word DropTail with RED. The NS implementation of a link is shown in a later

section. Like a node, a link is a compound object, and users can create its sub-

objects and connect them and the nodes. Link source codes can be found in "ns-

2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl" files. One thing to note is that

you can insert error modules in a link component to simulate a lossy link (actually

users can make and insert any network objects). Refer to the NS documentation to

find out how to do this.

$ns queue-limit node1 node2 number: This line sets the queue limit of the two

simplex links that connect node1 and node2 to the number specified. At this point,

the authors do not know how many of these kinds of member functions of

Simulator objects are available and what they are. Please take a look at "ns-

2/tcl/libs/ns-lib.tcl" and "ns-2/tcl/libs/ns-link.tcl", or NS documentation for more

information.

$ns duplex-link-op node1 node2 ...: The next couple of lines are used for the

NAM display. To see the effects of these lines, users can comment these lines out

and try the simulation.

Now that the basic network setup is done, the next thing to do is to setup traffic

agents such as TCP and UDP, traffic sources such as FTP and CBR, and attach them to

nodes and agents respectively.

set tcp [new Agent/TCP]: This line shows how to create a TCP agent. But in

general, users can create any agent or traffic sources in this way. Agents and

traffic sources are in fact basic objects (not compound objects), mostly

implemented in C++ and linked to OTcl. Therefore, there are no specific

Simulator object member functions that create these object instances. To create

agents or traffic sources, a user should know the class names these objects

(Agent/TCP, Agnet/TCPSink, Application/FTP and so on). This information can

be found in the NS documentation or partly in this documentation. But one

shortcut is to look at the "ns-2/tcl/libs/ns-default.tcl" file. This file contains the

default configurable parameter value settings for available network objects.

Therefore, it works as a good indicator of what kind of network objects are

available in NS and what are the configurable parameters.

$ns attach-agent node agent: The attach-agent member function attaches an agent

object created to a node object. Actually, what this function does is call the attach

member function of specified node, which attaches the given agent to itself.

Therefore, a user can do the same thing by, for example, $n0 attach $tcp.

$ns connect agent1 agent2: After two agents that will communicate with each

other are created, the next thing is to establish a logical network connection

between them. This line establishes a network connection by setting the

destination address to each others' network and port address pair.

Assuming that all the network configuration is done, the next thing to do is write a

simulation scenario (i.e. simulation scheduling). The Simulator object has many

scheduling member functions. However, the one that is mostly used is the following:

$ns at time "string": This member function of a Simulator object makes the

scheduler (scheduler_ is the variable that points the scheduler object created by

[new Scheduler] command at the beginning of the script) to schedule the

execution of the specified string at given simulation time. For example, $ns at 0.1

"$cbr start" will make the scheduler call a start member function of the CBR

traffic source object, which starts the CBR to transmit data. In NS, usually a

traffic source does not transmit actual data, but it notifies the underlying agent

that it has some amount of data to transmit, and the agent, just knowing how

much of the data to transfer, creates packets and sends them.

After all network configuration, scheduling and post-simulation procedure specifications

are done, the only thing left is to run the simulation. This is done by $ns run.

NETWORK COMPONENTS:

Figure 6. Class Hierarchy (Partial)

The root of the hierarchy is the TclObject class that is the superclass of all OTcl

library objects (scheduler, network components, timers and the other objects including

NAM related ones). As an ancestor class of TclObject, NsObject class is the superclass of

all basic network component objects that handle packets, which may compose compound

network objects such as nodes and links. The basic network components are further

divided into two subclasses, Connector and Classifier, based on the number of the

possible output data paths. The basic network objects that have only one output data path

are under the Connector class, and switching objects that have possible multiple output

data paths are under the Classifier class.

NODE AND ROUTING:

A node is a compound object composed of a node entry object and classifiers as

shown in Figure 7. There are two types of nodes in NS. A unicast node has an address

classifier that does unicast routing and a port classifier. A multicast node, in addition, has

a classifier that classify multicast packets from unicast packets and a multicast classifier

that performs multicast routing.

Figure 7. Node (Unicast and Multicast)

In NS, Unicast nodes are the default nodes. To create Multicast nodes the user

must explicitly notify in the input OTcl script, right after creating a scheduler object, that

all the nodes that will be created are multicast nodes. After specifying the node type, the

user can also select a specific routing protocol other than using a default one.

Unicast

- $ns rtproto type

- type: Static, Session, DV, cost, multi-path

Multicast

- $ns multicast (right after set $ns [new Scheduler])

- $ns mrtproto type

- type: CtrMcast, DM, ST, BST

LINK:

A link is another major compound object in NS. When a user creates a link using a

duplex-link member function of a Simulator object, two simplex links in both directions

are created as shown in Figure 8.

Figure 8. Link

One thing to note is that an output queue of a node is actually implemented as a

part of simplex link object. Packets dequeued from a queue are passed to the Delay object

that simulates the link delay, and packets dropped at a queue are sent to a Null Agent and

are freed there. Finally, the TTL object calculates Time To Live parameters for each

packet received and updates the TTL field of the packet.

Tracing

In NS, network activities are traced around simplex links. If the simulator is

directed to trace network activities (specified using $ns trace-all file or $ns namtrace-all

file), the links created after the command will have the following trace objects inserted as

shown in Figure 9. Users can also specifically create a trace object of type type between

the given src and dst nodes using the create-trace {type file src dst} command.

Figure 9. Inserting Trace Objects

When each inserted trace object (i.e. EnqT, DeqT, DrpT and RecvT) receives a

packet, it writes to the specified trace file without consuming any simulation time, and

passes the packet to the next network object. The trace format will be examined in the

General Analysis Example section.

Queue Monitor

Basically, tracing objects are designed to record packet arrival time at which they

are located. Although a user gets enough information from the trace, he or she might be

interested in what is going on inside a specific output queue. For example, a user

interested in RED queue behavior may want to measure the dynamics of average queue

size and current queue size of a specific RED queue (i.e. need for queue monitoring).

Queue monitoring can be achieved using queue monitor objects and snoop queue objects

as shown in Figure 10.

Figure 10. Monitoring Queue

When a packet arrives, a snoop queue object notifies the queue monitor object of

this event. The queue monitor using this information monitors the queue. A RED queue

monitoring example is shown in the RED Queue Monitor Example section. Note that

snoop queue objects can be used in parallel with tracing objects even though it is not

shown in the above figure.

PACKET FLOW EXAMPLE: Until now, the two most important network components (node and link) were

examined. Figure 11 shows internals of an example simulation network setup and packet

flow. The network consist of two nodes (n0 and n1) of which the network addresses are 0

and 1 respectively. A TCP agent attached to n0 using port 0 communicates with a TCP

sink object attached to n1 port 0. Finally, an FTP application (or traffic source) is

attached to the TCP agent, asking to send some amount of data.

Figure 11. Packet Flow Example

Note that the above figure does not show the exact behavior of a FTP over TCP. It

only shows the detailed internals of simulation network setup and a packet flow.

PACKET: A NS packet is composed of a stack of headers, and an optional data space (see

Figure 12). As briefly mentioned in the "Simple Simulation Example" section, a packet

header format is initialized when a Simulator object is created, where a stack of all

registered (or possibly useable) headers, such as the common header that is commonly

used by any objects as needed, IP header, TCP header, RTP header (UDP uses RTP

header) and trace header, is defined, and the offset of each header in the stack is recorded.

What this means is that whether or not a specific header is used, a stack composed of all

registered headers is created when a packet is allocated by an agent, and a network object

can access any header in the stack of a packet it processes using the corresponding offset

value.

Figure 12. NS Packet Format

Usually, a packet only has the header stack (and a data space pointer that is null).

Although a packet can carry actual data (from an application) by allocating a data space,

very few application and agent implementations support this. This is because it is

meaningless to carry data around in a non-real-time simulation. However, if you want to

implement an application that talks to another application cross the network, you might

want to use this feature with a little modification in the underlying agent implementation.

Another possible approach would be creating a new header for the application and

modifying the underlying agent to write data received from the application to the new

header. The second approach is shown as an example in a later section called "Add New

Application and Agent".

RESULT: Thus the details about NS2(Network Simulator 2) has been studied.

Ex. No. STUDY OF OPNET

Date:

AIM: To study about OPNET - Network Simulator

INTRODUCTION: OPNET (Optimized Network Engineering Tools) is a commercial tool from MIL3

Inc. It is being developed for almost 15 years. As everyone should guess, no much

technical detail are available about the internals.

USE:

Network with several hundreds of nodes can be simulated, but it would take time

for the computation. OPNET is used by companies like Thomson-CSF or CNET which

use it to model ATM networks and validate various layers protocols, packet switched

radio networks. An example of use of OPNET is George Mason University (Quality of

Service IP Network Simulation).

THE PACKAGE

The software comprises several tools and is divided in several parts, OPNET

Modeler and OPNET Planner, the Model Library, and the Analysis tool. Features

included in this generic simulator are an event-driven scheduled simulation kernel,

integrated analysis tools for interpreting and synthesizing output data, graphical

specification of models and a hierarchical object-based modeling.

OPNET Modeler is intended for modeling, simulating and analyzing the

performance of large communications networks, computer systems and applications.

Common uses are assessing and feasibility of new designs, optimizing already developed

communication systems and predicting performance.

The modeling methodology of OPNET is organized in a hierarchical structure. At

the lowest level, Process models are structured as a finite state machine. State and

transitions are specified graphically using state-transition diagrams whereas conditions

that specify what happen within each state are programmed with a C-like language called

Proto-C. Those processes and built-in modules in OPNET (source and destination

modules, traffic generators, queues, ...) are then configured with menus and organized

into data flow diagrams that represent nodes using the graphical Node Editor. Using a

graphical Network Editor, nodes and links are selected to build up the topology of a

communication network.

The Analysis Tool provides a graphical environment to view and manipulate data

collected during simulation runs. Results can be analyzed for any network element.

OPNET Planner is an application that allows administrators to evaluate the

performance of communications networks and distributed systems, without programming

or compiling. Planner analyses behavior and performance by discrete-event simulations.

Models are built using a graphical interface. The user only chooses pre-defined models

(from the physical layer to the application) from the library and sets attributes. The user

cannot define new models, he should contact MIL3's modeling service.

The modeling libraries are included with OPNET Modeler and OPNET Planner

and contains protocols and analysis environments, among them ATM, TCP, IP, Frame

Relay, FDDI, Ethernet, link models such as point-to-point or bus, queueing service

disciplines such as First-in-First-Out (FIFO), Last-In-First-Out (LIFO), priority non-

preemptive queueing, shortest first job, round-robin or preempt and resume.

OPNET Modeler is the industry's leading environment for network modeling and

simulation, allowing you to design and study communication networks, devices,

protocols, and applications with unmatched flexibility and scalability. Modeler is used by

the world's largest network equipment manufacturers to accelerate the R&D of network

devices and technologies such as VoIP, TCP, OSPFv3, MPLS, IPv6, and more.

Networking technology has become too complex for traditional analytical

methods or "rules of thumb" to yield an accurate understanding of system behavior.

Since 1986, OPNET Technologies Inc., has been the leader in developing

predictive software solutions for networking professionals. OPNET software enables its

users to optimize the performance and maximize the availability of communications

networks and applications.

OPNET is the state-of-art network simulation tool for modeling, simulating and analysing

the performance of

i. Communication Networks, Distributed Systems

ii. Computer systems and Applications.

Product modules provide value-added capabilities to OPNET's intelligent network

management software. Modules span a wide range of applications and receive regular

updates under OPNET's maintenance program.

OPNET MODULES:

Modeler

Terrain Modeling Module (TMM)

High Level Architecture (HLA)

MODELER:

OPNET Modeler is intended for modeling, simulating and analysing the

performance of large communications networks, computer systems and applications.

Common uses are assessing and feasibility of new designs, optimizing already developed

communication systems and predicting performance.

The modeling methodology of OPNET is organized in a hierarchical structure. At

the lowest level, Process models are structured as a finite state machine. State and

transitions are specified graphically using state-transition diagrams whereas conditions

that specify what happen within each state are programmed with a C-like language called

Proto-C. Those processes, and built-in modules in OPNET (source and destination

modules, traffic generators, queues, ...) are then configured with menus and organized

into data flow diagrams that represent nodes using the graphical Node Editor. Using a

graphical Network Editor, nodes and links are selected to build up the topology of a

communication network.

TERRAIN MODELING MODULE (TMM)

Building on the capabilities of OPNET's Wireless Module, the Terrain Modeling

Module provides the next level of accuracy and control for wireless network design and

planning. This module allows you to model environmental effects on wireless network

communications anywhere.

Featuring an open-source Longley-Rice model, the Terrain Modeling Module is

ready to replicate any known atmospheric or topological conditions and their combined

impact on signal propagation. Create elevation maps, line-of-sight profiles, and signal-

power comparisons of mobile network models. Interfaces to read DTED and USGS DEM

terrain data are provided. Open and flexible, the Terrain Modeling Module is supported

by the standard Radio Transceiver Pipeline, which enables easy implementation of

custom propagation models

HIGH LEVEL ARCHITECTURE (HLA)

The High-Level Architecture (HLA) Module supports building and running a

federation of many simulators, each modeling some aspect of a composite system. The

OPNET HLA Module enables OPNET simulations to model some portion (or the

entirety) of the communications aspects of the HLA federation models. The OPNET-

HLA interface provides the various simulators (federates) the necessary mechanisms to

share a common object representation (for persistent elements), to exchange messages

(interactions), and to maintain appropriate time synchronization.

The module supports a large subset of the HLA interface, including:

Time management such that an OPNET simulation remains synchronized with

overall HLA time

Generation and reception of RTI interactions using OPNET packet

Creation, destruction, or discovery of RTI objects during the simulation

Generation of RTI object attribute updates based on changes to OPNET attribute

values

Automatic updates of OPNET attributes upon reception of RTI object attribute

updates

A user-defined mapping specifying the relation between RTI and OPNET objects

RESULT:

Thus the Network Simulator-OPNET has been studied.