def main(argv): cmd = ns.core.CommandLine() cmd.Parse(argv) ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl")) print "Create nodes." n = ns.network.NodeContainer() n.Create(4) internet = ns.internet.InternetStackHelper() internet.Install(n) print ("Create channels.") csma = ns.csma.CsmaHelper() csma.SetChannelAttribute("DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000))) csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2))); csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400)) d = csma.Install(n) print ("Assign IP Addresses.") ipv4 = ns.internet.Ipv4AddressHelper() ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0")) i = ipv4.Assign(d) print ("Create Applications.") port = 9 server = ns.applications.UdpEchoServerHelper(port) apps = server.Install(n.Get(1)) apps.Start(ns.core.Seconds(1.0)) apps.Stop(ns.core.Seconds(10.0)) packetSize = 1024 maxPacketCount = 500 interPacketInterval = ns.core.Seconds(0.01) client = ns.applications.UdpEchoClientHelper(i.GetAddress (1), port) client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount)) client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval)) client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize)) apps = client.Install(n.Get(0)) apps.Start(ns.core.Seconds(2.0)) apps.Stop(ns.core.Seconds(10.0)) ascii = ns.network.AsciiTraceHelper() csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr")) csma.EnablePcapAll("realtime-udp-echo", False) print ("Run Simulation.") ns.core.Simulator.Run() ns.core.Simulator.Destroy() print ("Done.")
def main(argv): # # Allow the user to override any of the defaults and the above Bind() at # run-time, via command-line arguments # cmd = ns.core.CommandLine() cmd.Parse(argv) # # But since this is a realtime script, don't allow the user to mess with # that. # ns.core.GlobalValue.Bind("SimulatorImplementationType", ns.core.StringValue("ns3::RealtimeSimulatorImpl")) # # Explicitly create the nodes required by the topology (shown above). # print "Create nodes." n = ns.network.NodeContainer() n.Create(4) internet = ns.internet.InternetStackHelper() internet.Install(n) # # Explicitly create the channels required by the topology (shown above). # print("Create channels.") csma = ns.csma.CsmaHelper() csma.SetChannelAttribute( "DataRate", ns.network.DataRateValue(ns.network.DataRate(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.") ipv4 = ns.internet.Ipv4AddressHelper() ipv4.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0")) i = ipv4.Assign(d) print("Create Applications.") # # Create a UdpEchoServer application on node one. # port = 9 # well-known echo port number server = ns.applications.UdpEchoServerHelper(port) apps = server.Install(n.Get(1)) apps.Start(ns.core.Seconds(1.0)) apps.Stop(ns.core.Seconds(10.0)) # # Create a UdpEchoClient application to send UDP datagrams from node zero to # node one. # packetSize = 1024 maxPacketCount = 500 interPacketInterval = ns.core.Seconds(0.01) client = ns.applications.UdpEchoClientHelper(i.GetAddress(1), port) client.SetAttribute("MaxPackets", ns.core.UintegerValue(maxPacketCount)) client.SetAttribute("Interval", ns.core.TimeValue(interPacketInterval)) client.SetAttribute("PacketSize", ns.core.UintegerValue(packetSize)) apps = client.Install(n.Get(0)) apps.Start(ns.core.Seconds(2.0)) apps.Stop(ns.core.Seconds(10.0)) ascii = ns.network.AsciiTraceHelper() csma.EnableAsciiAll(ascii.CreateFileStream("realtime-udp-echo.tr")) csma.EnablePcapAll("realtime-udp-echo", False) # # Now, do the actual simulation. # print("Run Simulation.") ns.core.Simulator.Run() ns.core.Simulator.Destroy() print("Done.")
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("UdpEchoClientApplication", ns.core.LOG_LEVEL_ALL) # ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_ALL) # # 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 (4) 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 = 9 # 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)) apps = client.Install (n.Get (0)) 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-echo-py.tr")) csma.EnablePcapAll ("udp-echo-py", False) # # Now, do the actual simulation. # print ("Run Simulation.") ns.core.Simulator.Run () ns.core.Simulator.Destroy () print ("Done.")
def main(argv): # # Allow the user to override any of the defaults and the above Bind() at # run-time, via command-line arguments # cmd = ns.core.CommandLine() cmd.packetSize = 1024 cmd.packetCount = 10 cmd.packetInterval = 1.0 #Socket options for IPv6, currently TCLASS, HOPLIMIT, RECVTCLASS, and RECVHOPLIMIT cmd.ipv6Tclass = 0 cmd.ipv6RecvTclass = True cmd.ipv6Hoplimit = 0 cmd.ipv6RecvHoplimit = True cmd.AddValue("PacketSize", "Packet size in bytes") cmd.AddValue("PacketCount", "Number of packets to send") cmd.AddValue("Interval", "Interval between packets") cmd.AddValue("IPV6_TCLASS", "IPV6_TCLASS") cmd.AddValue("IPV6_RECVTCLASS", "IPV6_RECVTCLASS") cmd.AddValue("IPV6_HOPLIMIT", "IPV6_HOPLIMIT") cmd.AddValue("IPV6_RECVHOPLIMIT", "IPV6_RECVHOPLIMIT") cmd.Parse(argv) packetSize = int(cmd.packetSize) packetCount = int(cmd.packetCount) packetInterval = float(cmd.packetInterval) ipv6Tclass = int(cmd.ipv6Tclass) ipv6RecvTclass = bool(cmd.ipv6RecvTclass) ipv6Hoplimit = int(cmd.ipv6Hoplimit) ipv6RecvHoplimit = bool(cmd.ipv6RecvHoplimit) print("Create nodes.") n = ns.network.NodeContainer() n.Create(2) internet = ns.internet.InternetStackHelper() internet.Install(n) 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) print("Assign IP Addresses.") 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 sockets.") #Receiver socket on n1 tid = ns3.TypeId.LookupByName("ns3::UdpSocketFactory") recvSink = ns3.Socket.CreateSocket(n.Get(1), tid) local = ns3.Inet6SocketAddress(ns3.Ipv6Address.GetAny(), 4477) recvSink.SetIpv6RecvTclass(ipv6RecvTclass) recvSink.SetIpv6RecvHopLimit(ipv6RecvHoplimit) recvSink.Bind(local) recvSink.SetRecvCallback(ReceivePacket) #Sender socket on n0 source = ns3.Socket.CreateSocket(n.Get(0), tid) remote = ns3.Inet6SocketAddress(i6.GetAddress(1, 1), 4477) #Set socket options, it is also possible to set the options after the socket has been created/connected. if ipv6Tclass != 0: source.SetIpv6Tclass(ipv6Tclass) if ipv6Hoplimit > 0: source.SetIpv6HopLimit(ipv6Hoplimit) source.Connect(remote) ascii = ns.network.AsciiTraceHelper() csma.EnableAsciiAll(ascii.CreateFileStream("socket-options-ipv6-py.tr")) csma.EnablePcapAll("socket-options-ipv6", False) #Schedule SendPacket interPacketInterval = ns.core.Seconds(packetInterval) ns.core.Simulator.ScheduleWithContext(source.GetNode().GetId(), ns.core.Seconds(1.0), SendPacket, source, packetSize, packetCount, interPacketInterval) print("Run Simulation.") ns.core.Simulator.Run() ns.core.Simulator.Destroy() print("Done.")
import ns.internet import ns.network import ns.point_to_point import ns.csma import ns.internet_apps c = ns.network.NodeContainer() c.Create(4) csma = ns.csma.CsmaHelper() csma.SetChannelAttribute("DataRate", ns.core.StringValue("100Mbps")) csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.NanoSeconds(6560))) csma.SetDeviceAttribute ("EncapsulationMode", ns.core.StringValue ("Llc")); devs = csma.Install (c); stack = ns.internet.InternetStackHelper() stack.Install(c) ip = ns.internet.Ipv4AddressHelper() ip.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0")) addresses = ns.internet.Ipv4InterfaceContainer() addresses = ip.Assign(devs) ns.core.Config.SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", ns.core.StringValue ("2"))
def main(argv): # # Allow the user to override any of the defaults and the above Bind() at # run-time, via command-line arguments # cmd = ns.core.CommandLine() cmd.packetSize = 1024 cmd.packetCount = 10 cmd.packetInterval = 1.0 # Socket options for IPv4, currently TOS, TTL, RECVTOS, and RECVTTL cmd.ipTos = 0 cmd.ipRecvTos = True cmd.ipTtl = 0 cmd.ipRecvTtl = True cmd.AddValue("PacketSize", "Packet size in bytes") cmd.AddValue("PacketCount", "Number of packets to send") cmd.AddValue("Interval", "Interval between packets") cmd.AddValue("IP_TOS", "IP_TOS") cmd.AddValue("IP_RECVTOS", "IP_RECVTOS") cmd.AddValue("IP_TTL", "IP_TTL") cmd.AddValue("IP_RECVTTL", "IP_RECVTTL") cmd.Parse(argv) packetSize = int(cmd.packetSize) packetCount = int(cmd.packetCount) packetInterval = float(cmd.packetInterval) ipTos = int(cmd.ipTos) ipRecvTos = bool(cmd.ipRecvTos) ipTtl = int(cmd.ipTtl) ipRecvTtl = bool(cmd.ipRecvTtl) print("Create nodes.") n = ns.network.NodeContainer() n.Create(2) internet = ns.internet.InternetStackHelper() internet.Install(n) print("Create channels.") csma = ns.csma.CsmaHelper() csma.SetChannelAttribute( "DataRate", ns.network.DataRateValue(ns.network.DataRate(5000000))) csma.SetChannelAttribute("Delay", ns.core.TimeValue(ns.core.MilliSeconds(2))) csma.SetDeviceAttribute("Mtu", ns.core.UintegerValue(1400)) d = csma.Install(n) print("Assign IP addresses.") 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)) print("Create sockets.") # Receiver socket on n1 tid = ns3.TypeId.LookupByName("ns3::UdpSocketFactory") recvSink = ns3.Socket.CreateSocket(n.Get(1), tid) local = ns3.InetSocketAddress(ns3.Ipv4Address.GetAny(), 4477) recvSink.SetIpRecvTos(ipRecvTos) recvSink.SetIpRecvTtl(ipRecvTtl) recvSink.Bind(local) recvSink.SetRecvCallback(ReceivePacket) # Sender socket on n0 source = ns3.Socket.CreateSocket(n.Get(0), tid) remote = ns.network.InetSocketAddress(i.GetAddress(1), 4477) # Set socket options, it is also possible to set the options after the socket has been created/connected. if ipTos > 0: source.SetIpTos(ipTos) if ipTtl > 0: source.SetIpTtl(ipTtl) source.Connect(remote) ascii = ns.network.AsciiTraceHelper() csma.EnableAsciiAll(ascii.CreateFileStream("socket-options-ipv4.tr")) csma.EnablePcapAll("socket-options-ipv4", False) # Schedule SendPacket interPacketInterval = ns.core.Seconds(packetInterval) ns.core.Simulator.ScheduleWithContext(source.GetNode().GetId(), ns.core.Seconds(1.0), SendPacket, source, packetSize, packetCount, interPacketInterval) print("Run Simulation.") ns.core.Simulator.Run() ns.core.Simulator.Destroy() print("Done.")
def main(argv): ns.core.LogComponentEnable("UdpClient", ns.core.LOG_LEVEL_INFO) ns.core.LogComponentEnable("UdpServer", ns.core.LOG_LEVEL_INFO) cmd = ns.core.CommandLine () cmd.useIpv6 = "False" cmd.AddValue ("useIpv6", "Use Ipv6") cmd.Parse (argv) print "Create nodes." n = ns.network.NodeContainer () n.Create (2) internet = ns.internet.InternetStackHelper () internet.Install (n) 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) 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." port = 4000 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)) MaxPacketSize = 1024; interPacketInterval = ns.core.Seconds (0.05) maxPacketCount = 320; 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 (MaxPacketSize)) clientapps = client.Install (n.Get (0)) serverapps.Start (ns.core.Seconds (2.0)) serverapps.Stop (ns.core.Seconds (10.0)) ascii = ns.network.AsciiTraceHelper () csma.EnableAsciiAll (ascii.CreateFileStream ("udp-client-serv.tr")) csma.EnablePcapAll ("udp-client-serv", False) print "Run Simulation." ns.core.Simulator.Run () ns.core.Simulator.Destroy () print "Done."