A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
first.py
Go to the documentation of this file.
2# SPDX-License-Identifier: GPL-2.0-only
3#
4
5try:
6 from ns import ns
7except ModuleNotFoundError:
8 raise SystemExit(
9 "Error: ns3 Python module not found;"
10 " Python bindings may not be enabled"
11 " or your PYTHONPATH might not be properly configured"
12 )
13
14# // Default Network Topology
15# //
16# // 10.1.1.0
17# // n0 -------------- n1
18# // point-to-point
19# //
20
21ns.LogComponentEnable("UdpEchoClientApplication", ns.LOG_LEVEL_INFO)
22ns.LogComponentEnable("UdpEchoServerApplication", ns.LOG_LEVEL_INFO)
23
24nodes = ns.NodeContainer()
25nodes.Create(2)
26
27pointToPoint = ns.PointToPointHelper()
28pointToPoint.SetDeviceAttribute("DataRate", ns.StringValue("5Mbps"))
29pointToPoint.SetChannelAttribute("Delay", ns.StringValue("2ms"))
30
31devices = pointToPoint.Install(nodes)
32
33stack = ns.InternetStackHelper()
34stack.Install(nodes)
35
36address = ns.Ipv4AddressHelper()
37address.SetBase(ns.Ipv4Address("10.1.1.0"), ns.Ipv4Mask("255.255.255.0"))
38
39interfaces = address.Assign(devices)
40
41echoServer = ns.UdpEchoServerHelper(9)
42
43serverApps = echoServer.Install(nodes.Get(1))
44serverApps.Start(ns.Seconds(1.0))
45serverApps.Stop(ns.Seconds(10.0))
46
47address = interfaces.GetAddress(1).ConvertTo()
48echoClient = ns.UdpEchoClientHelper(address, 9)
49echoClient.SetAttribute("MaxPackets", ns.UintegerValue(1))
50echoClient.SetAttribute("Interval", ns.TimeValue(ns.Seconds(1.0)))
51echoClient.SetAttribute("PacketSize", ns.UintegerValue(1024))
52
53clientApps = echoClient.Install(nodes.Get(0))
54clientApps.Start(ns.Seconds(2.0))
55clientApps.Stop(ns.Seconds(10.0))
56
57ns.Simulator.Run()
58ns.Simulator.Destroy()