Computer Networks Laboratory
Computer Networks Laboratory
Lab Experiment 1 :
Implement three nodes point – to – point network with duplex links between them. Set the queue
size, vary the bandwidth and find the number of packets dropped.
Topology-
Source/udp0
n0
n2 n3
router Destination/null
n1
Source/udp1
Code –
#Create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#label nodes
$n0 label "Source/udp0"
$n1 label "Source/udp1"
$n2 label "Router"
$n3 label "Destination/null"
#create links, specify the type, nodes, bandwidth, delay and ARQ algorithm for it
$ns duplex-link $n0 $n2 10Mb 300ms DropTail
$ns duplex-link $n1 $n2 10Mb 300ms DropTail
#set udp0 packet to red color and udp1 packet to blue color
$udp0 set class_ 1
$udp1 set class_ 2
#finish procedure
proc finish { } {
global ns nf nt
$ns flush-trace
exec nam lab1.nam &
close $nt
close $nf
exit 0
}
Awk file-
BEGIN{
count=0;
}
{
if($1=="r")
count++
}
END{
printf("Number of packets dropped is = %d\n",count);
}
Output-
$awk -f numDrop.awk lab1.tr
Number of packets dropped is = 714
Simulation-
Trace File-
Lab Experiment 2 :
Implement transmission of ping messages/trace route over a network topology consisting of 6 nodes and
find the number of packets dropped due to congestion.
Topology- Ping3
n3
Ping0 Ping4
n0 n4
n5
router
n2
n1 Ping1
Ping2
Code-
#create 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]
#label nodes
$n0 label "ping0"
$n1 label "ping1"
$n2 label "ping2"
$n3 label "ping3"
$n4 label "ping4"
$n5 label "router"
#create links, specify the type, nodes, bandwidth, delay and ARQ algorithm for it
$ns duplex-link $n0 $n5 1Mb 10ms DropTail
$ns duplex-link $n1 $n5 1Mb 10ms DropTail
$ns duplex-link $n2 $n5 1Mb 10ms DropTail
$ns duplex-link $n3 $n5 1Mb 10ms DropTail
$ns duplex-link $n4 $n5 1Mb 10ms DropTail
proc sendPingPacket { } {
global ns p2 p3
proc finish { } {
global ns nt nf
$ns flush-trace
close $nt
close $nf
exec nam prac2.nam &
exit 0
}
Awk file-
BEGIN{
count=0;
}
{
if($1=="r")
count++
}
END{
printf("Number of packets dropped is = %d\n",count);
}
Output-
$ns lab2.tcl
node 3 received ping answer from 4 with round-trip time 66.3 ms
node 3 received ping answer from 4 with round-trip time 66.8 ms
node 3 received ping answer from 4 with round-trip time 66.3 ms
node 3 received ping answer from 4 with round-trip time 66.9 ms
node 3 received ping answer from 4 with round-trip time 66.4 ms
node 3 received ping answer from 4 with round-trip time 66.9 ms
node 3 received ping answer from 4 with round-trip time 66.4 ms
node 3 received ping answer from 4 with round-trip time 66.9 ms
node 3 received ping answer from 4 with round-trip time 66.4 ms
node 3 received ping answer from 4 with round-trip time 66.9 ms
node 3 received ping answer from 4 with round-trip time 66.4 ms
node 3 received ping answer from 4 with round-trip time 67.0 ms
node 3 received ping answer from 4 with round-trip time 66.5 ms
node 3 received ping answer from 4 with round-trip time 67.0 ms
node 3 received ping answer from 4 with round-trip time 66.5 ms
node 3 received ping answer from 4 with round-trip time 67.0 ms
node 3 received ping answer from 4 with round-trip time 66.5 ms
Simulation-
Trace File-
Lab Experiment 3:
Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot congestion window for
different source / destination.
Topology-
TCP/FTP
n0 TCPSink/TELNET
n2 n3 n5
n1
TCP/TELNET
n4
TCPSink/FTP
Code-
#set ns Simulator
set ns [new Simulator]
#create 6 nodes
set n0 [$ns node]
set n1 [$ns node]
set lan [$ns newLan "$n3 $n4 $n5" 0.5Mb 40ms LL Queue/DropTail MAC/802_3]
$ns run
Simulation-
Trace File-
Congestion graph-
Lab Program 4 :
Write a program for error detecting code using CRC-CCITT (16- bits).
Code –
import java.util.Scanner;
String copy,rec,code,zero="0000000000000000";
n=code.length();
copy=code;
code+=zero;
code=crc.divide(code);
System.out.println("dataword="+copy);
copy=copy.substring(0,n)+code.substring(n);
System.out.print("CRC=");
System.out.println(code.substring(n));
if(zero.equals(crc.divide(rec).substring(n)))
System.out.println("correct bits received");
else
System.out.println("received frame contains one or more error");
sc.close();
}
int i,j;
char x;
for(i=0;i<n;i++)
{
x=s.charAt(i);
for(j=0;j<17;j++)
{
if(x=='1')
{
if(s.charAt(i+j)!=div.charAt(j))
s=s.substring(0,i+j)+"1"+s.substring(i+j+1);
else
s=s.substring(0,i+j)+"0"+s.substring(i+j+1);
}
}
}
return s;
}
}
Output 1 –
enter the dataword to be sent
1100
dataword=1100
CRC=1100000110001100
transmitted frame is=11001100000110001100
enter received data:
1100110000010001100
received frame contains one or more error
Output 2 –
enter the dataword to be sent
1100
dataword=1100
CRC=1100000110001100
transmitted frame is=11001100000110001100
enter received data:
11001100000110001100
correct bits received
Output 3 –
enter the dataword to be sent
1101
dataword=1101
CRC=1101000110101101
transmitted frame is=11011101000110101101
enter received data:
11011001000110110010
received frame contains one or more error
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
Output:
Sending packet 0
Sending packet 1
Sending packet 2
Sending packet 3
Sending packet 4
Sending packet 5
Sending packet 6
Sending packet 7
Received ACK for packet 7
Sending packet 8
Sending packet 9
Lab Program 6 :
Write a program to find the shortest path between vertices using bellman-ford and path vector routing
algorithm.
Code –
import java.util.Scanner;
for(int node=1;node<=numb_vert;node++)
distance[node]=MAX_VALUE;
distance[source]=0;
for(int node=1;node<=numb_vert-1;node++)
{
for(int src_node=1;src_node<=numb_vert;src_node++)
{
for(int dest_node=1;dest_node<=numb_vert;dest_node++)
{
if(adj_matrix[src_node][dest_node]!=MAX_VALUE)
{
if(distance[dest_node] > distance[src_node] +
adj_matrix[src_node][dest_node])
distance[dest_node] = distance[src_node] +
adj_matrix[src_node][dest_node];
}
}
}
}
for(int src_node=1;src_node<=numb_vert;src_node++)
{
for(int dest_node=1;dest_node<=numb_vert;dest_node++)
{
if(adj_matrix[src_node][dest_node]!=MAX_VALUE)
{
Dept. of CS&E, VKIT, 2024 – 25 14
Computer Network Laboratory (BCS502) V SEM
}
}
}
}
System.out.println("Destination Distance\t");
for(int vertex=1;vertex<=numb_vert;vertex++)
System.out.println(+vertex+"\t\t\t"+distance[vertex]);
int numb_vert=0;
int source;
Scanner scan = new Scanner(System.in);
if(adj_matrix[src_node][dest_node]==0)
adj_matrix[src_node][dest_node]=MAX_VALUE;
}
for(int i=1;i<=numb_vert;i++)
{
bellmanford bellmanford = new bellmanford(numb_vert);
bellmanford.BellmanfordpEvaluation(i,adj_matrix);
}
scan.close();
}
}
Output 1 –
5
Enter the number of vertices
6
Enter the adjacency matrix
0 2 5 1 999 999
2 0 3 2 999 999 3
5 3 13 1 5 2 3
1 2 3 0 1 999
999 999 1 1 0 2 5
999 999 5 999 2 0 2
Routing Table for Router 1 is
Destination Distance 1 2 3 6
1
1 0
2 2
3 3 1 2
4 1
5 2
4 5
6 4 1
Routing Table for Router 2 is
Destination Distance
1 2
2 0
3 3
4 2
5 3
6 5
Routing Table for Router 3 is
Destination Distance
1 3
2 3
3 0
4 2
5 1
6 3
Routing Table for Router 4 is
Destination Distance
1 1
2 2
3 2
4 0
5 1
6 3
Routing Table for Router 5 is
Destination Distance
1 2
2 3
3 1
4 1
5 0
6 2
Routing Table for Router 6 is
Destination Distance
1 4
2 5
3 3
4 3
5 2
6 0
Output 2 –
Enter the number of vertices
5
Enter the adjacency matrix
0 1 3 999 999 5
1 0 7 5 2 4
3 7 0 3 4 2
999 5 3 0 4 1
999 2 4 4 0 2 3
Routing Table for Router 1 is 7 4
Destination Distance 1
1 0
2 1 3
3 3 3 5
4 6
4
5 3
Routing Table for Router 2 is
Destination Distance
1 1
2 0
3 4
4 5
5 2
Routing Table for Router 3 is
Destination Distance
1 3
2 4
3 0
4 3
5 4
Routing Table for Router 4 is
Destination Distance
1 6
2 5
3 3
4 0
5 4
Routing Table for Router 5 is
Destination Distance
1 3
2 2
3 4
4 4
5 0
Output 3 –
4
Enter the number of vertices 2 4
5
Enter the adjacency matrix 3
0 999 3 1 4 7
999 0 4 999 7
1
5
3 4 0 5 999
1 999 5 0 999 4 1
4 7 999 999 0
Routing Table for Router 1 is 3 5
Destination Distance
1 0
2 7
3 3
4 1
5 4
Routing Table for Router 2 is
Destination Distance
1 7
2 0
3 4
4 8
5 7
Routing Table for Router 3 is
Destination Distance
1 3
2 4
3 0
4 4
5 7
Routing Table for Router 4 is
Destination Distance
1 1
2 8
3 4
4 0
5 5
Routing Table for Router 5 is
Destination Distance
1 4
2 7
3 7
4 5
5 0
Lab Program 7 :
Using TCP/IP sockets, write a client – server program to make the client send the file name and to make
the server send back the contents of the requested file if present.
Code –
import java.util.*;
import java.net.*;
import java.io.*;
try
{
Scanner ser=new Scanner(System.in);
Socket s=new Socket("localhost",998);
System.out.println(dis.readUTF());
dos.writeUTF(path);
System.out.println(new String (dis.readUTF()));
dis.close();
dos.close();
s.close();
ser.close();
}
catch(IOException e)
{
System.out.println("IO: "+e.getMessage());
}
import java.util.*;
import java.net.*;
import java.io.*;
public class tcpserver
{
public static void main (String args[])
{
try
{
ServerSocket s=new ServerSocket(998);
Socket s1=s.accept();
System.out.println(dis.readUTF());
String path=dis.readUTF();
System.out.println("\n request received \n processing ...... ");
try
{
File myfile=new File(path);
Scanner scr=new Scanner(myfile);
String st=scr.nextLine();
st="\n the context of file is \n "+st;
while(scr.hasNextLine())
{
st=st + "\n" + scr.nextLine();
}
dos.writeUTF(st);
dos.close();
s1.close();
scr.close();
}
catch(FileNotFoundException e)
{
System.out.println("\n,,,error...\n file not found");
dos.writeUTF("...error \n file not found");
}
}
catch(IOException e)
{
System.out.println("IO: "+e.getMessage());
}
finally
{
Output –
Client Side
$ javac tcpclient.java
$java tcpclient
connected to server
Server Side
$javac tcpserver.java
$ java tcpserver
server ready
waiting for connection
connected to 127.0.0.1
request received
processing.......
connection terminated
Lab Program 8 :
Develop a program on datagram socket for client/server to display the messages on client side, typed at the
server side.
Code –
import java.net.*;
import java.io.*;
try
{
aSocket=new DatagramSocket(clientPort);
byte[] buf=new byte[1000];
aSocket.receive(data);
byte[] msg=new byte[1000];
msg=data.getData();
System.out.println("\n msg:"+(new String(msg,0,data.getLength())));
}
catch(SocketException e)
{
System.out.println("Socket:" +e.getMessage());
}
catch(IOException e)
{
System.out.println("IO:" +e.getMessage());
}
finally
{
if(aSocket!=null)
aSocket.close();
}
import java.net.*;
import java.util.*;
import java.io.*;
try
{
aSocket=new DatagramSocket(serverPort);
buffer=str.getBytes();
}
catch(SocketException e)
{
System.out.println("Socket:"+e.getMessage());
}
catch(IOException e)
{
System.out.println("Io:"+e.getMessage());
}
finally
{
System.out.println("\nMessage sent\nConnection terminated");
if(aSocket!=null)
aSocket.close();
scn.close();
}
}
}
Output –
Client Side
$ javac UDPClient.java
$# java UDPClient
Waiting for server
Server Side
$javac UDPServer.java
$ java UDPServer
Server Ready
Waiting for connection....
Message sent
Connection terminated
Lab Program 9 :
Develop a program for simple RSA algorithm to encrypt and decrypt the data.
Code –
import java.math.BigInteger;
import java.util.Random;
private Random r;
long p1;
public rsalab()
{
r=new Random();
p=BigInteger.probablePrime(bitlength, r);
q=BigInteger.probablePrime(bitlength, r);
n=p.multiply(q);
phi=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e=BigInteger.probablePrime(bitlength/2, r);
d=e.modInverse(phi);
import java.lang.*;
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
class bts
{
public String bytesToString(byte[] encrypted)
{
String test="";
for(byte b:encrypted)
{
test+=Byte.toString(b);
}
return test;
}
}
Output 1–
Enter the plain text:
Hello World
Encrypting string: Hello World
String in bytes:721011081081113287111114108100
Encrypted string :45-2467-21-4376-10110519-45-12653101-11-9795-122111108-43-8077-1169-40-1172-85714-
5930-21-25-117-112-20-36-9110768-11395-20-5336-77-125147457-85-4748107-33-5578-87-1819-111-72-63-
705785179011914
Decrypted string in bytes :721011081081113287111114108100
Decrypted string :Hello World
Output 2–
Enter the plain text:
This is a sample
Encrypting string: This is a sample
String in bytes:841041051153210511532973211597109112108101
Encrypted string :7-38-64-487597-725231-45-87-6981-29-17-73-34127-101108-1289-126-769143-126-56-22-
21-27-7819120852868-91-81-47-105-7937-75-48-10681-6651-43-74-126-28-10468-853610941-38-58-127-126-
10910936-63347-69127
Decrypted string in bytes :841041051153210511532973211597109112108101
Decrypted string :This is a sample
Output 3–
Enter the plain text:
rsa algorithm
Encrypting string: rsa algorithm
String in bytes:114115973297108103111114105116104109
Encrypted string :3-56-1172220151939-1055135-16-4771-43127-58-2160117-3011961-46-323011771-125-
5612-175326-89480-23-102-111-94-239089983410156-12-113-128-50-9787-32-49-12033110-113-75-1611-23-
12671-86-852-62-70
Decrypted string in bytes :114115973297108103111114105116104109
Decrypted string :rsa algorithm
Code –
import java.util.Scanner;
int bucket=0;
int op_rate,i,n,bsize;
System.out.println("\nSec\tpsize\tBucket\tAccept/Reject\tpkt_send");
System.out.println(" ");
for(i=0;i<n;i++)
{
System.out.print(i+1+"\t"+pkt[i]+"\t");
if(bucket+pkt[i]<=bsize)
{
bucket+=pkt[i];
System.out.print(bucket+"\tAccept\t\t"+min(bucket,op_rate)+"\n" +"");
bucket=sub(bucket,op_rate);
}
else
{
int reject=(bucket+pkt[i]-bsize);
bucket=bsize;
System.out.print(bucket+"\tReject "+reject+"\t"+min(bucket,op_rate)+"\n");
bucket=sub(bucket,op_rate);
Dept. of CS&E, VKIT, 2024 – 25 28
Computer Network Laboratory (BCS502) V SEM
}
}
while(bucket!=0)
{
System.out.print((++i)+"\t0\t"+bucket+"\tAccept\t\t"+min(bucket,op_rate)+"\t");
bucket=sub(bucket,op_rate);
}
}
Output 1–
1 6 6 Accept 6
2 8 8 Accept 7
3 9 8 Reject 2 7
4 5 6 Accept 6
Output 2–
1 4 4 Accept 4
2 5 5 Accept 5
3 6 6 Accept 6
4 10 8 Reject 2 6
5 0 2 Accept 2
Output 3–
1 4 4 Accept 4
2 6 5 Reject 1 5
3 3 3 Accept 3
4 7 5 Reject 2 5
5 5 5 Accept 5