0% found this document useful (0 votes)
4 views

TCP Socket Programming STC PLB

The document provides an introduction to network programming, focusing on TCP/IP and UDP protocols, and the use of Berkley sockets for client-server communication. It outlines the structure and components of NS2, a discrete-event driven network simulator, detailing the creation of network topologies, agents, and traffic sources using Tcl/OTcl and C++. Additionally, it covers simulation procedures, event scheduling, and visualization tools for analyzing network behavior.

Uploaded by

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

TCP Socket Programming STC PLB

The document provides an introduction to network programming, focusing on TCP/IP and UDP protocols, and the use of Berkley sockets for client-server communication. It outlines the structure and components of NS2, a discrete-event driven network simulator, detailing the creation of network topologies, agents, and traffic sources using Tcl/OTcl and C++. Additionally, it covers simulation procedures, event scheduling, and visualization tools for analyzing network behavior.

Uploaded by

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

Introduction to

Network
Programming
Dr. Padmalochan Bera
Associate Professor (CSE)
School of Electrical and Computer Sciences
IIT Bhubaneswar
TCP/IP
TCP/UDP
TCP/UDP
Process Communication
Process Communication
Berkley Sockets
Sockets
Client Server Communication
Socket Procedures
Client Server Communication
Socket Creation in C
Socket close
Specifying Address
Assigning Address to Socket:
bind( )
bind( ) example
Skipping bind( )
Assigning Address to Socket:
bind( )
Establish Connection: connect( )
Accepting Connection: accept( )
Exchanging Data with stream
socket
Exchanging Data with datagram
socket
Example: Echo server
Example: Echo server using
stream socket
Example: Echo server using
stream socket
Example: Echo server using
stream socket
Example: Echo server using
stream socket
Example: Echo server using
stream socket
Example: Echo server using
stream socket
Dealing with Blocking Calls
Dealing with Blocking Calls
Non-blocking sockets
Signals
Asynchronous I/O
Timeouts
Multi-tasking per Client Process
Example: Multi-tasking
Example: Multi-tasking
Multi-tasking per client thread
Multiple Recipients
Multiple Recipients: Broadcast
Multiple Recipients: Multicast
The End
Introduction
• NS2 history
– Modified from REAL network simulator
– Developed through VINT project at UCB
– NS1 vs. NS2

• NS version2 is a discrete-event driven and


object-oriented network simulator
– Type of simulation: continuous, discrete event, and combined

• Events  Packet and Timer


Retransmit TCP Packet
timer start R1 R2
Ack
Retransmit
Fundamental Skills (II)
• NS2 is written in C++ and OTcl
– OTcl = Tcl + OO
– C++ implements the code that executed frequently
– OTcl configures the system

set ns [new Simulator]


set n1 [new Node]
set n2 [new Node]
n1 n2
$ns duplex-link $n1 $n2 5Mb 2ms DropTail
NS2 and Tcl

1. Toolkit Command Language (Tcl/OTcl) scripts are written to set up/configure


network topologies.
2. C++ is used to write the Event Schedulers and Basic network component
objects.
3. Tcl/OTcl provides linkage for class hierarchy, object instantiation, variable
binding and command dispatching.
4. These compiled objects are made available to the OTcl interpreter through
an OTcl linkage that creates a matching OTcl object for each of the C++
objects and makes the control functions and the configurable variables
specified by the C++ object act as member functions and member variables
of the corresponding OTcl object.
NS-2 Generic Script Structure
1. Create Simulator object
2. Turn on tracing
3. Create topology
4. Setup link dynamics
5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulation
Step1: Create Simulator Object
• Create event scheduler
– set ns [new Simulator]
Step2: Turn on Tracing
• Insert immediately after scheduler!

• Trace packets on all links


set nf [open out.nam w]
$ns trace-all $nf

$ns namtrace-all $nf


Step2: Turn on Tracing cont…
Structure of Trace File:
NS-2 Generic Script Structure
1. Create Simulator object
2. Turn on tracing
3. Create topology
4. Setup link dynamics
5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulation
Step 3: Create network
 Two nodes, One link

n0

n1
Step 3: Create Network n0

• Nodes
– set n0 [$ns node]
– set n1 [$ns node] n1

• Links and queuing


– $ns duplex-link $n0 $n1 1Mb 10ms RED

