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

MC Exp4

Mobile computing lab experiments

Uploaded by

sanjanabhosle27
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)
4 views

MC Exp4

Mobile computing lab experiments

Uploaded by

sanjanabhosle27
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/ 8

EXPERIMENT NO: 4

Aim of the Experiment: -

To study how RTS/CTS helps in wireless networks, when:


1. No RTS/CTS is being sent.
2. Nodes do exchange RTS/CTS packets.
Compare the no. of packet retransmissions required in both the cases (as obtained
in the output) and compare the results.

Lab Outcome: -

Date of Performance: -

Date of Submission: -

Implementation Understanding Punctuality & Discipline Total Marks


(05) (05) (05) (15)

Practical In charge
EXPERIMENT NO. 04
Title: Illustration of Hidden Terminal/Exposed terminal Problem. Consider two Wi-fi
base stations (STA) and an access point (AP) located along the x-axis. All the nodes
are fixed. The AP is situated at the middle of the two STA, the distance of separation
being 150 m. [variable]. Node #0 and node #1 are the hidden terminals. Both are
transmitting some data to the AP (almost at same rate) at the same time. The loss
across the wireless link between each STA and the AP is fixed at 50 dB irrespective
of the distance of separation.

Aim: To study how RTS/CTS helps in wireless networks, when


1. No RTS/CTS is being sent.
2. Nodes do exchange RTS/CTS packets.
Compare the no. of packet retransmissions required in both the cases (as obtained
in the output) and compare the results.

Theory:
Hidden and exposed terminals:

Consider the scenario with three mobile phones as shown in Figure 1. The
transmission range of A reaches B, but not C (the detection range does not reach C
either). The transmission range of C reaches B, but not A. Finally, the transmission
range of B reaches A and C, i.e., A cannot detect C and vice versa. A starts sending to
B, C does not receive this transmission. C also wants to send something to B and
senses the medium. The medium appears to be free, the carrier sense fails. C also
starts sending causing a collision at B. But A cannot detect this collision at B and
continues with its transmission. A is hidden for C and vice versa.

Figure.1: Hidden and Exposed terminals


While hidden terminals may cause collisions, the next effect only causes unnecessary
delay. Now consider the situation that B sends something to A and C wants to
transmit data to some other mobile phone outside the interference ranges of A and B.
C senses the carrier and detects that the carrier is busy (B’s signal). C postpones its
transmission until it detects the medium as being idle again. But as A is outside the
interference range of C, waiting is not necessary. Causing a ‘collision’ at B does not
matter because the collision is too weak to propagate to A. In this situation, C is
exposed to B
Multiple access with collision avoidance:
Multiple access with collision avoidance (MACA) presents a simple scheme that solves the
hidden terminal problem. A and C both want to send to B. A has already started the
transmission, but is hidden for C; C also starts with its transmission, thereby causing a
collision at B.

Figure. 2:
With MACA, A does not start its transmission at once, but sends a request to send (RTS)
first. B receives the RTS that contains the name of sender and receiver, as well as the length of
the future transmission. This RTS is not heard by C, but triggers an acknowledgement from B,
called clear to send (CTS). The CTS again contains the names of sender (A) and receiver (B)
of the user data, and the length of the future transmission. This CTS is now heard by C and the
medium for future use by A is now reserved for the duration of the transmission. After
receiving a CTS, C is not allowed to send anything for the duration indicated in the CTS
toward B. A collision cannot occur at B during data transmission, and the hidden terminal
problem is solved – provided that the transmission conditions remain the same. (Another
station could move into the transmission range of B after the transmission of CTS.)

NS2 Implementation:
Here a simulator object has to be created. We have to start the simulation with the same
command, and if we want to run nam automatically, we will always have to open a trace
file, initialize it, and define a procedure which closes it and starts nam.

Now insert the following lines into the code to create four nodes. set n0

[$ns node]
set n1 [$ns node]
set n2 [$ns node] set n3
[$ns node]

The following piece of Tcl code creates three duplex links between the nodes.

$ns duplex-link $n0 $n2 1Mb 10ms DropTail


$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 1Mb 10ms DropTail

Add the next three lines to your Tcl script and start it again.
$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

The events:
Now we create two UDP agents with CBR traffic sources and attach them to the nodes
n0 and n1. Then we create a Null agent and attach it to node n3.

#Create a UDP agent and attach it to node n0 set


udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0 set


cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

#Create a UDP agent and attach it to node n1 set


udp1 [new Agent/UDP]
$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1 set


cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1 set null0
[new Agent/Null]
$ns attach-agent $n3 $null0
The two CBR agents have to be connected to the Null agent.
$ns connect $udp0 $null0
$ns connect $udp1 $null0

We want the first CBR agent to start sending at 0.5 seconds and to stop at 4.5 seconds while
the second CBR agent starts at 1.0 seconds and stops at 4.0 seconds.
$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
Marking flows:
Add the following two lines to your CBR agent definitions.
$udp0 set class_ 1
$udp1 set class_ 2

The parameter 'fid_' stands for 'flow id'.


Now add the following piece of code to your Tcl script, preferably at the beginning after the
simulator object has been created, since this is a part of the simulator setup.

$ns color 1 Blue


$ns color 2 Red

This code allows you to set different colors for each flow id.

Wired

Program:

import simpy
import networkx as nx
import matplotlib.pyplot as plt
env = simpy.Environment()
n0 = {"label": "Client1", "color": "blue"}
n1 = {"label": "Server", "color": "red", "shape": "hexagon"}
n2 = {"label": "Client2", "color": "green", "shape": "square"}
links = [((0, "Client1"), (1, "Server")), ((1, "Server"), (2, "Client2"))]
G = nx.DiGraph()
for node in [n0, n1, n2]:
G.add_node(node["label"], color=node.get("color", "gray"), shape=node.get("shape", "ellipse"))

for link in links:


G.add_edge(link[0][1], link[1][1])
def create_link(env, node1, node2, bandwidth, delay):
link = Link(env, node1, node2, bandwidth, delay)
return link

class Link:
def __init__(self, env, node1, node2, bandwidth, delay):
self.env = env
self.node1 = node1
self.node2 = node2
self.bandwidth = bandwidth
self.delay = delay
self.action = env.process(self.run())

def run(self):
yield self.env.timeout(0) # Initial delay
print(f"Link between {self.node1['label']} and {self.node2['label']} established.")

for link in links:


create_link(env, link[0], link[1], 2, 100)

pos = nx.spring_layout(G)
ellipse_nodes = [node for node, data in G.nodes(data=True) if data["shape"] == "ellipse"]
hexagon_nodes = [node for node, data in G.nodes(data=True) if data["shape"] == "hexagon"]
square_nodes = [node for node, data in G.nodes(data=True) if data["shape"] == "square"]

nx.draw_networkx_nodes(G, pos, nodelist=ellipse_nodes, node_color="blue", node_size=700)


nx.draw_networkx_nodes(G, pos, nodelist=hexagon_nodes, node_color="red", node_size=700,
node_shape="v")
nx.draw_networkx_nodes(G, pos, nodelist=square_nodes, node_color="green", node_size=700,
node_shape="s")
nx.draw_networkx_edges(G, pos)
plt.show()
print("Simulation finished.")
Output:

WIRELESS

Program:

import simpy
import networkx as nx
import matplotlib.pyplot as plt

env = simpy.Environment()
node_positions = [(0, 0), (1, 0), (2, 0), (1, 1)] # Example positions, adjust as needed
nodes = [simpy.Container(env) for _ in range(len(node_positions))]

links = [
(0, 1, {"bandwidth": 2e6, "delay": 1e-3}),
(1, 2, {"bandwidth": 2.5e6, "delay": 1e-3}),
(2, 3, {"bandwidth": 2e6, "delay": 1.5e-3}),
(3, 1, {"bandwidth": 12e6, "delay": 1e-2}),
]

G = nx.DiGraph()
G.add_nodes_from(range(len(node_positions)))
G.add_edges_from(links)

tcp_agents = [simpy.Container(env) for _ in range(len(node_positions))]


sink_agents = [simpy.Container(env) for _ in range(len(node_positions))]
for i in range(len(node_positions)):
nodes[i].tcp_agent = tcp_agents[i]
nodes[i].sink_agent = sink_agents[i]
def ftp_traffic(env, src, dest):
yield env.timeout(1.0)
dest.tcp_agent.put(1)

for i in range(len(node_positions)):
env.process(ftp_traffic(env, nodes[i], nodes[(i + 2) % len(node_positions)]))
def visualize_graph(G):
pos = nx.spring_layout(G)
edge_styles = [(u, v, {"dashed": True}) for u, v in G.edges()]
nx.draw(G, pos, with_labels=True, node_color="skyblue", node_size=700, font_color="black",
font_weight="bold", edgelist=edge_styles, connectionstyle="arc3,rad=0.1")
plt.show()
env.run(until=3.0)
visualize_graph(G)
print("Simulation finished.")

Output:

CONCLUSION:

Thus, we have studied how RTS/CTS help in wireless networks.

You might also like