A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
first.py
Go to the documentation of this file.
2# This program is free software; you can redistribute it and/or modify
3# it under the terms of the GNU General Public License version 2 as
4# published by the Free Software Foundation;
5#
6# This program is distributed in the hope that it will be useful,
7# but WITHOUT ANY WARRANTY; without even the implied warranty of
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9# GNU General Public License for more details.
10#
11# You should have received a copy of the GNU General Public License
12# along with this program; if not, write to the Free Software
13# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14#
15
16try:
17 from ns import ns
18except ModuleNotFoundError:
19 raise SystemExit(
20 "Error: ns3 Python module not found;"
21 " Python bindings may not be enabled"
22 " or your PYTHONPATH might not be properly configured"
23 )
24
25# // Default Network Topology
26# //
27# // 10.1.1.0
28# // n0 -------------- n1
29# // point-to-point
30# //
31
32ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
33ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
34
35nodes = ns.network.NodeContainer()
36nodes.Create(2)
37
38pointToPoint = ns.point_to_point.PointToPointHelper()
39pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
40pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
41
42devices = pointToPoint.Install(nodes)
43
44stack = ns.internet.InternetStackHelper()
45stack.Install(nodes)
46
47address = ns.internet.Ipv4AddressHelper()
48address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
49
50interfaces = address.Assign(devices)
51
52echoServer = ns.applications.UdpEchoServerHelper(9)
53
54serverApps = echoServer.Install(nodes.Get(1))
55serverApps.Start(ns.core.Seconds(1.0))
56serverApps.Stop(ns.core.Seconds(10.0))
57
58address = interfaces.GetAddress(1).ConvertTo()
59echoClient = ns.applications.UdpEchoClientHelper(address, 9)
60echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
61echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
62echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
63
64clientApps = echoClient.Install(nodes.Get(0))
65clientApps.Start(ns.core.Seconds(2.0))
66clientApps.Stop(ns.core.Seconds(10.0))
67
68ns.core.Simulator.Run()
69ns.core.Simulator.Destroy()