CN Lab 2024-1
CN Lab 2024-1
Set the queue size, vary the bandwidth and find the number of packets dropped.
Aim: 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.
proc finish { } {
global ns nf tf
$ns flush-trace # clears trace file contents
close $nf
close $tf
exec nam lab1.nam &
exit 0
}
set n0 [$ns node] # creates 3 nodes
set n2 [$ns
node] set n3
[$ns node]
AWK file: (Open a new editor using “vi command” and write awk file and save with
“.awk” extension)
#immediately after BEGIN should open braces ‘{‘
BEGIN{ c=0;}
{
if($1= ="d")
{ c++;
printf("%s\t%s\n",$5,$11);
}
}
END{ printf("The number of packets dropped is %d\n",c); }
AWK file: (Open a new editor using “gedit command” and write awk file and save
with “.awk” extension)
BEGIN{
drop=0;
}
{
if($1= ="d" )
{
drop++;
}
}
END{
printf("Total number of %s packets dropped due to congestion =%d\n",$5,drop);
}
Steps for execution
Open gedit editor and type program. Program name should have the extension “ .tcl
”
[root@localhost ~]# gedit lab2.tcl
Save the program and close the file.
Open gedit editor and type awk program. Program name should have the extension
“.awk ”
[root@localhost ~]# gedit lab2.awk
Save the program and close the file.
Run the simulation program
[root@localhost~]# ns lab2.tcl
Here “ns” indicates network simulator. We get the topology shown in the snapshot.
Now press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,
[root@localhost~]# awk –f lab2.awk lab2.tr
To see the trace file contents open the file as ,
[root@localhost~]# gedit lab2.tr
Topology Output
Output
Experiment No: 3 Date:
ETHERNET LAN USING N-NODES WITH MULTIPLE TRAFFIC
Aim: Implement an Ethernet LAN using n nodes and set multiple traffic nodes and plot
congestion window for different source / destination
AWK file: (Open a new editor using “gedit command” and write awk file and save
with “.awk” extension)
cwnd:- means congestion
window BEGIN {
}
{
if($6= ="cwnd_") # don’t leave space after writing
cwnd_ printf("%f\t%f\t\n",$1,$7); # you must put \n in printf
}
END {
}
Steps for execution
Open gedit editor and type program. Program name should have the extension “ .tcl
”
[root@localhost ~]# gedit lab3.tcl
Save the program and close the file.
Open gedit editor and type awk program. Program name should have the extension
“.awk ”
[root@localhost ~]# gedit lab3.awk
Save the program and close the file.
Run the simulation program
[root@localhost~]# ns lab3.tcl
Here “ns” indicates network simulator. We get the topology shown in the snapshot.
Now press the play button in the simulation window and the simulation will begins.
After simulation is completed run awk file to see the output ,
[root@localhost~]# awk –f lab3.awk file1.tr > a1
[root@localhost~]# awk –f lab3.awk file2.tr > a2
[root@localhost~]# xgraph a1 a2\
Here we are using the congestion window trace files i.e. file1.tr and file2.tr and
we are redirecting the contents of those files to new files say a1 and a2 using
output redirection operator (>).
To see the trace file contents open the file as ,
Topolgy:
Output:
Experiment No: 4
for(int i=0;i<len;i++)
{
if(a[i]!=0)
{
System.out.println("ERROR in Received data"); return;
}
System.out.println("no error");
}
}
}
Output:
Bellman-ford Algorithm
Source Code:
import java.util.Scanner;
public class BellmanFord
{ private int D[];
private int num_ver;
public static final int MAX_VALUE = 999;
public BellmanFord(int n)
{ this.n=n;
D = new int[n+1];
}
public void shortest(int s,int A[][])
{ for (int i=1;i<=n;i++)
{ D[i]=MAX_VALUE;
} D[s] = 0;
for(int k=1;k<=n-1;k++)
{ for(int i=1;i<=n;i++)
{ for(int j=1;j<=n;j++)
{ if(A[i][j]!=MAX_VALUE)
{ if(D[j]>D[i]+A[i][j])
D[j]=D[i]+A[i][j];
}
}
}
}
for(int i=1;i<=n;i++)
{ for(int j=1;j<=n;j++)
{ if(A[i][j]!=MAX_VALUE)
{ if(D[j]>D[i]+A[i][j])
{
System.out.println("The Graph contains negative egde cycle");
return;
} }
}
}
for(int i=1;i<=n;i++)
{
System.out.println("Distance of source " + s + " to "+ i + " is " + D[i]);
}
}
public static void main(String[ ] args)
{ int n=0,s;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertices");
n = sc.nextInt();
int A[][] = new int[n+1][n+1];
System.out.println("Enter the Weighted Matrix");
for(int i=1;i<=n;i++)
{ for(int j=1;j<=n;j++)
{ A[i][j]=sc.nextInt();
if(i==j)
{ A[i][j]=0;
continue;
} if(A[i]
[j]==0)
{ A[i][j]=MAX_VALUE;
}
}
}
System.out.println("Enter the source vertex");
s=sc.nextInt();
BellmanFord b = new BellmanFord(n);
b.shortest(s,A);
sc.close();
}
}
Output:
Enter the number of vertices 4
Enter the Weighted Matrix
0 5 0 0
5 0 3 4
0 3 0 2
0 4 2 0
Enter the source vertex
2
Distance of source 2 to 1 is 5
Distance of source 2 to 2 is 0
Distance of source 2 to 3 is 3
Distance of source 2 to 4 is 4
Experiment No: 7
Client-server using TCP/IP sockets
Source Code:
TCP Server
The server program has three responsibilities which must be fulfilled in the code. First
job is to read the file name coming from client. For this, it uses input stream. Second one is to
open the file, using some input stream, and read the contents. Third one is, as the reading is
going on, to send the contents each line separately.
import java.net.*;
import java.io.*;
public class ContentsServer
{ public static void main(String args[]) throws Exception
{ // establishing the connection with the server
ServerSocket sersock = new ServerSocket(4000);
System.out.println("Server ready for connection");
Socket sock = sersock.accept(); // binding with port: 4000
System.out.println("Connection is successful and waiting for chatting");
// reading the file name from client
InputStream istream = sock.getInputStream( );
BufferedReader fileRead =new BufferedReader(new InputStreamReader(istream));
String fname = fileRead.readLine( );
// reading file contents
BufferedReader contentRead = new BufferedReader(new FileReader(fname) );
// keeping output stream ready to send the contents
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
String str;
// reading line-by-line from file
while((str = contentRead.readLine()) != null)
{ pwrite.println(str); // sending each line to client
}
sock.close();
sersock.close(); // closing network sockets
pwrite.close();
fileRead.close();
contentRead.close();
}
}
TCP Client:
To read filename as input from keyboard, remember, this file should exist on server.
For this, it uses input stream. The file read from keyboard should be sent to the server. For
this client uses output stream. The file contents sent by the server, the client should receive
and print on the console.
BufferedReader:
The System.in is a byte stream and cannot be chained to BufferedReader as
BufferedReader is a character stream. The byte stream System.in is to be converted
(wrapped) into a character stream and then passed to BufferedReader constructor. To take
input from the keyboard, a BufferedReader object, keyRead, is created. To send the file
name to the server, pwrite of PrintWriter and to receive the file contents from the
server, socketRead of BufferedReader are created.
PrintWriter pwrite = new PrintWriter(ostream, true);
import java.net.*;
import java.io.*;
public class ContentsClient
{ public static void main( String args[ ] ) throws Exception
{
Socket sock = new Socket( "127.0.0.1", 4000);
// reading the file name from keyboard. Uses input stream
System.out.print("Enter the file name");
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
String fname = keyRead.readLine();
// sending the file name to server. Uses PrintWriter
OutputStream ostream = sock.getOutputStream( );
PrintWriter pwrite = new PrintWriter(ostream, true);
pwrite.println(fname);
// receiving the contents from server. Uses input stream
InputStream istream = sock.getInputStream();
BufferedReader socketRead = new BufferedReader(new InputStreamReader(istream));
String str;
while((str = socketRead.readLine()) != null) // reading line-by-line
{ System.out.println(str);
} pwrite.close();
socketRead.close();
keyRead.close();
}
}
Output:
At server side:
[root@localhost]# javac ContentsServer.java
[root@localhost]# Java ContentsServer
Server ready for connection
Connection is successful and waiting for chatting
At Client Side:
Source Code:
UDP Client
import
java.net.*;
import java.io.*;
class connectionClient
{ public static void main(String[ ] args)
{
try
{ InetAddress acceptorHost =
InetAddress.getByName(args[0]); int serverPortNum =
Integer.parseInt(args[1]);
Socket clientSocket = new Socket(acceptorHost, serverPortNum);
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream( )));
System.out.println(br.readLine( ));
clientSocket.close();
}
catch(Exception e)
{ e.printStackTrace( );
}
}
}
UDP Server
import
java.net.*;
import java.io.*;
class connectionServer
{ public static void main(String[ ] args)
{
try
{ String message = args[0];
int serverPortNumber = Integer.parseInt(args[1]);
ServerSocket connectionSocket = new ServerSocket(serverPortNumber);
Socket dataSocket = connectionSocket.accept();
PrintStream socketOutput = new PrintStream(dataSocket.getOutputStream());
socketOutput.println(message);
System.out.println("sent response to client");
socketOutput.flush( );
dataSocket.close( );
connectionSocket.close( );
}
catch(Exception e)
{ e.printStackTrace( );
}
}
}
Output:
Server Side
[root@localhost]# Javac ConnectionServer.java
[root@localhost]# Java ConnectionServer “Welcome to Computer Network Lab”
3956
Client Side:
[root@localhost]# Javac ConnectionClient.java
[root@localhost]# Java ConnectionClient localhost 3956
Welcome to Computer Network Lab
Experiment No: 9
RSA Algorithm to Encrypt and Decrypt the Data
Source Code:
import java.util.*;
import java.io.*;
public class rsa
{ static int gcd(int m,int n)
{ while(n!=0)
{ int r=m%n;
m=n;
n=r;
}
return m;
public static void main(String args[])
{
int p=0,q=0,n=0,e=0,d=0,phi=0;
int nummes[]=new int[100];
int encrypted[]=new int[100];
int decrypted[]=new int[100];
int i=0,j=0,nofelem=0;
Scanner sc=new Scanner(System.in);
String message ;
System.out.println("Enter the Message tobe encrypted:");
message= sc.nextLine();
System.out.println("Enter value of p and q\n");
p=sc.nextInt();
q=sc.nextInt();
n=p*q;
phi=(p-1)*(q-1);
for(i=2;i<phi;i++)
if(gcd(i,phi)==1) break;
e=i;
for(i=2;i<phi;i++)
if((e*i-1)%phi==0)
break;
d=i;
for(i=0;i<message.length();i++)
{
char c = message.charAt(i);
int a =(int)c;
nummes[i]=c-96;
}
nofelem=message.length();
for(i=0;i<nofelem;i++)
{
encrypted[i]=1;
for(j=0;j<e;j++)
encrypted[i] =(encrypted[i]*nummes[i])%n;
}
System.out.println("\n Encrypted message\n");
for(i=0;i<nofelem;i++)
{
System.out.print(encrypted[i]); System.out.print((char)(encrypted[i]
+96));
}
for(i=0;i<nofelem;i++)
{ decrypted[i]=1;
for(j=0;j<d;j++)
decrypted[i]=(decrypted[i]*encrypted[i])%n;
}
#********************************************************
**RESULT**
Source Code:
import java.util.*;
public class leaky
{
static int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
public static void main(String[] args)
{ int drop=0,mini,nsec,cap,count=0,i,process;
int inp[]=new int[25];
Scanner sc=new Scanner(System.in);
Output:
1 5 2 3 0
2 4 2 3 2
3 3 2 3 1
4 0 2 1 0
5 0 1 0 0
VIVA QUESTIONS
1. What are functions of different layers?
2. Differentiate between TCP/IP Layers and OSI Layers
3. Why header is required?
4. What is the use of adding header and trailer to frames?
5. What is encapsulation?
6. Why fragmentation requires?
7. What is MTU?
8. Which layer imposes MTU?
9. Differentiate between flow control and congestion control.
10. Differentiate between Point-to-Point Connection and End-to-End connections.
11. What are protocols running in different layers?
12. What is Protocol Stack?
13. Differentiate between TCP and UDP.
14. Differentiate between Connectionless and connection oriented connection.
15. Why frame sorting is required?
16. What is meant by subnet?
17. What is meant by Gateway?
18. What is an IP address?
19. What is MAC address?
20. Why IP address is required when we have MAC address?
21. What is meant by port?
22. What are ephemerical port number and well known port numbers?
23. What is a socket?
24. What are the parameters of socket()?
25. Describe bind(), listen(), accept(),connect(), send() and recv().
26. What are system calls? Mention few of them.
27. What is IPC? Name three techniques.
28. Explain mkfifo(), open(), close() with parameters.
29. What is meant by file descriptor?
30. What is meant by traffic shaping?
31. How do you classify congestion control algorithms?
32. Differentiate between Leaky bucket and Token bucket.
33. How do you implement Leaky bucket?
34. How do you generate busty traffic?
35. What is the polynomial used in CRC-CCITT?
36. What are the other error detection algorithms?
37. What is difference between CRC and Hamming code?
38. Why Hamming code is called 7,4 code?
39. What is odd parity and even parity?
40. What is meant by syndrome?
41. What is generator matrix?
42. What is spanning tree?
43. Differentiate between Prim’s and Kruskal’s algorithm.
44. What are Routing algorithms?
45. How do you classify routing algorithms? Give examples for each.
46. What are drawbacks in distance vector algorithm?
47. How routers update distances to each of its neighbor?
48. How do you overcome count to infinity problem?
49. What is cryptography?
50. How do you classify cryptographic algorithms?
51. What is public key?
52. What is private key?
53. What are key, ciphertext and plaintext?
54. What is simulation?
55. What are advantages of simulation?
56. Differentiate between Simulation and Emulation.
57. What is meant by router?
58. What is meant by bridge?
59. What is meant by switch?
60. What is meant by hub?
61. Differentiate between route, bridge, switch and hub.
62. What is ping and telnet?
63. What is FTP?
64. What is BER?
65. What is meant by congestion window?
66. What is BSS?
67. What is incoming throughput and outgoing throughput?
68. What is collision?
69. How do you generate multiple traffics across different sender-receiver pairs?
70. How do you setup Ethernet LAN?
71. What is meant by mobile host?
72. What is meant by NCTUns?
73. What are dispatcher, coordinator and nctunsclient?
74. Name few other Network simulators
75. Differentiate between logical and physical address.
76. Which address gets affected if a system moves from one place to another place?
77. What is ICMP? What are uses of ICMP? Name few.
78. Which layer implements security for data?
5.Develop a program to implement a sliding window protocol in the data link layer.
import java.util.Scanner;
import java.util.Random;
class SlidingWindowProtocol {
private int windowSize; // Size of the window
private int totalFrames; // Total frames to be sent
private int[] frames; // Array to hold frames
// Sending loop
while (sentFrameIndex < totalFrames) {
int windowEnd = Math.min(sentFrameIndex + windowSize, totalFrames);
System.out.print("Sending frames: ");
for (int i = sentFrameIndex; i < windowEnd; i++) {
System.out.print(frames[i] + " ");
}
System.out.println();
sc.close();
}
}
Output:
Sending frames: 7 8
ACK received for frame 7
ACK received for frame 8
Sending frames: 9 10
ACK received for frame 9
ACK received for frame 10
6.Develop a program to find the shortest path between vertices using the Bellman-Ford and path vector
routing algorithm.
import java.util.Scanner;
import java.util.Arrays;
private static final int MAX_VALUE = 9999; // A large value to represent infinity
private int numVertices; // Number of vertices in the graph
private int[][] graph; // Adjacency matrix to store edge weights
private int[][] distanceVectors; // Distance vector table
bfPv.pathVectorRouting();
sc.close();
}
}
Output:
Shortest path from source vertex 1 using Bellman-Ford:
Distance to vertex 1 is 0
Distance to vertex 2 is 3
Distance to vertex 3 is 4
Distance to vertex 4 is 6