0% found this document useful (0 votes)
32 views41 pages

CN Lab 2024-1

Uploaded by

iqrafarheen675
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views41 pages

CN Lab 2024-1

Uploaded by

iqrafarheen675
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

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.

THREE NODE POINT TO POINT NETWORK

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.

set ns [new Simulator] # Letter S is capital


set nf [open lab1.nam w] # open a nam trace file in write mode
$ns namtrace-all $nf # nf nam filename
set tf [open lab1.tr w] # tf trace filename
$ns trace-all $tf

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]

$ns duplex-link $n0 $n2 200Mb 10ms DropTail # establishing links


$ns duplex-link $n2 $n3 1Mb 1000ms DropTail
$ns queue-limit $n0 $n2 10

set udp0 [new Agent/UDP] # attaching transport layer protocols


$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR] # attaching application layer protocols
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null] # creating sink(destination) node


$ns attach-agent $n3 $null0
$ns connect $udp0 $null0

$ns at 0.1 "$cbr0 start"


$ns at 1.0 "finish"
$ns run

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); }

Steps for execution


 Open gedit editor and type program. Program name should have the extension “ .tcl

[root@localhost ~]# gedit lab1.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 lab1.awk
 Save the program and close the file.
 Run the simulation program
[root@localhost~]# ns lab1.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 lab1.awk lab1.tr
 To see the trace file contents open the file as ,
[root@localhost~]# gedit lab1.tr

Trace file contains 12 columns:


Event type, Event time, From Node, To Node, Packet Type, Packet Size, Flags (indicated by
--------), Flow ID, Source address, Destination address, Sequence ID, Packet ID

Contents of Trace File Topology Output


Experiment No: 2 Date:
TRANSMISSION OF PING MESSAGE

Aim: 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.
set ns [ new Simulator ]
set nf [ open lab2.nam
w]
$ns namtrace-all $nf
set tf [ open lab2.tr w ]
$ns trace-all $tf
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]
$ns duplex-link $n0 $n4 1005Mb 1ms DropTail
$ns duplex-link $n1 $n4 50Mb 1ms DropTail
$ns duplex-link $n2 $n4 2000Mb 1ms DropTail
$ns duplex-link $n3 $n4 200Mb 1ms DropTail
$ns duplex-link $n4 $n5 1Mb 1ms DropTail

set p1 [new Agent/Ping] # letters A and P should be capital


$ns attach-agent $n0 $p1
$p1 set packetSize_ 50000
$p1 set interval_ 0.0001
set p2 [new Agent/Ping] # letters A and P should be capital
$ns attach-agent $n1 $p2
set p3 [new Agent/Ping] # letters A and P should be capital
$ns attach-agent $n2 $p3
$p3 set packetSize_ 30000
$p3 set interval_ 0.00001
set p4 [new Agent/Ping] # letters A and P should be capital
$ns attach-agent $n3 $p4
set p5 [new Agent/Ping] # letters A and P should be capital
$ns attach-agent $n5 $p5
$ns queue-limit $n0 $n4 5
$ns queue-limit $n2 $n4 3
$ns queue-limit $n4 $n5 2
Agent/Ping instproc recv {from rtt}
{
$self instvar node_
puts "node [$node_ id] received answer from $from with round trip time $rtt msec"
}
# please provide space between $node_ and id. No space between $ and from. No space
between and $ and rtt */
$ns connect $p1 $p5
$ns connect $p3 $p4
proc finish { } {
global ns nf tf
$ns flush-trace
close $nf
close $tf
exec nam lab2.nam &
exit 0
}
$ns at 0.1 "$p1 send"
$ns at 0.2 "$p1 send"
$ns at 0.3 "$p1 send"
$ns at 0.4 "$p1 send"
$ns at 0.5 "$p1 send"
$ns at 0.6 "$p1 send"
$ns at 0.7 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$p1 send"
$ns at 0.1 "$p3 send"
$ns at 0.2 "$p3 send"
$ns at 0.3 "$p3 send"
$ns at 0.4 "$p3 send"
$ns at 0.5 "$p3 send"
$ns at 0.6 "$p3 send"
$ns at 0.7 "$p3 send"
$ns at 0.8 "$p3 send"
$ns at 0.9 "$p3 send"
$ns at 1.0 "$p3 send"

$ns at 2.0 "finish"


$ns run

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

set ns [new Simulator]


