You are on page 1of 33

Dr.

Ambedkar Institute of Technology


(Near Jnana Bharathi Campus, Mallathahalli, Bengaluru – 560056)
Department of Computer Science & Engineering

“COMPUTER NETWORKS”
(21CST502)
Laboratory Record

Department of CSE, Dr AIT Page | 1


Dr. Ambedkar Institute of Technology
(Near Jnana Bharathi Campus, Mallathahalli, Bengaluru – 560056)

Department of Computer Science & Engineering

Certificate
This is to certify that SNEHA SATYAPPA MUCHANDI [1DA21CS137] has satisfactorily
completed the course of experiments in practical “Computer Networks (21CST502)”
prescribed by the Dr. Ambedkar Institute of Technology, university VTU in the Laboratory
of this college in the year 2023-2024.

Signature of the Teacher in charge of Lab


Date:

Department of CSE, Dr AIT Page | 2


TABLE OF CONTENTS

Sl No Experiment Page No
1 Topology (Bus, 4-9
Ring, Star)
2 Simulate a Simple Topology-TCP/UDP Traffic 10-13
3 Hamming Distance Program 14
4 CRC (Cyclic Redundancy Check) Program 15-17
5 Stop and Wait Program 18-19
6 Client and Server Program 20-24
7 Simulation of Link State Routing Algorithm 25-27
8 Simulation of Distance Vector Routing 28-30
Algorithm

Program 1: Topology Program.

Department of CSE, Dr AIT Page | 3


Bus Topology:
#Create a simulator object

set ns [new Simulator]

#Open the nam trace file set

nf [open out.nam w]

$ns namtrace-all $nf

#Define a 'finish' procedure

proc finish {} { global ns nf

$ns flush-trace

#Close the trace file

close $nf

#Executenam on the trace file exec

nam out.nam &

exit 0

#Create five nodes

set n0 [$ns node] set

n1 [$ns node] set n2

[$ns node] set n3

[$ns node] set n4

[$ns node]

#Create Lan between the nodes

set lan0 [$ns newLan "$n0 $n1 $n2 $n3 $n4" 0.5Mb 40ms LL Queue/DropTail MAC/Csma/Cd Channel]
#set lan0 [$ns newLan “$node0 $node1$node2 $node3 $node4” 0.5Mb 40ms LL Queue/FQ MAC/Csma/Cd
Channel]

#Create a TCP agent and attach it to node n0

set tcp0 [new Agent/TCP] $tcp0 set class_ 1

$ns attach-agent $n1 $tcp0

#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3

set sink0 [new Agent/TCPSink]

Department of CSE, Dr AIT Page | 4


$ns attach-agent $n3 $sink0

#Connect the traffic sources with the traffic sink

$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0 set

cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.01

$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

#Run the simulation

$ns run

Output:

Ring Topology:

Department of CSE, Dr AIT Page | 5


#Create a simulator object

set ns [new Simulator]

#Open the nam trace file

set nf [open out.nam w] $ns

namtrace-all $nf #Define a

'finish' procedure proc

