To simulate Simple Mail Transfer Protocol (SMTP) using NS2, which encompasses designing the behaviour of SMTP that is an application-layer protocol utilized for transmitting email messages among the clients (email senders) and servers (email recipients). SMTP depends on TCP as its transport protocol for reliable data transfer.
Even though NS2 doesn’t directly support an SMTP-specific application model such as it does for FTP or HTTP, we can replicate SMTP traffic by making a generic TCP connection among the nodes, which signifies the communication between a mail client (sender) and a mail server (recipient). It permits to model the data transfer included in transmitting an email over TCP.
Below is a step-by-step guide on how to simulate SMTP-like behaviour utilizing NS2 by replicating a mail client and a mail server exchanging data over a TCP connection.
Steps to Simulate SMTP Projects Using NS2
- Install NS2
Make sure we have NS2 installed on the machine. We can download NS2 from here.
- Understanding SMTP Traffic
In an SMTP simulation:
- Mail Client (Sender): It will introduce the SMTP connection to the mail server to transmit an email.
- Mail Server (Receiver): This will accept the connection from the client and receive the email.
- TCP: SMTP utilizes TCP to make sure reliable communication.
- TCL Script for SMTP-like Traffic Simulation
In the following TCL script, we replicate a TCP connection among two nodes: a mail client and a mail server. The mail client introduces a connection to the mail server to mimic the transmitting of an email message.
Example TCL Script for SMTP Simulation
# Create a simulator object
set ns [new Simulator]
# Open files for tracing and NAM visualization
set tracefile [open smtp_simulation.tr w]
$ns trace-all $tracefile
set namfile [open smtp_simulation.nam w]
$ns namtrace-all $namfile
# Define network topology
set topo [new Topography]
$topo load_flatgrid 500 500
# Create two nodes: one will act as the SMTP client (mail sender) and the other as the SMTP server (mail recipient)
set client [$ns node]
set server [$ns node]
# Define link parameters: bandwidth, delay, and queue type
set bw 10Mb
set delay 10ms
set queue DropTail
# Create a duplex link between the client and the server
$ns duplex-link $client $server $bw $delay $queue
# Attach a TCP agent to the client node (simulating the SMTP client initiating the connection)
set tcp [new Agent/TCP]
$ns attach-agent $client $tcp
# Attach a TCP Sink agent to the server node (simulating the SMTP server receiving the email)
set sink [new Agent/TCPSink]
$ns attach-agent $server $sink
# Connect the TCP agent (SMTP client) and TCP Sink agent (SMTP server)
$ns connect $tcp $sink
# Create an Application/Traffic/CBR to simulate the email content being sent over TCP
set smtp_traffic [new Application/Traffic/CBR]
$smtp_traffic set packetSize_ 1000 ;# Size of an email packet
$smtp_traffic set rate_ 1Mb ;# Rate at which email content is sent
$smtp_traffic attach-agent $tcp
# Start SMTP traffic at 1.0 second and stop it at 5.0 seconds
$ns at 1.0 “$smtp_traffic start”
$ns at 5.0 “$smtp_traffic stop”
# End simulation after 10 seconds
$ns at 10.0 “finish”
# Define the finish procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
exec nam smtp_simulation.nam &
exit 0
}
# Run the simulation
$ns run
- Explanation of the Script
- Simulator Setup: The simulator object is made, and trace files (smtp_simulation.tr) and NAM files (smtp_simulation.nam) are opened for recording the events of the simulation.
- Node Creation: Two nodes are made to replicate the SMTP client (email sender) and SMTP server (email recipient). A duplex link with a bandwidth of 10 Mbps and a delay of 10 ms associates these two nodes, replicating the network over which the email is sent.
- TCP and Traffic Setup: A TCP agent is attached to the client node, and a TCP Sink agent is connected to the server node. This models the TCP connection utilised by SMTP. An application generating CBR (Constant Bit Rate) traffic is used to replicate the email content being transmitting from the client to the server.
- Traffic Generation: SMTP traffic is generated beginning at 1.0 seconds and stopped at 5.0 seconds. During this period, the SMTP client transmits email content (packets) to the server over the TCP connection.
- Simulation End: The simulation stops after 10 seconds, with the SMTP-like traffic flow happening among the client and the server in the first 5 seconds.
- Running the Simulation
We can save the above TCL script as smtp_simulation.tcl, and run it using the following command in the terminal:
ns smtp_simulation.tcl
It will generate a trace file (smtp_simulation.tr) and a NAM file (smtp_simulation.nam).
- Analyzing the Results
- NAM Visualization: We can envision the network and monitor the email traffic (SMTP-like communication) among the client and server using NAM (Network Animator). Open the generated .nam file using the below command:
nam smtp_simulation.nam
NAM will indicate the network topology, packet flow, and the TCP connection among the SMTP client and server as the email is sent.
- Trace File Analysis: The .tr trace file records the events of the simulation, containing the packet transmission, receptions, and any potential packet losses or delays. We can utilize this trace file to examine the performance of the SMTP-like traffic. We can write AWK or Python scripts to extract key performance parameters like:
- Packet Delivery Ratio (PDR)
- End-to-End Delay
- Throughput
Example AWK Script for Analyzing Throughput:
Below is an AWK script to compute the throughput from the trace file:
BEGIN { received_bytes = 0; start_time = 1; end_time = 5 }
{
if ($1 == “r” && $4 == “tcp”) {
received_bytes += $5
}
}
END {
duration = end_time – start_time
throughput = (received_bytes * 8) / (duration * 1000000) # Convert to Mbps
print “Throughput: “, throughput, “Mbps”
}
To use this script, we can save it as throughput.awk and run it on the trace file as follows:
awk -f throughput.awk smtp_simulation.tr
- Extending the Simulation
We can extend the simulation to contain more complex SMTP-like traffic scenarios, like:
7.1. Multiple SMTP Clients and Servers
Add more nodes to the simulation to represent multiple mail clients and servers, simulating multiple email exchanges happening simultaneously.
# Add additional SMTP clients and servers
set client2 [$ns node]
set server2 [$ns node]
# Create links between new client-server pairs
$ns duplex-link $client2 $server2 $bw $delay $queue
# Attach TCP agents to the new nodes
set tcp2 [new Agent/TCP]
$ns attach-agent $client2 $tcp2
set sink2 [new Agent/TCPSink]
$ns attach-agent $server2 $sink2
# Connect the second TCP agent and sink
$ns connect $tcp2 $sink2
# Create a second SMTP-like traffic generator
set smtp_traffic2 [new Application/Traffic/CBR]
$smtp_traffic2 set packetSize_ 1000
$smtp_traffic2 set rate_ 512Kb
$smtp_traffic2 attach-agent $tcp2
# Start and stop the second traffic generator
$ns at 3.0 “$smtp_traffic2 start”
$ns at 8.0 “$smtp_traffic2 stop”
7.2. Varying Link Characteristics
We can change the link bandwidth, delay, and queue types to monitor the impacts of distinct network conditions on the performance of SMTP traffic.
7.3. Introducing Packet Loss
Introduce packet loss to replicate network failures and learn how SMTP (using TCP) manages retransmissions and conserves reliable data transfer.
# Add packet loss to the link
$ns lossmodel [new ErrorModel/Uniform] 0.01 $client $server
Through the entire process, you can acquire the simulation and execution process regarding the Simple Mail Transfer Protocol offered in it using ns2 tool. We offer more information regarding the Simple Mail Transfer Protocol in another manual.
Check out phdprime.com for simulating Simple Mail Transfer Protocol projects using the NS2 approach. Our talented team is dedicated to bringing these projects to life with the NS2 tool and sharing top-notch project ideas and topics in this field. Reach out to us, and we’ll provide you with the best research support!