From a943ad21aae1ac788c1bbb561fc51eb7154033c6 Mon Sep 17 00:00:00 2001 From: RDhir Date: Wed, 15 Nov 2017 15:54:38 +0530 Subject: [PATCH 1/2] added udp trace client server python file --- udp/udp-tracer-server-client.py | 120 ++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 udp/udp-tracer-server-client.py diff --git a/udp/udp-tracer-server-client.py b/udp/udp-tracer-server-client.py new file mode 100644 index 0000000..9f62622 --- /dev/null +++ b/udp/udp-tracer-server-client.py @@ -0,0 +1,120 @@ +# -*- Mode: Python; -*- + + +import ns.core +import ns.csma +import ns.applications +import ns.internet + +def main(argv): + # + # Users may find it convenient to turn on explicit debugging + # for selected modules; the below lines suggest how to do this + # + ns.core.LogComponentEnable("UdpTraceClient", ns.core.LOG_LEVEL_INFO) + ns.core.LogComponentEnable("UdpServer", ns.core.LOG_LEVEL_INFO) + + # + # Allow the user to override any of the defaults at + # run-time, via command-line arguments + # + cmd = ns.core.CommandLine () + cmd.useIpv6 = "False" + cmd.AddValue ("useIpv6", "Use Ipv6") + cmd.Parse (argv) + + # + # Explicitly create the nodes required by the topology (shown above). + # + print "Create nodes." + n = ns.network.NodeContainer () + n.Create (2) + + internetstack = ns.internet.InternetStackHelper () + internetstack.Install (n) + + # + # Explicitly create the channels required by the topology (shown above). + # + print "Create channels." + csma = ns.csma.CsmaHelper () + csma.SetChannelAttribute ("DataRate", ns.core.StringValue ("5000000")) + csma.SetChannelAttribute ("Delay", ns.core.TimeValue (ns.core.MilliSeconds (2))) + csma.SetDeviceAttribute ("Mtu", ns.core.UintegerValue (1400)) + d = csma.Install (n) + + # + # We've got the "hardware" in place. Now we need to add IP addresses. + # + print "Assign IP Addresses." + if (cmd.useIpv6 == "False"): + ipv4 = ns.internet.Ipv4AddressHelper () + ipv4.SetBase (ns.network.Ipv4Address ("10.1.1.0"), ns.network.Ipv4Mask ("255.255.255.0")) + i = ipv4.Assign (d) + serverAddress = ns.network.Address (i.GetAddress (1)) + + else: + ipv6 = ns.internet.Ipv6AddressHelper () + ipv6.SetBase (ns.network.Ipv6Address ("2001:0000:f00d:cafe::"), ns.network.Ipv6Prefix (64)) + i6 = ipv6.Assign (d) + serverAddress = ns.network.Address (i6.GetAddress (1, 1)) + + print "Create Applications." + # + # Create a UdpEchoServer application on node one. + # + port = 4000 # well-known echo port number + server = ns.applications.UdpEchoServerHelper (port) + serverapps = server.Install (n.Get (1)) + serverapps.Start (ns.core.Seconds (1.0)) + serverapps.Stop (ns.core.Seconds (10.0)) + + # + # Create a UdpEchoClient application to send UDP datagrams from node zero to + # node one + # + #packetSize = 1024 + #maxPacketCount = 1 + #interPacketInterval = ns.core.Seconds (1.0) + + #client = ns.applications.UdpEchoClientHelper (serverAddress, port) + #client.SetAttribute ("MaxPackets", ns.core.UintegerValue (maxPacketCount)) + #client.SetAttribute ("Interval", ns.core.TimeValue (interPacketInterval)) + #client.SetAttribute ("PacketSize", ns.core.UintegerValue (packetSize)) + + MaxPacketSize = 1472 + client = ns.applications.UdpTraceClientHelper (serverAddress, port, "") + # client= ns.applications.UdpTraceClientHelper(serverAddress,port,"") + client.SetAttribute ("MaxPacketSize", ns.core.UintegerValue (MaxPacketSize)) + + apps = client.Install (n) + apps.Start (ns.core.Seconds (2.0)) + apps.Stop (ns.core.Seconds (10.0)) + + # + # Users may find it convenient to initialize echo packets with actual data; + # the below lines suggest how to do this + # + # client.SetFill (apps.Get (0), "Hello World") + # + # client.SetFill (apps.Get (0), 0xa5, 1024) + # + # Following does not work as intended. + # fill = [0, 1, 2, 3, 4, 5, 6] + # client.SetFill (apps.Get (0), fill[0], 1024) + + asciitracer = ns.network.AsciiTraceHelper () + csma.EnableAsciiAll (asciitracer.CreateFileStream ("udp-trace.tr")) + csma.EnablePcapAll ("udp-trace", False) + + # + # Now, do the actual simulation. + # + print "Run Simulation." + ns.core.Simulator.Run () + ns.core.Simulator.Destroy () + print "Done." + +if __name__ == '__main__': + import sys +main (sys.argv) From b886dde865912453df21d7182065fe96098f496f Mon Sep 17 00:00:00 2001 From: RDhir Date: Wed, 15 Nov 2017 16:04:18 +0530 Subject: [PATCH 2/2] added udp trace client server python file --- udp/udp-tracer-server-client.py | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/udp/udp-tracer-server-client.py b/udp/udp-tracer-server-client.py index 9f62622..5c07b44 100644 --- a/udp/udp-tracer-server-client.py +++ b/udp/udp-tracer-server-client.py @@ -23,9 +23,9 @@ def main(argv): cmd.AddValue ("useIpv6", "Use Ipv6") cmd.Parse (argv) - # - # Explicitly create the nodes required by the topology (shown above). - # + + + print "Create nodes." n = ns.network.NodeContainer () n.Create (2) @@ -61,7 +61,7 @@ def main(argv): print "Create Applications." # - # Create a UdpEchoServer application on node one. + # port = 4000 # well-known echo port number server = ns.applications.UdpEchoServerHelper (port) @@ -69,18 +69,7 @@ def main(argv): serverapps.Start (ns.core.Seconds (1.0)) serverapps.Stop (ns.core.Seconds (10.0)) - # - # Create a UdpEchoClient application to send UDP datagrams from node zero to - # node one - # - #packetSize = 1024 - #maxPacketCount = 1 - #interPacketInterval = ns.core.Seconds (1.0) - #client = ns.applications.UdpEchoClientHelper (serverAddress, port) - #client.SetAttribute ("MaxPackets", ns.core.UintegerValue (maxPacketCount)) - #client.SetAttribute ("Interval", ns.core.TimeValue (interPacketInterval)) - #client.SetAttribute ("PacketSize", ns.core.UintegerValue (packetSize)) MaxPacketSize = 1472 client = ns.applications.UdpTraceClientHelper (serverAddress, port, "") @@ -91,17 +80,7 @@ def main(argv): apps.Start (ns.core.Seconds (2.0)) apps.Stop (ns.core.Seconds (10.0)) - # - # Users may find it convenient to initialize echo packets with actual data; - # the below lines suggest how to do this - # - # client.SetFill (apps.Get (0), "Hello World") - # - # client.SetFill (apps.Get (0), 0xa5, 1024) - # - # Following does not work as intended. - # fill = [0, 1, 2, 3, 4, 5, 6] - # client.SetFill (apps.Get (0), fill[0], 1024) + asciitracer = ns.network.AsciiTraceHelper () csma.EnableAsciiAll (asciitracer.CreateFileStream ("udp-trace.tr"))