– $ns duplex-link $n0 $n1 <bandwidth> <delay>


<queue_type>
– <queue_type>: DropTail, RED, etc.
Creating a larger topology
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms RED
}
NS-2 Generic Script Structure
1. Create Simulator object
2. Turn on tracing
3. Create topology
4. Setup link dynamics
5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
8. Start simulation
Step 4: Network Dynamics
• Link failures
– Hooks in routing module to reflect routing changes
• $ns rtmodel-at <time> up|down $n0
$n1

• For example:

$ns rtmodel-at 1.0 down $n0 $n1


$ns rtmodel-at 2.0 up $n0 $n1
Step 5: Creating UDP connection
udp
set udp [new Agent/UDP]
set null [new Agent/Null]
n0

$ns attach-agent $n0 $udp


$ns attach-agent $n1
$null n1

$ns connect $udp $null null


Step 6: Creating Traffic
(On Top of UDP) cbr

• CBR udp

– set cbr [new


Application/Traffic/CBR] n0

– $cbr set packetSize_ 500


– $cbr set interval_ 0.005
n1

– $cbr attach-agent $udp


null
Creating TCP connection
tcp
set tcp [new Agent/TCP]
set tcpsink [new Agent/TCPSink]
n0

$ns attach-agent $n0 $tcp


$ns attach-agent $n1 $tcpsink
n1
$ns connect $tcp $tcpsink
sink
Step 6: Creating Traffic
(On Top of TCP) ftp

• FTP tcp

– set ftp [new Application/FTP]


– $ftp attach-agent $tcp n0

• Telnet
– set telnet [new
Application/Telnet] n1
– $telnet attach-agent $tcp
sink
Recall: Generic Script Structure
1. set ns [new Simulator]
2. Turn on tracing
3. Create topology
4. Setup link dynamics
5. Create agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
Examples
8. Start simulation
Post-Processing Procedures
• Add a 'finish' procedure that closes the trace
file and starts nam.

proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
Run Simulation
• Schedule Events
$ns at <time> <event>
– <event>: any legitimate ns/tcl commands

$ns at 0.5 "$cbr start"


$ns at 4.5 "$cbr stop“

• Call ‘finish’
$ns at 5.0 "finish"

• Run the simulation


$ns run
Recall: Generic Script Structure
1. set ns [new Simulator]
2. Turn on tracing
3. Create topology
4. Setup link dynamics
5. Create routing agents
6. Create application and/or traffic sources
7. Post-processing procedures (i.e. nam)
Examples
8. Start simulation
Visualization Tools
• nam-1 (Network AniMator Version 1)
– Packet-level animation
– Well supported by ns
• xgraph
– Simulation results
nam Interface: Nodes
• Color
$node color red
• Shape (can’t be changed after sim starts)
$node shape box (circle, box,
hexagon)
• Label (single string)
$ns at 1.1 “$n0 label \”web cache 0\””
nam Interfaces: Links
• Color
$ns duplex-link-op $n0 $n1 color
"green"
• Label
$ns duplex-link-op $n0 $n1 label
“backbone"
nam Interface: Topology Layout
• “Manual” layout: specify everything
$ns duplex-link-op $n(0) $n(1) orient right
$ns duplex-link-op $n(1) $n(2) orient right
$ns duplex-link-op $n(2) $n(3) orient right
$ns duplex-link-op $n(3) $n(4) orient 60deg

• If anything missing  automatic layout


Simulation Example
Simulation Example cont…
#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)


$ns color 1 Blue
$ns color 2 Red

#Open the NAM trace file


set nf [open out.nam w]
$ns namtrace-all $nf
Simulation Example cont…
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
Simulation Example cont…
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create links between the nodes


$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail

#Set Queue Size of link (n2-n3) to 10


$ns queue-limit $n2 $n3 10
Simulation
#Give node position (for NAM)
Example cont…
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right

#Monitor the queue for link (n2-n3). (for NAM)


$ns duplex-link-op $n2 $n3 queuePos 0.5

#Setup a TCP connection


set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
Simulation Example cont…
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

#Setup a UDP connection


set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
Simulation Example cont…
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr set random_ false

#Schedule events for the CBR and FTP agents


$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
Simulation Example cont…
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of simulation time


$ns at 5.0 "finish"

#Print CBR packet size and interval


puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"

#Run the simulation


$ns run
Thank you…

You might also like