set tf [open lab3.tr w]
$ns trace-all $tf
set nf [open lab3.nam w]
$ns namtrace-all $nf
set n0 [$ns node]
$n0 color "magenta"
$n0 label "src1"
set n1 [$ns node]
set n2 [$ns node]
$n2 color "magenta"
$n2 label "src2"
set n3 [$ns node]
$n3 color "blue"
$n3 label "dest2"
set n4 [$ns node]
set n5 [$ns node]
$n5 color "blue"
$n5 label "dest1"
$ns make-lan "$n0 $n1 $n2 $n3 $n4" 100Mb 100ms LL Queue/ DropTail Mac/802_3
$ns duplex-link $n4 $n5 1Mb 1ms DropTail
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
$ftp0 set packetSize_ 500
$ftp0 set interval_ 0.0001
set sink5 [new Agent/TCPSink]
$ns attach-agent $n5 $sink5
$ns connect $tcp0 $sink5
set tcp2 [new Agent/TCP]
$ns attach-agent $n2 $tcp2
set ftp2 [new Application/FTP]
$ftp2 attach-agent $tcp2
$ftp2 set packetSize_ 600
$ftp2 set interval_ 0.001
set sink3 [new Agent/TCPSink]
$ns attach-agent $n3 $sink3
$ns connect $tcp2 $sink3
set file1 [open file1.tr w]
$tcp0 attach $file1
set file2 [open file2.tr w]
$tcp2 attach $file2
$tcp0 trace cwnd_ # must put underscore ( _ ) after cwnd and no space between them
$tcp2 trace cwnd_
proc finish { } {
global ns nf tf
$ns flush-trace
close $tf
close $nf
exec nam lab3.nam &
exit 0
}

$ns at 0.1 "$ftp0 start"


$ns at 5 "$ftp0 stop"
$ns at 7 "$ftp0 start"
$ns at 0.2 "$ftp2 start"
$ns at 8 "$ftp2 stop"
$ns at 14 "$ftp0 stop"
$ns at 10 "$ftp2 start"
$ns at 15 "$ftp2 stop"
$ns at 16 "finish"
$ns run

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

Error Detecting Code Using CRC-CCITT (16-bit)


Source Code:
import java.util.*;
class crc
{ void div(int a[],int k)
{ int gp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int count=0;
for(int i=0;i<k;i++)
{
if(a[i]==gp[0])
{
for(int j=i;j<17+i;j++)
{
a[j]=a[j]^gp[count++];
}
count=0;
}
}
}
public static void main(String args[])
{
int a[]=new int[100];
int b[]=new
int[100]; int len,k;
crc ob=new crc();
System.out.println("Enter the length of Data Frame:");
Scanner sc=new Scanner(System.in);
len=sc.nextInt();
int flag=0;
System.out.println("Enter the Message:");
for(int i=0;i<len;i++)
{ a[i]=sc.nextInt();
}
for(int i=0;i<16;i++)
{ a[len++]=0;
}
k=len-16;
for(int i=0;i<len;i++)
{ b[i]=a[i];
}
ob.div(a,k);
for(int i=0;i<len;i++)
a[i]=a[i]^b[i];
System.out.println("Data to be transmitted: ");
for(int i=0;i<len;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Enter the Reveived Data: "); for(int
i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
ob.div(a, k);

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:

Enter the length of Data Frame: 4 Enter


the Message: 1 0 1 1
Data to be transmitted: 1 0 1 1 1 0 1 1 0 0 0 1 0 1 1 0 1 0 1 1
Enter the Received Data: 1 0 1 1 1 0 1 1 0 0 0 0 0 1 1 0 1 0 1 1
ERROR in Received Data
**********************************************************
Experiment No 6:

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:

[root@localhost]# javac ContentsClient.java


[root@localhost]# java ContentsClient
Enter the file name
aa.txt
Welcome to Network Lab
Experiment No: 8
Client-Server Communication Socket

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;
}

System.out.println("\n Decrypted message\n ");


for(i=0;i<nofelem;i++) System.out.print((char)
(decrypted[i]+96)); return;
}

#********************************************************
**RESULT**

Enter the text:


hello
Enter the value of P and Q :
5
7
Encrypted Text is: 8 h 10 j 17 q 17 q 15 o
Decrypted Text is: hello
Experiment No: 10
Congestion Control Using Leaky Bucket Algorithm

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);

System.out.println("Enter The Bucket Size\n");


