CN Lab Manual Cse Dsupdated
CN Lab Manual Cse Dsupdated
Part-A
Introduction to NS-2:
Widely known as NS2, is simply an event driven simulation tool.
Simulation of wired as well as wireless network functions and protocols (e.g., routing algorithms, TCP,
UDP) can be done using NS2.
In general, NS2 provides users with a way of specifying such network protocols and simulating their
corresponding behaviors.
Tcl scripting
• Tcl is a general purpose scripting language. [Interpreter]
• Tcl runs on most of the platforms such as Unix, Windows, and Mac.
• The strength of Tcl is its simplicity.
• It is not necessary to declare a data type for variable prior to the usage.
Basics of TCL
Syntax: command arg1 arg2 arg3
Hello World!
puts stdout{Hello, World!}
Hello, World!
Simple Arithmetic
expr 7.2 / 4
Procedures
proc Diag {a b} {
set c [expr sqrt($a * $a + $b * $b)]
return $c }
Loops
while{$i < $n}
{
for {set i 0} {$i < $n} {incr i}
{
NS Simulator Preliminaries.
Initialization and termination aspects of the ns simulator.
Definition of network nodes, links, queues and
topology. Definition of agents and of applications.
The nam visualization tool.
Tracing and random variables.
Which is thus the first line in the tcl script? This line declares a new variable as using the set command, you can
call this variable as you wish, In general people declares it as ns because it is an instance of the Simulator class,
so an object the code [new Simulator] is indeed the installation of the class Simulator using the reserved word
new.
In order to have output files with data on the simulation (trace files) or files used for visualization (nam files),
we need to create the files using ―open command:
The above creates a data trace file called ―out.tr and a nam visualization trace file called ―out.nam
.Within the tcl script, these files are not called explicitly by their names, but instead by pointers that are declared
above and called tracefile1 and namfile respectively. Comments/Remark begins with a # symbol. The second line
open the file “out.tr” to be used for writing, declared with the letter “w”. The third line uses a simulator method
called trace-all that have as parameter the name of the file where the traces will go.
The last line tells the simulator to record all 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. In our case, this will be the file
pointed
at by the pointer “$namfile”,i.e the file “out.tr”.
The termination of the program is done using a ―finish‖ procedure
$ns flush-trace
Close $tracefile1
Close $namfile
Exit 0
The word proc declares a procedure in this case called finish and without arguments. The word global is used to
tell that we are using variables declared outside the procedure. The simulator method “flush-trace” will dump the
traces on the respective files. The tcl command “close” closes the trace files defined before and exec executes the
nam program for visualization. The command exit will ends the application and return the number 0 as status to
the system. Zero is the default for a clean exit. Other values can be used to say that is a exit because something
fails.
At the end of ns program we should call the procedure finish and specify at what time the termination
should occur. For example,
$ns at 125.0 “finish”
will be used to call finish at time 125sec. Indeed,the at method of the simulator allows us to schedule events
The simulation can then begin using the command
$ns run
The node is created which is printed by the variable n0. When we shall refer to that node in the script we shall
thus write $n0.
Once we define several nodes, we can define the links that connect them. An example of a definition of a
link is:
$ns duplex-link $n0 $n2 10Mb 10ms DropTail
Which means that $n0 and $n2 are connected using a bi-directional link that has 10ms of propagation delay and a
capacity of 10Mb per sec for each direction.
To define a directional link instead of a bi-directional one, we should replace ―duplex-link by ―simplex-
link.
In NS, an output queue of a node is implemented as a part of each link whose input is that node. The definition of
the link then includes the way to handle overflow at that queue. In our case, if the buffer capacity of the output
queue is exceeded then the last packet to arrive is dropped. Many alternative options exist, such as the RED
(Random Early Discard) mechanism, the FQ (Fair Queuing), the DRR (Deficit Round Robin), the stochastic Fair
Queuing (SFQ) and the CBQ (which including a priority and a round-robin scheduler).
In ns, an output queue of a node is implemented as a part of each link whose input is that node. We should
also define the buffer capacity of the queue related to each link. An example would be:
#set Queue Size of link (n0-n2) to 20
The command $ns attach-agent $n0 $tcp defines the source node of the tcp connection.
The command
set sink [new Agent /TCPSink]
Defines the behavior of the destination node of TCP and assigns to it a pointer called sink.
Dept. of CSE (DS), BIT, 2024- 5
2025
Computer Network Laboratory (BCS502)
When we have several flows, we may wish to distinguish them so that we can identify them with different colors
in the visualization part. This is done by the command $tcp set fid_ 1 that assigns to the TCP connection a
flow identification of ―1‖.We shall later give the flow identification of ―2‖ to the UDP connection.
Scheduling Events
NS is a discrete event based simulation. The tcp script defines when event should occur. The initializing
command set ns [new Simulator] creates an event scheduler, and events are then scheduled using the format:
The scheduler is started when running ns that is through the command $ns run.
The beginning and end of the FTP and CBR application can be done through the following command
1. The first field is the event type. It is given by one of four possible symbols r, +, -, d which correspond
respectively to receive (at the output of the link), enqueued, dequeued and dropped.
2. The second field gives the time at which the event occurs.
3. Gives the input node of the link at which the event occurs.
4. Gives the output node of the link at which the event occurs.
5. Gives the packet type (eg CBR or TCP)
6. Gives the packet size
7. Some flags
8. This is the flow id (fid) of IPv6 that a user can set for each flow at the input OTcl script one can further
use this field for analysis purposes; it is also used when specifying stream color for the NAM display.
9. This is the source address given in the form of ―node.port‖.
10. This is the destination address, given in the same form.
11. This is the network layer protocol’s packet sequence number. Even though UDP implementations in a real
network do not use sequence number, ns keeps track of UDP packet sequence number for analysis purposes
12. The last field shows the Unique id of the packet.
XGRAPH
The xgraph program draws a graph on an x-display given data read from either data file or from standard
input if no files are specified. It can display upto 64 independent data sets using different colors and line styles for
each set. It annotates the graph with a title, axis labels, grid lines or tick marks, grid labels and a legend.
Syntax:
Xgraph [options] file-name
Awk- An Advanced
Syntax:
awk option ‘selection_criteria {action}’ file(s)
Here, selection_criteria filters input and select lines for the action component to act upon. The
selection_criteria is enclosed within single quotes and the action within the curly braces. Both the
selection_criteria and action forms an awk program.
Example: $ awk „/manager/ {print}‟ emp.lst
Variables
Awk allows the user to use variables of there choice. You can now print a serial number, using the variable kount,
and apply it those directors drawing a salary exceeding 6700:
$ awk –F”|” „$3 == “director” && $6 > 6700
{ kount =kount+1
printf “ %3f %20s %-12s %d\n”, kount,$2,$3,$6 }‟ empn.lst
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=="d") count+
} +
END{
Output-
Trace file (lab1.tr) needs to checked to see the data transfer
$ns lab1.tcl
$awk -f numDrop.awk lab1.tr
Number of packets dropped due to congestion 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
set intervalTime 0.001
set now [$ns now]
$ns at [expr $now + $intervalTime] "$p2 send"
$ns at [expr $now + $intervalTime] "$p3 send"
$ns at [expr $now + $intervalTime] "sendPingPacket"
}
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=="d") 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 Experiment 4 :
Write a program for error detecting code using CRC-CCITT (16- bits).
Whenever digital data is stored or interfaced, data corruption might occur. Since the beginning of
computer science, developers have been thinking of ways to deal with this type of problem. For serial data they
came up with the solution to attach a parity bit to each sent byte. This simple detection mechanism works if an
odd number of bits in a byte changes, but an even number of false bits in one byte will not be detected by the
parity check. To overcome this problem developers have searched for mathematical sound mechanisms to detect
multiple false bits. The CRC calculation or cyclic redundancy check was the result of this. Nowadays CRC
calculations are used in all types of communications. All packets sent over a network connection are checked
with a CRC. Also each data block on your hard disk has a CRC value attached to it. Modern computer world
cannot do without these CRC calculations. So let's see why they are so widely used. The answer is simple; they
are powerful, detect many types of errors and are extremely fast to calculate especially when dedicated hardware
chips are used.
The idea behind CRC calculation is to look at the data as one large binary number. This number is divided
by a certain value and the remainder of the calculation is called the CRC. Dividing in the CRC calculation at first
looks to cost a lot of computing power, but it can be performed very quickly if we use a method similar to the
one learned at school. We will as an example calculate the remainder for the character 'm'—which is 1101101 in
binary notation—by dividing it by 19 or 10011. Please note that 19 is an odd number. This is necessary as we
will see further on. Please refer to your schoolbooks as the binary calculation method here is not very different
from the decimal method you learned when you were young. It might only look a little bit strange. Also notations
differ between countries, but the method is similar.
With decimal calculations you can quickly check that 109 divided by 19 gives a quotient of 5 with 14 as
the remainder. But what we also see in the scheme is that every bit extra to check only costs one binary
comparison and in 50% of the cases one binary subtraction. You can easily increase the number of bits of the test
data string—for example to 56 bits if we use our example value "Lammert"—and the result can be calculated
with 56 binary comparisons and an average of 28 binary subtractions. This can be implemented in hardware
directly with only very few transistors involved. Also software algorithms can be very efficient.
All of the CRC formulas you will encounter are simply checksum algorithms based on modulo-2 binary
division where we ignore carry bits and in effect the subtraction will be equal to an exclusive or operation.
Though some differences exist in the specifics across different CRC formulas, the basic mathematical process is
always the same:
The message bits are appended with c zero bits; this augmented message is the dividend
A predetermined c+1-bit binary sequence, called the generator polynomial, is the divisor
The checksum is the c-bit remainder that results from the division operation
Table 1 lists some of the most commonly used generator polynomials for 16- and 32-bit CRCs. Remember that
the width of the divisor is always one bit wider than the remainder. So, for example, you’d use a 17-bit generator
polynomial whenever a 16-bit checksum is required.
Note if G(x) has order n - highest power is x ,and the remainder will cover n bits.
i.e. Add n bits (Zeros) to message.
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
Lab Program 5 :
Develop a program to implement a sliding window protocol in the data link layer
The sliding window protocol is a well-known technique that plays a significant role in ensuring reliable and
orderly data exchange between a sender and a receiver. In this section, we will delve into the concept of the
sliding window protocol and demonstrate how to implement it using Java.
Understanding the Sliding Window Protocol
The sliding window protocol is a communication protocol used to manage the flow control and reliability of data
transmission over a network. It allows the sender to transmit a specified number of packets, known as the
window size, without waiting for an acknowledgment from the receiver for each packet. This approach enhances
efficiency by minimizing the communication overhead.
The protocol employs two primary components: the sender's sliding window and the receiver's sliding window.
The sender's window keeps track of the packets that have been sent but not yet acknowledged, while the
receiver's window tracks the expected sequence of packets to receive. As acknowledgments are received, both
windows slide forward, allowing for the continuous flow of data.
Implementation of the Sliding Window Protocol
To implement the sliding window protocol in Java, we will create a simplified example of a sender and a receiver
using sockets for communication. We will assume a reliable connection, so the focus will be on the sliding
window mechanism.
Server Program
import java.io.*;
import java.net.*;
import java.util.Random;
System.out.println("Client connected.");
String line;
int base = 0;
if (simulatePacketLoss()) {
} else {
// Simulate acknowledgment
base++;
}
Dept. of CSE (DS), BIT, 2024- 31
2025
Computer Network Laboratory (BCS502)
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static boolean simulatePacketLoss() {
Random random = new Random();
return random.nextDouble() < PACKET_LOSS_RATE;
}
}
Client Program
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.util.Random;
public class SlidingWindowClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int PORT = 12345;
private static final int WINDOW_SIZE = 4;
private static final double PACKET_LOSS_RATE = 0.1; // 10% packet loss
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try (Socket socket = new Socket(SERVER_ADDRESS, PORT);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
System.out.println("Connected to server.");
System.out.println("Enter packets (type 'exit' to quit):");
int base = 0;
while (true) {
if (ack != null) {
base++;
if (packet.equalsIgnoreCase("exit")) {
break;
}
Dept. of CSE (DS), BIT, 2024- 32
2025
Computer Network Laboratory (BCS502)
if (simulatePacketLoss()) {
} else {
out.println(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
Lab Program : 6
Write a program to find the shortest path between vertices using bellman-ford algorithm.
Bellman-Ford algorithm is a procedure used to find all shortest path in a graph from one source to all other nodes.
The algorithm requires that the graph does not contain any cycles of negative length, but if it does, the algorithm
is able to detect it.
The algorithm was introduced by American mathematicians Richard Bellman and Lester Ford.
It is similar to Dijkstra's algorithm but it can work with graphs in which edges can have negative weights.
Why would one ever have edges with negative weights in real life?
Negative weight edges might seem useless at first but they can explain a lot of phenomena like cashflow, heat
Dept. of CSE (DS), BIT, 2024- 35
2025
Computer Network Laboratory (BCS502)
For instance, if there are different ways to reach from one chemical A to another chemical B, each method will
have sub-reactions involving both heat dissipation and absorption.
If we want to find the set of reactions where minimum energy is required, then we will need to be able to factor in
the heat absorption as negative weights and heat dissipation as positive weights.
Negative weight edges can create negative weight cycles i.e. a cycle which will reduce the total path distance by
coming back to the same point.
Shortest path algorithms like Dijkstra's Algorithm that aren't able to detect such a cycle can give an incorrect
result because they can go through a negative weight cycle and reduce the path length.
Bellman Ford algorithm works by overestimating the length of the path from the starting vertex to all other
vertices. Then it iteratively relaxes those estimates by finding new paths that are shorter than the previously
overestimated paths.
By doing this repeatedly for all vertices, we are able to guarantee that the end result is
optimized.
Code -
import java.util.Scanner;
public class bellmanford
{
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)
{
if(distance[dest_node] > distance[src_node] +
adj_matrix[src_node][dest_node])
{
System.out.println("The graph contains negative edge cycle");
}
Dept. of CSE (DS), BIT, 2024- 38
2025
Computer Network Laboratory (BCS502)
}
}
}
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 0 3 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
1 is Routing Table for Router 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.
Sockets are a protocol independent method of creating a connection between processes. Sockets can be either
Packet based or streams based: Are there message boundaries or is it one stream?
Reliable or unreliable: Can messages be lost, duplicated, reordered, or corrupted?
Socket characteristics
Sockets are characterized by their domain, type and transport protocol. Common domains are:
AF_UNIX: address format is UNIX pathname
AF_INET: address format is host and port number Common types are:
virtual circuit: received in order transmitted and reliably
datagram: arbitrary order, unreliable
Each socket type has one or more protocols. Ex:
TCP/IP (virtual circuits)
UDP (datagram)
Use of sockets:
Connection–based sockets communicate client-server: the server waits for a connection from the client
Connectionless sockets are peer-to-peer: each process is symmetric.
Socket APIs
socket: creates a socket of a given domain, type, protocol (buy a phone)
bind: assigns a name to the socket (get a telephone number)
listen: specifies the number of pending connections that can be queued for a server socket. (call waiting
allowance)
accept: server accepts a connection request from a client (answer phone)
connect: client requests a connection request to a server (call)
send, sendto: write to connection (speak)
ecv, recvfrom: read from connection (listen)
shutdown: end the call
Connection-based communication
Server performs the following actions
socket: create the socket
bind: give the address of the socket on the server
listen: specifies the maximum number of connection requests that can be pending for this process
accept: establish the connection with a specific client
send, recv: stream-based equivalents of read and write (repeated)
shutdown: end reading or writing
close: release kernel data structures
TCP-based sockets
socket API
#include<sys/types.h>
#include<sys /socket.h>
int socket(int domain, int type, int protocol) ;
Returns a file descriptor (called a socket ID) if successful, -1 otherwise. Note that the socket returns a socket
descriptor which is the same as a file descriptor.
The domain is AF_INET.
The type argument can be:
SOCK_STREAM: Establishes a virtual circuit for stream
SOCK_DGRAM: Establishes a datagram for communication
SOCK_SEQPACKET: Establishes a reliable, connection based, two way communication with maximum
message size. (This is not available on most machines.)
protocol is usually zero, so that type defines the connection within domain.
bind
#include <sys / types.h>
#include<sys / socket.h>
int bind(int sid, struct sockaddr *addrPtr, int len)
Where
sid: is the socket id
addrPtr: is a pointer to the address family dependent address structure
len: is the size of *addrPtr
Associates a socket id with an address to which other processes can connect. In internet protocol the address is
[ipNumber, portNumber]
sockaddr
For the internet family:
struct sockaddr_in {
sa_family_t sin_family; // = AF INET
in_port_t sin_port; // is a port number
struct in_addr sin_addr; // an IP address
}
listen
#include <sys / types.h>
#include <sys / socket.h>
int listen (int sid, int size) ;
Where size it the number of pending connection requests allowed (typically limited by Unix kernels to 5).
Returns the 0 on success, or -1 if failure.
accept
#include <sys / types.h>
#include <sys / socket.h>
int accept(int sid ,struct sockaddr *addrPtr , int *lenPtr )
Returns the socketId and address of client connecting to socket.
if lenPtr or addrPtr equal zero, no address structure is returned.
lenPtr is the maximum size of address structure that can be called, returns the actual value.
Waits for an incoming request, and when received creates a socket for it.
send
#include <sys / types.h>
#include <sys / socket.h>
int send(int sid ,const char *bufferPtr ,int len ,int flag)
Send a message. Returns the number of bytes sent or -1 if failure.
(Must be a bound socket).
flag is either
0: default
MSG OOB: Out-of-band high priority communication
recv
#include <sys / types.h>
#include <sys / socket.h>
int recv ( int sid , char *bufferPtr , int len , int flags)
Receive up to len bytes in bufferPtr. Returns the number of bytes received or -1 on failure.
flags can be either
0: default
MSG OOB: out-of-bound message
MSG PEEK: look at message without removing
Shutdown
#include <sys / types.h>
#include <sys / socket.h>
Connect
-this is the first of the client calls
#include <sys / types.h>
#include <sys / socket.h>
int connect ( int sid , struct sockaddr *addrPtr , int len)
Specifies the destination to form a connection with (addrPtr), and returns a 0 if successful, -1 otherwise.
Port usage
Note that the initiator of communications needs a fixed port to target communications.
This means that some ports must be reserved for these ―well known‖ ports.
Port usage:
0-1023: These ports can only be binded to by
root 1024-5000: well known ports
5001-64K-1: ephemeral ports
import java.util.*;
import java.net.*;
import java.io.*;
public class tcpclient
{
public static void main(String args[])
{
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());
}
}
}
// Client Program
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.
c. DatagramPacket(byte[] buffer, int length, InetAddress address, int port):it creates a datagram
packet. This constructor is used to send the packets.
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();
}
}
}
Dept. of CSE (DS), BIT, 2024- 53
2025
Computer Network Laboratory BCS502)
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 :
Write a program for simple RSA algorithm to encrypt and decrypt the data.
The RSA algorithm can be used for both public key encryption and digital signatures. Its security is based on the
difficulty of factoring large integers.
The RSA algorithm's efficiency requires a fast method for performing the modular exponentiation operation. A
less efficient, conventional method includes raising a number (the input) to a power (the secret or public key of
the algorithm, denoted e and d, respectively) and taking the remainder of the division with N. A straight-forward
implementation performs these two steps of the operation sequentially: first, raise it to the power and second,
apply modulo.
A very simple example of RSA encryption
This is an extremely simple example using numbers you can work out on a pocket calculator (those of you over
the age of 35 can probably even do it by hand on paper).
1. Select primes p = 11, q = 3.
2. n = pq = 11.3 = 33
phi = (p-1)(q-1) = 10.2 = 20
3. Choose e=3
Check gcd(e, p-1) = gcd(3, 10) = 1 (i.e. 3 and 10 have no common factors except 1),
and check gcd(e, q-1) = gcd(3, 2) = 1
therefore gcd(e, phi) = gcd(e, (p-1)(q-1)) = gcd(3, 20) = 1
4. Compute d such that ed ≡ 1 (mod phi)
^-1 ^-1
e i.e. compute d = mod phi = 3 mod 20
i.e. find a value for d such that phi divides (ed-1)
i.e. find d such that 20 divides 3d-1.
Simple testing (d = 1, 2, ...) gives d = 7
Check: ed-1 = 3.7 - 1 = 20, which is divisible by phi.
5. Public key = (n, e) = (33, 3)
Private key = (n, d) = (33, 7).
This is actually the smallest possible value for the modulus n for which the RSA algorithm works.
Now say we want to encrypt the message m = 7,
^e ^3
Remember that calculating m^e mod n is easy, but calculating the inverse c^-e mod n is very difficult, well, for
large n's anyway. However, if we can factor n into its prime factors p and q, the solution becomes easy again, even
for large n's. Obviously, if we can get hold of the secret exponent d, the solution is easy, too.
Code –
import java.util.*;
import java.math.*;
public class RSA
{
static BigInteger p,q,e,d,n,phi;
static int bitLength=256;
static Scanner S=new Scanner(System.in);
static Random R=new Random();
public static void main (String args[])
{ p=BigInteger.probablePrime(bitLength,R);
q=BigInteger.probablePrime(bitLength,R);
n=p.multiply(q);
e=BigInteger.probablePrime(bitLength/2,R);
phi=p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
while(phi.gcd(e).compareTo(BigInteger.ONE)!=0 && e.compareTo(phi)<0)
e.add(BigInteger.ONE);
d=e.modInverse(phi);
String msg="";
System.out.print("Enter The Msg : ");
msg=S.nextLine();
byte msg_arr[]=msg.getBytes();
System.out.println("Msg Byte Array :
"+display(msg_arr)); byte en[]=encrypt(msg_arr);
System.out.println("Encrypted Byte Array :
"+display(en)); byte de[]=decrypt(en);
System.out.println("Decrypted Byte Array : "+display(de));
System.out.println("Received Msg : "+ new String(de));
}
static byte[] encrypt(byte a[])
{ return (new BigInteger(a).modPow(e,n)).toByteArray(); }
static byte[] decrypt(byte a[])
{ return (new BigInteger(a).modPow(d,n)).toByteArray(); }
Output 1-
Enter The Msg : HELLO WORLD!2@
Msg Byte Array : 7269767679328779827668335064
Encrypted Byte Array : 33-121-10895123127-921071446103-13-98-31-4012178-95-6233-5913106-65-49-41-60112-
556-49131010683-451390-5399-5835-1088589-31-7722-125104-101-114108419566-58-126-12733-1657-69
Decrypted Byte Array : 7269767679328779827668335064
Received Msg : HELLO WORLD!2@
Output 2-
Enter The Msg : This is a sample
Msg Byte Array:841041051153210511532973211597109112108101
Encrypted Byte Array: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 Byte Array:841041051153210511532973211597109112108101
Received Msg: This is a sample
Output 3–
Enter The Msg: rsa algorithm
Msg Byte Array:114115973297108103111114105116104109
Encrypted Byte Array: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 Byte Array:114115973297108103111114105116104109
Received Msg:rsa algorithm
Lab Program 10 :
The main concept of the leaky bucket algorithm is that the output data flow remains constant despite the variant
input traffic, such as the water flow in a bucket with a small hole at the bottom. In case the bucket contains water
(or packets) then the output flow follows a constant rate, while if the bucket is full any additional load will be lost
because of spillover. In a similar way if the bucket is empty the output will be zero. From network perspective,
leaky bucket consists of a finite queue (bucket) where all the incoming packets are stored in case there is space in
the queue, otherwise the packets are discarded. In order to regulate the output flow, leaky bucket transmits one
packet from the queue in a fixed time (e.g. at every clock tick). In the following figure we can notice the main
rationale of leaky bucket algorithm, for both the two approaches (e.g. leaky bucket with water (a) and with
packets (b)).
Implementation Algorithm:
Steps:
1. Read The Data For Packets
2. Read The Queue Size
3. Divide the Data into Packets
4. Assign the random Propagation delays for each packets to input into the bucket (input_packet).
5. wlile((Clock++<5*total_packets)and
(out_packets< total_paclets))
a. if (clock == input_packet)
i. insert into Queue
b. if (clock % 5 == 0 )
i. Remove paclet from Queue
6. End
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);
}
}
while(bucket!=0)
{
System.out.print((++i)+"\t0\t"+bucket+"\tAccept\t\t"+min(bucket,op_rate)+"\t");
bucket=sub(bucket,op_rate);
}
}
1 6 6 Accept 6
2 8 8 Accept 7
3 9 8 Reject 2 7
4 5 6 Accept 6
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