finish {} { global ns nf

$ns flush-trace

#Close the trace file

close $nf

#Execute nam on the trace file

exec nam out.nam &

exit0

#Create five nodes

set n0 [$ns node] set

n1 [$ns node] set n2

[$ns node] set n3

[$ns node] set n4

[$ns node] set n5

[$ns node]

#Create links between the nodes

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n2 $n3 1Mb 10ms DropTail

$ns duplex-link $n3 $n4 1Mb 10ms DropTail

$ns duplex-link $n4 $n5 1Mb 10ms DropTail

$ns duplex-link $n5 $n0 1Mb 10ms DropTail

#Create a TCP agent and attach it to node n0

Department of CSE, Dr AIT Page | 6


set tcp0 [new Agent/TCP] $tcp0 set class_ 1

$ns attach-agent $n1 $tcp0

#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3 set

sink0 [new Agent/TCPSink]

$ns attach-agent $n3 $sink0

$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0 set

cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.01

$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

#Run the simulation

$ns run

Output:

Star Topology:

Department of CSE, Dr AIT Page | 7


#Create a simulator object

set ns [new Simulator]

#Open the nam trace file

set nf [open out.nam w]

$ns namtrace-all $nf

#Define a 'finish' procedure

proc finish {} {

global ns nf

$ns flush-trace

#Close the trace file

close $nf

#Execute nam on the trace file

exec nam out.nam &

exit 0 }

#Create six nodes

set n0 [$ns node] set

n1 [$ns node] set n2

[$ns node] set n3

[$ns node] set n4

[$ns node] set n5

[$ns node]

#Change the shape of center node in a star topology

$n0 shape square

#Create links between the nodes

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

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

$ns duplex-link $n0 $n3 1Mb 10ms DropTail

$ns duplex-link $n0 $n4 1Mb 10ms DropTail

Department of CSE, Dr AIT Page | 8


$ns duplex-link $n0 $n5 1Mb 10ms DropTail

#Create a TCP agent and attach it to node n0

set tcp0 [new Agent/TCP]

$tcp0 set class_ 1

$ns attach-agent $n1 $tcp0

#Create a TCP Sink agent (a traffic sink) for TCP and attach it to node n3

set sink0 [new Agent/TCPSink]

$ns attach-agent $n3 $sink0

#Connect the traffic sources with the traffic sink

$ns connect $tcp0 $sink0

# Create a CBR traffic source and attach it to tcp0 set

cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.01

$cbr0 attach-agent $tcp0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$ns at 4.5 "$cbr0 stop"

#Call the finish procedure after 5 seconds of simulation time

$ns at 5.0 "finish"

$ns run

Output:

Department of CSE, Dr AIT Page | 9


Department of CSE, Dr AIT Page | 10
Program 2: Write and Execute a four node point-to-point network with the
links connected as follows: n0-n2,n1-n2 and n2-n3.Apply TCP agent between
n0-n3 and FTP between n1-n3.
Apply relevant applications over TCP and FTP agents changing the parameter
and determine the number of packets sent by TCP/FTP.

# Create a simulator object

set ns [new Simulator]

# Define different colors

# for data flows (for NAM)

$ns color 1 Blue

$ns color 2 Red


# Open the NAM trace file

set nf [open out.nam w]

$ns namtrace-all $nf


# Define a 'finish' procedure

proc finish {} {

global ns nf

$ns flush-trace
# Close the NAM trace file
close $nf
# Execute NAM on the trace file

exec nam out.nam &

exit 0

}
# Create four nodes

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

Department of CSE, Dr AIT Page | 11


# Create links between the nodes

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

$ns duplex-link $n1 $n2 2Mb 10ms DropTail

$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail


# Set Queue Size of link (n2-n3) to 10

$ns queue-limit $n2 $n3 10


# Give node position (for NAM)

$ns duplex-link-op $n0 $n2 orient right-down

$ns duplex-link-op $n1 $n2 orient right-up

$ns duplex-link-op $n2 $n3 orient right


# Monitor the queue for link (n2-n3). (for NAM)

$ns duplex-link-op $n2 $n3 queuePos 0.5


# Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

$ns connect $tcp $sink

$tcp set fid_ 1


# Setup a FTP over TCP connection

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP

# Setup a UDP connection

set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n3 $null

$ns connect $udp $null

$udp set fid_ 2


Department of CSE, Dr AIT Page | 12
# Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

$cbr set type_ CBR

$cbr set packet_size_ 1000

$cbr set rate_ 1mb

$cbr set random_ false


# Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"

$ns at 1.0 "$ftp start"

$ns at 4.0 "$ftp stop"

$ns at 4.5 "$cbr stop"


# Detach tcp and sink agents

# (not really necessary)

$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
# Call the finish procedure after

# 5 seconds of simulation time

$ns at 5.0 "finish"

# Print CBR packet size and interval puts

"CBR packet size = [$cbr set packet_size_]" puts

"CBR interval = [$cbr set interval_]"

# Run the simulation

$ns run

Department of CSE, Dr AIT Page | 13


Output:

Department of CSE, Dr AIT Page | 14


Program 3: Hamming Distance Program
#include<stdio.h>

#include<iostream.h>
#include<string.h>
int main() {
int i, length, count = 0;
char v1[8], v2[8];
clrscr();

printf("\nWrite a Program to Find Hamming Distance\n");

printf("Enter 1st String : ");


scanf("%s", &v1);
printf("Enter 2nd String : ");
scanf("%s", &v2);
length = strlen(v2);
for(i = 0; i < length; i++)
{
if(v1[i] != v2[i])
count++;
}

printf("\nHamming Distance : %d", count);


getch();
}

Output:

Department of CSE, Dr AIT Page | 15


Program 4: CRC (Cyclic Redundancy Check) Program.
#include<stdio.h>

#include<string.h>

// length of the generator polynomial

#define N strlen(gen_poly)

// data to be transmitted and

received char data[28];

// CRC value char

check_value[28];

// generator polynomial char

gen_poly[10];

// variables

int data_length,i,j;

// function that performs XOR operation

void XOR(){

for(j = 1;j < N; j++)

check_value[j] = (( check_value[j] == gen_poly[j])?'0':'1');

// Function to check for errors on the receiver

void receiver(){

// get the received data

printf("Enter the received data: ");

scanf("%s", data);

printf("\n-----------------------------\n");

printf("Data received: %s", data);

// Cyclic Redundancy Check

crc();

// Check if the remainder is zero to find the error

for(i=0;(i<N-1) && (check_value[i]!='1');i++);

Department of CSE, Dr AIT Page | 16


if(i<N-1) printf("\nError detected\n\n");

else printf("\nNo error detected\n\n");

void crc(){

// initializing check_value

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

check_value[i]=data[i];

do{

// check if the first bit is 1 and calls XOR function

if(check_value[0]=='1')

XOR();

// Move the bits by 1 position for the next computation

for(j=0;j<N-1;j++)

check_value[j]=check_value[j+1];

// appending a bit from data

check_value[j]=data[i++];

while(i<=data_length+N-1);

// loop until the data ends

}
int main()
{

// get the data to be transmitted

printf("\nEnter data to be transmitted: ");

scanf("%s",data);

printf("\n Enter the Generating polynomial: ");

// get the generator polynomial

scanf("%s",gen_poly);

Department of CSE, Dr AIT Page | 17


// find the length of data

data_length=strlen(data);

// appending n-1 zeros to the data

for(i=data_length;i<data_length+N-1;i++)

data[i]='0'; printf("\n----------------------------------------");

// print the data with padded zeros

printf("\n Data padded with n-1 zeros : %s",data);

printf("\n----------------------------------------");

crc();

// print the computed check value

printf("\nCRC or Check value is : %s",check_value);

// Append data with check_value(CRC)

for(i=data_length;i<data_length+N-1;i++)

data[i]=check_value[i-data_length];

printf("\n----------------------------------------");

// printing the final data to be sent

printf("\n Final data to be sent : %s",data);

receiver();

return 0;

Output:

Department of CSE, Dr AIT Page | 18


Department of CSE, Dr AIT Page | 19
Program 5: Stop and Wait Program.
set ns [new Simulator]

$ns color 1 Blue

# set nam output file

set nf [open out.nam w]

$ns namtrace-all $nf

# destructor proc

finish {} {

global ns nf

$ns flush-trace

close $nf

exec nam out.nam &

exit 0

#create two nodes and create labels for them

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

$ns at 0.0 "$n0 label \" Sender \" "

$ns at 0.0 "$n1 label \"Receiver\" "

# set up a new duplex link

$ns duplex-link $n0 $n1 1Mb 200ms DropTail

$ns duplex-link-op $n0 $n1 orient right

# create a new TCP agent

set tcp [new Agent/TCP]

# attach the agent to first node

$ns attach-agent $n0 $tcp

$tcp set fid_ 1

$tcp set window_ 1

$tcp set maxcwnd_ 1

$ns add-agent-trace $tcp tcp

Department of CSE, Dr AIT Page | 20


$ns monitor-agent-trace $tcp set

tcpsink [new Agent/TCPSink]

$ns attach-agent $n1 $tcpsink

$ns connect $tcp $tcpsink

set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ns at 0.5 "$ftp start"

$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $tcpsink "

$ns at 1.0 "$ns trace-annotate \"send packet 1\""

$ns at 1.4 "$ns trace-annotate \"recieve ack 1\""

$ns at 2.0 "$ns trace-annotate \"send packet 2\""

$ns at 2.5 "$ns trace-annotate \"receive ack 2\""

$ns at 3.2 "$ns trace-annotate \"send packet 3\""

$ns at 3.5 "$ns trace-annotate \"receive ack 3\""

$ns at 3.8 "$ns trace-annotate \"send packet 4\""

$ns at 4.0 "finish"

$ns run

Output:

Department of CSE, Dr AIT Page | 21


Program 6: Client and Server Program.
Server-Side Program:
#include <stdio.h>

#include <string.h>

#include <sys/socket.h>

#include <arpa/inet.h>

int main(void)

int socket_desc, client_sock, client_size;

struct sockaddr_in server_addr, client_addr;

char server_message[2000], client_message[2000];

// Clean buffers:

memset(server_message, '\0', sizeof(server_message));

memset(client_message, '\0', sizeof(client_message));

// Create socket:

socket_desc = socket(AF_INET, SOCK_STREAM, 0);


if(socket_desc < 0)
{
printf("Error while creating socket\n");

return -1;

printf("Socket created successfully\n");


// Set port and IP:
server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(2000);

server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

// Bind to the set port and IP:

if(bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr))<0)

{
Department of CSE, Dr AIT Page | 22
printf("Couldn't bind to the port\n");

return -1;

printf("Done with binding\n");


// Listen for clients:
if(listen(socket_desc, 1) < 0)

printf("Error while listening\n");

return -1;

printf("\nListening for incoming connections.....\n");

// Accept an incoming connection:


client_size = sizeof(client_addr);
client_sock = accept(socket_desc, (struct sockaddr*)&client_addr, &client_size);
if (client_sock < 0)
{
printf("Can't accept\n");
return -1;
}
printf("Client connected at IP: %s and port: %i\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

// Receive client's message:


if (recv(client_sock, client_message, sizeof(client_message), 0) < 0)
{
printf("Couldn't receive\n");
return -1;
}

printf("Msg from client: %s\n", client_message);

// Respond to client:

strcpy(server_message, "This is the server's message.");

if (send(client_sock, server_message, strlen(server_message), 0) < 0){

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

return -1;

Department of CSE, Dr AIT Page | 23


}
// Closing the socket:
close(client_sock);
close(socket_desc);
return 0;
}

Client-Side Program:
#include <stdio.h>

#include <string.h>

#include <sys/socket.h>

#include <arpa/inet.h>

int main(void)

int socket_desc, client_sock, client_size;

struct sockaddr_in server_addr, client_addr;

char server_message[2000], client_message[2000];

// Clean buffers:

memset(server_message, '\0', sizeof(server_message));

memset(client_message, '\0', sizeof(client_message));

// Create socket:

socket_desc = socket(AF_INET, SOCK_STREAM, 0);


if(socket_desc < 0){
printf("Error while creating socket\n");

return -1;

printf("Socket created successfully\n");

// Set port and IP:

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(2000);

server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

Department of CSE, Dr AIT Page | 24


// Bind to the set port and IP:

if(bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr))<0)

printf("Couldn't bind to the port\n");

return -1;

printf("Done with binding\n");

// Listen for clients:

if(listen(socket_desc, 1) < 0)

{ printf("Error while listening\n");

return -1;

printf("\nListening for incoming connections.....\n");


// Accept an incoming connection:
client_size = sizeof(client_addr);
client_sock = accept(socket_desc, (struct sockaddr*)&client_addr, &client_size);
if (client_sock < 0){
printf("Can't accept\n");
return -1;
}
printf("Client connected at IP: %s and port: %i\n", inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port))

// Receive client's message:


if (recv(client_sock, client_message, sizeof(client_message), 0) < 0)
{
printf("Couldn't receive\n");
return -1;
}

printf("Msg from client: %s\n", client_message);

// Respond to client:

strcpy(server_message, "This is the server's message.");


if (send(client_sock, server_message, strlen(server_message), 0) < 0)
{
printf("Can't send\n");
return -1;
}

Department of CSE, Dr AIT Page | 25


// Closing the socket:

close(client_sock);

close(socket_desc);

return 0;

Output:

Department of CSE, Dr AIT Page | 26


Program 7: Simulation of Link State Routing Algorithm.
set ns [new Simulator]
$ns rtproto LS

set node1 [$ns node]


set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
set node5 [$ns node]
set node6 [$ns node]
set node7 [$ns node]

set tf [open out.tr w]


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

$node1 label "node 1"


$node2 label "node 2"
$node3 label "node 3"
$node4 label "node 4"
$node5 label "node 5"
$node6 label "node 6"
$node7 label "node 7"

$node1 label-color blue


$node2 label-color red
$node3 label-color red
$node4 label-color blue
$node5 label-color blue
$node6 label-color blue
$node7 label-color blue

$ns duplex-link $node1 $node2 1.5Mb 10ms DropTail


$ns duplex-link $node2 $node3 1.5Mb 10ms DropTail
$ns duplex-link $node3 $node4 1.5Mb 10ms DropTail
$ns duplex-link $node4 $node5 1.5Mb 10ms DropTail
$ns duplex-link $node5 $node6 1.5Mb 10ms DropTail
$ns duplex-link $node6 $node7 1.5Mb 10ms DropTail
$ns duplex-link $node7 $node1 1.5Mb 10ms DropTail

$ns duplex-link-op $node1 $node2 orient left-down


$ns duplex-link-op $node2 $node3 orient left-down
$ns duplex-link-op $node3 $node4 orient right-down
$ns duplex-link-op $node4 $node5 orient right

Department of CSE, Dr AIT Page | 27


$ns duplex-link-op $node5 $node6 orient right-up
$ns duplex-link-op $node6 $node7 orient left-up
$ns duplex-link-op $node7 $node1 orient left-up

set tcp2 [new Agent/TCP]


$ns attach-agent $node1 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $node4 $sink2
$ns connect $tcp2 $sink2

set traffic_ftp2 [new Application/FTP]


$traffic_ftp2 attach-agent $tcp2

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

$ns at 0.5 "$traffic_ftp2 start"


$ns rtmodel-at 1.0 down $node2 $node3
$ns rtmodel-at 2.0 up $node2 $node3
$ns at 3.0 "$traffic_ftp2 start"
$ns at 4.0 "$traffic_ftp2 stop"
$ns at 5.0 "finish"
$ns run

Output:

Department of CSE, Dr AIT Page | 28


Department of CSE, Dr AIT Page | 29
Program 8: Simulation of Distance Vector Program Routing Algorithm.
set ns [new Simulator]

$ns rtproto DV

set node1 [$ns node]


set node2 [$ns node]
set node3 [$ns node]
set node4 [$ns node]
set node5 [$ns node]
set node6 [$ns node]
set node7 [$ns node]

set tf [open out.tr w]


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

$node1 label "node 1"


$node2 label "node 2"
$node3 label "node 3"
$node4 label "node 4"
$node5 label "node 5"
$node6 label "node 6"
$node7 label "node 7"

$node1 label-color blue


$node2 label-color red
$node3 label-color red
$node4 label-color blue
$node5 label-color blue
$node6 label-color blue
$node7 label-color blue

$ns duplex-link $node1 $node2 1.5Mb 10ms DropTail


$ns duplex-link $node2 $node3 1.5Mb 10ms DropTail
$ns duplex-link $node3 $node4 1.5Mb 10ms DropTail
$ns duplex-link $node4 $node5 1.5Mb 10ms DropTail
$ns duplex-link $node5 $node6 1.5Mb 10ms DropTail
$ns duplex-link $node6 $node7 1.5Mb 10ms DropTail
$ns duplex-link $node7 $node1 1.5Mb 10ms DropTail

$ns duplex-link-op $node1 $node2 orient left-down


$ns duplex-link-op $node2 $node3 orient left-down
$ns duplex-link-op $node3 $node4 orient right-down
$ns duplex-link-op $node4 $node5 orient right
Department of CSE, Dr AIT Page | 30
$ns duplex-link-op $node5 $node6 orient right-up
$ns duplex-link-op $node6 $node7 orient left-up
$ns duplex-link-op $node7 $node1 orient left-up

set tcp2 [new Agent/TCP]


$ns attach-agent $node1 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $node4 $sink2
$ns connect $tcp2 $sink2

set traffic_ftp2 [new Application/FTP]


$traffic_ftp2 attach-agent $tcp2
proc finish {} {
global ns nf $ns flush-trace
close $nf
exec nam out.nam &
exit 0
}

$ns at 0.5 "$traffic_ftp2 start"


$ns rtmodel-at 1.0 down $node2 $node3
$ns rtmodel-at 2.0 up $node2 $node3
$ns at 3.0 "$traffic_ftp2 start"
$ns at 4.0 "$traffic_ftp2 stop"
$ns at 5.0 "finish"
$ns run

Output:

Department of CSE, Dr AIT Page | 31


Department of CSE, Dr AIT Page | 32
Department of CSE, Dr AIT Page | 33

You might also like