cap= sc.nextInt();
System.out.println("Enter The Operation Rate\n");
process= sc.nextInt();
System.out.println("Enter The No. Of Seconds You Want To Stimulate\n");
nsec=sc.nextInt();
for(i=0;i<nsec;i++)
{ System.out.print("Enter The Size Of The Packet Entering At "+ i+1 + "
sec");
inp[i] = sc.nextInt();
}
System.out.println("\nSecond | Packet Recieved | Packet Sent | Packet Left |
Packet Dropped|\n");
System.out.println(" \n");
for(i=0;i<nsec;i++)
{ count+=inp[i];
if(count>cap)
{ drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t"+inp[i]);
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
drop=0;
System.out.println();
}
for(;count!=0;i++)
{
if(count>cap)
{
drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t0");
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
System.out.println();
}
}
}

Output:

Enter The Bucket Size


5
Enter The Operation Rate
2
Enter The No. Of Seconds You Want To Stimulate
3
Enter The Size Of The Packet Entering At 1 sec
5
Enter The Size Of The Packet Entering At 1 sec
4
Enter The Size Of The Packet Entering At 1 sec
3
Second|Packet Recieved|Packet Sent|Packet Left|Packet Dropped|

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

public SlidingWindowProtocol(int totalFrames, int windowSize) {


this.totalFrames = totalFrames;
this.windowSize = windowSize;
frames = new int[totalFrames];
for (int i = 0; i < totalFrames; i++) {
frames[i] = i + 1; // Frame numbers start from 1 for simplicity
}
}

// Simulates sending frames with sliding window protocol


public void sendFrames() {
int sentFrameIndex = 0;
Scanner sc = new Scanner(System.in);
Random rand = new Random();

System.out.println("Starting Sliding Window Protocol Simulation");

// 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();

// Simulate ACK reception for each frame in the current window


for (int i = sentFrameIndex; i < windowEnd; i++) {
// Randomly simulate successful ACK reception or timeout
if (rand.nextInt(10) < 8) { // 80% chance of successful ACK
System.out.println("ACK received for frame " + frames[i]);
sentFrameIndex++; // Move window forward
} else {
System.out.println("Timeout for frame " + frames[i] + ". Resending window...");
break; // Resend the entire window on timeout
}
}
System.out.println();
}

System.out.println("All frames sent successfully!");


sc.close();
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter the total number of frames to send: ");


int totalFrames = sc.nextInt();

System.out.print("Enter the window size: ");


int windowSize = sc.nextInt();

SlidingWindowProtocol swp = new SlidingWindowProtocol(totalFrames, windowSize);


swp.sendFrames();

sc.close();
}
}
Output:

Enter the total number of frames to send: 10


Enter the window size: 4

Starting Sliding Window Protocol Simulation


Sending frames: 1 2 3 4
ACK received for frame 1
ACK received for frame 2
ACK received for frame 3
ACK received for frame 4
Sending frames: 5 6 7 8
ACK received for frame 5
ACK received for frame 6
Timeout for frame 7. Resending window...

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

All frames sent successfully!

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;

public class BellmanFordPathVector {

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

public BellmanFordPathVector(int numVertices) {


this.numVertices = numVertices;
this.graph = new int[numVertices + 1][numVertices + 1];
this.distanceVectors = new int[numVertices + 1][numVertices + 1];
}

// Initialize the graph with user input


public void initializeGraph() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the weighted adjacency matrix:");
for (int i = 1; i <= numVertices; i++) {
for (int j = 1; j <= numVertices; j++) {
graph[i][j] = sc.nextInt();
if (i == j) {
graph[i][j] = 0; // Distance from a vertex to itself is 0
} else if (graph[i][j] == 0) {
graph[i][j] = MAX_VALUE; // 0 indicates no direct edge
}
}
}
}

// Bellman-Ford algorithm to find shortest paths from a source vertex


public void bellmanFord(int source) {
int[] distance = new int[numVertices + 1];
Arrays.fill(distance, MAX_VALUE); // Initialize distances to infinity
distance[source] = 0;

// Relax all edges numVertices - 1 times


for (int k = 1; k <= numVertices - 1; k++) {
for (int i = 1; i <= numVertices; i++) {
for (int j = 1; j <= numVertices; j++) {
if (graph[i][j] != MAX_VALUE && distance[i] != MAX_VALUE && distance[j] > distance[i] +
graph[i][j]) {
distance[j] = distance[i] + graph[i][j];
}
}
}
}

// Print shortest path from source to each vertex


System.out.println("Shortest path from source vertex " + source + " using Bellman-Ford:");
for (int i = 1; i <= numVertices; i++) {
System.out.println("Distance to vertex " + i + " is " + (distance[i] == MAX_VALUE ? "infinity" :
distance[i]));
}
}

// Path Vector Routing Algorithm to update the distance vector table


public void pathVectorRouting() {
// Initialize each vertex's distance vector based on direct edges
for (int i = 1; i <= numVertices; i++) {
for (int j = 1; j <= numVertices; j++) {
distanceVectors[i][j] = graph[i][j];
}
}

// Update the distance vectors iteratively


for (int k = 0; k < numVertices - 1; k++) { // Repeat for (|V|-1) rounds
for (int i = 1; i <= numVertices; i++) {
for (int j = 1; j <= numVertices; j++) {
for (int via = 1; via <= numVertices; via++) {
if (distanceVectors[i][via] != MAX_VALUE && distanceVectors[via][j] != MAX_VALUE) {
int newDist = distanceVectors[i][via] + distanceVectors[via][j];
if (distanceVectors[i][j] > newDist) {
distanceVectors[i][j] = newDist;
}
}
}
}
}
}

// Print the final distance vector table for each vertex


System.out.println("\nDistance vector table after Path Vector Routing:");
for (int i = 1; i <= numVertices; i++) {
System.out.print("Distance from vertex " + i + " to others: ");
for (int j = 1; j <= numVertices; j++) {
System.out.print((distanceVectors[i][j] == MAX_VALUE ? "inf" : distanceVectors[i][j]) + " ");
}
System.out.println();
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of vertices: ");
int numVertices = sc.nextInt();

BellmanFordPathVector bfPv = new BellmanFordPathVector(numVertices);


bfPv.initializeGraph();
System.out.print("Enter the source vertex for Bellman-Ford: ");
int source = sc.nextInt();
bfPv.bellmanFord(source);

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

Distance vector table after Path Vector Routing:


Distance from vertex 1 to others: 0 3 4 6
Distance from vertex 2 to others: 3 0 1 3
Distance from vertex 3 to others: 4 1 0 2
Distance from vertex 4 to others: 6 3 2 0

10. leaky bucket program


package cnlab;
import java.util.Scanner;
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);
System.out.println("enter the bucket size");
cap= sc.nextInt();
System.out.println("Enter The Operation Rate");
process= sc.nextInt();
System.out.println("Enter The No. Of Seconds You Want To Stimulate");
nsec=sc.nextInt();
for(i=0;i<nsec;i++)
{
System.out.println("Enter The Size Of The Packet Entering At "+ i+1 + "sec");
inp[i] = sc.nextInt();
}
System.out.println("\nSecond | Packet Recieved | Packet Sent | Packet Left | Packet Dropped|\n");
System.out.println(" ");
for(i=0;i<nsec;i++)
{
count+=inp[i]; if(count>cap)
{
drop=count-cap; count=cap;
}
}
System.out.print(i+1);
System.out.print("\t\t"+inp[i]);
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
drop=0;
System.out.println();
for(;count!=0;i++)
{
if(count>cap)
{
drop=count-cap;
count=cap;
}
System.out.print(i+1);
System.out.print("\t\t0");
mini=min(count,process);
System.out.print("\t\t"+mini);
count=count-mini;
System.out.print("\t\t"+count);
System.out.print("\t\t"+drop);
System.out.println();
}
}
}
REFERENCE

1. Communication Networks: Fundamental Concepts and Key


Architectures - Alberto Leon, Garcia and Indra Widjaja, 3 rd Edition, Tata
McGraw- Hill, 2004.
2. Data and Computer Communication, William Stallings, 8th tEdition,
Pearson Education, 2007.
3. Computer Networks: A Systems Approach - Larry L. Peterson and Bruce S.
David, 4th Edition, Elsevier, 2007.
4. Introduction to Data Communications and Networking – Wayne Tomasi,
Pearson Education, 2005.
5. Communication Networks – Fundamental Concepts and Key
architectures – Alberto Leon- Garcia and Indra Widjaja:, 2rd Edition, Tata
McGraw-Hill, 2004
6. Computer and Communication Networks – Nader F. Mir:, Pearson
Education, 2007.

You might also like