Examples/udp/udp- Source File - NS-3

A Discrete-Event Network Simulator
  • Home
  • Tutorials ▼ English Portuguese
  • Docs ▼ Wiki Manual Models
  • Develop ▼ API Bugs
API
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages udp-echo.cc Go to the documentation of this file. 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 2 /* 3  * This program is free software; you can redistribute it and/or modify 4  * it under the terms of the GNU General Public License version 2 as 5  * published by the Free Software Foundation; 6  * 7  * This program is distributed in the hope that it will be useful, 8  * but WITHOUT ANY WARRANTY; without even the implied warranty of 9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10  * GNU General Public License for more details. 11  * 12  * You should have received a copy of the GNU General Public License 13  * along with this program; if not, write to the Free Software 14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15  */ 16  17 // Network topology 18 // 19 // n0 n1 n2 n3 20 // | | | | 21 // ================= 22 // LAN 23 // 24 // - UDP flows from n0 to n1 and back 25 // - DropTail queues 26 // - Tracing of queues and packet receptions to file "udp-echo.tr" 27  28 #include <fstream> 29 #include "ns3/core-module.h" 30 #include "ns3/csma-module.h" 31 #include "ns3/applications-module.h" 32 #include "ns3/internet-module.h" 33  34 using namespace ns3; 35  36 NS_LOG_COMPONENT_DEFINE ("UdpEchoExample"); 37  38 int 39 main (int argc, char *argv[]) 40 { 41 // 42 // Users may find it convenient to turn on explicit debugging 43 // for selected modules; the below lines suggest how to do this 44 // 45 #if 0 46  LogComponentEnable ("UdpEchoExample", LOG_LEVEL_INFO); 47  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_ALL); 48  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_ALL); 49 #endif 50 // 51 // Allow the user to override any of the defaults and the above Bind() at 52 // run-time, via command-line arguments 53 // 54  bool useV6 = false; 55  Address serverAddress; 56  57  CommandLine cmd; 58  cmd.AddValue ("useIpv6", "Use Ipv6", useV6); 59  cmd.Parse (argc, argv); 60 // 61 // Explicitly create the nodes required by the topology (shown above). 62 // 63  NS_LOG_INFO ("Create nodes."); 64  NodeContainer n; 65  n.Create (4); 66  67  InternetStackHelper internet; 68  internet.Install (n); 69  70  NS_LOG_INFO ("Create channels."); 71 // 72 // Explicitly create the channels required by the topology (shown above). 73 // 74  CsmaHelper csma; 75  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); 76  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); 77  csma.SetDeviceAttribute ("Mtu", UintegerValue (1400)); 78  NetDeviceContainer d = csma.Install (n); 79  80 // 81 // We've got the "hardware" in place. Now we need to add IP addresses. 82 // 83  NS_LOG_INFO ("Assign IP Addresses."); 84  if (useV6 == false) 85  { 86  Ipv4AddressHelper ipv4; 87  ipv4.SetBase ("10.1.1.0", "255.255.255.0"); 88  Ipv4InterfaceContainer i = ipv4.Assign (d); 89  serverAddress = Address(i.GetAddress (1)); 90  } 91  else 92  { 93  Ipv6AddressHelper ipv6; 94  ipv6.SetBase ("2001:0000:f00d:cafe::", Ipv6Prefix (64)); 95  Ipv6InterfaceContainer i6 = ipv6.Assign (d); 96  serverAddress = Address(i6.GetAddress (1,1)); 97  } 98  99  NS_LOG_INFO ("Create Applications."); 100 // 101 // Create a UdpEchoServer application on node one. 102 // 103  uint16_t port = 9; // well-known echo port number 104  UdpEchoServerHelper server (port); 105  ApplicationContainer apps = server.Install (n.Get (1)); 106  apps.Start (Seconds (1.0)); 107  apps.Stop (Seconds (10.0)); 108  109 // 110 // Create a UdpEchoClient application to send UDP datagrams from node zero to 111 // node one. 112 // 113  uint32_t packetSize = 1024; 114  uint32_t maxPacketCount = 1; 115  Time interPacketInterval = Seconds (1.); 116  UdpEchoClientHelper client (serverAddress, port); 117  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount)); 118  client.SetAttribute ("Interval", TimeValue (interPacketInterval)); 119  client.SetAttribute ("PacketSize", UintegerValue (packetSize)); 120  apps = client.Install (n.Get (0)); 121  apps.Start (Seconds (2.0)); 122  apps.Stop (Seconds (10.0)); 123  124 #if 0 125 // 126 // Users may find it convenient to initialize echo packets with actual data; 127 // the below lines suggest how to do this 128 // 129  client.SetFill (apps.Get (0), "Hello World"); 130  131  client.SetFill (apps.Get (0), 0xa5, 1024); 132  133  uint8_t fill[] = { 0, 1, 2, 3, 4, 5, 6}; 134  client.SetFill (apps.Get (0), fill, sizeof(fill), 1024); 135 #endif 136  137  AsciiTraceHelper ascii; 138  csma.EnableAsciiAll (ascii.CreateFileStream ("udp-echo.tr")); 139  csma.EnablePcapAll ("udp-echo", false); 140  141 // 142 // Now, do the actual simulation. 143 // 144  NS_LOG_INFO ("Run Simulation."); 145  Simulator::Run (); 146  Simulator::Destroy (); 147  NS_LOG_INFO ("Done."); 148 } ns3::ApplicationContainerholds a vector of ns3::Application pointers. Definition: application-container.h:41 ns3::Timekeep track of time values and allow control of global simulation resolution Definition: nstime.h:81 ns3::AsciiTraceHelperManage ASCII trace files for device models. Definition: trace-helper.h:109 ns3::CsmaHelper::SetChannelAttributevoid SetChannelAttribute(std::string n1, const AttributeValue &v1)Definition: csma-helper.cc:69 ns3::Ipv6InterfaceContainerKeep track of a set of IPv6 interfaces. Definition: ipv6-interface-container.h:41 ns3::Ipv4InterfaceContainerholds a vector of std::pair of Ptr and interface index. Definition: ipv4-interface-container.h:32 ns3::UdpEchoClientHelpercreate an application which sends a udp packet and waits for an echo of this packet ...Definition: udp-echo-helper.h:100 ns3::Simulator::Runstatic void Run(void)Definition: simulator.cc:157 ns3::InternetStackHelperaggregate IP/TCP/UDP functionality to existing Nodes. Definition: internet-stack-helper.h:64 ns3::Ipv6AddressHelper::SetBasevoid SetBase(Ipv6Address network, Ipv6Prefix prefix, Ipv6Address base=Ipv6Address("::1"))Set the base network number, network prefix, and base interface ID. Definition: ipv6-address-helper.cc:52 ns3::CsmaHelper::InstallNetDeviceContainer Install(Ptr< Node > node) const Definition: csma-helper.cc:215 NS_LOG_INFO#define NS_LOG_INFO(msg)Definition: log.h:264 ns3::AsciiTraceHelper::CreateFileStreamPtr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)Create and initialize an output stream object we'll use to write the traced bits. ...Definition: trace-helper.cc:184 portuint16_t portDefinition: dsdv-manet.cc:44 ns3::Addressa polymophic address class Definition: address.h:86 ns3::UdpEchoServerHelper::InstallApplicationContainer Install(Ptr< Node > node) const Definition: udp-echo-helper.cc:43 ns3::Ipv6AddressHelper::AssignIpv6InterfaceContainer Assign(const NetDeviceContainer &c)Allocate an Ipv6InterfaceContainer with auto-assigned addresses. Definition: ipv6-address-helper.cc:119 ns3::DataRateClass for representing data rates. Definition: data-rate.h:71 ns3::UdpEchoServerHelperCreate a server application which waits for input udp packets and sends them back to the original sen...Definition: udp-echo-helper.h:36 ns3::PcapHelperForDevice::EnablePcapAllvoid EnablePcapAll(std::string prefix, bool promiscuous=false)Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...Definition: trace-helper.cc:432 ns3::TimeValuehold objects of type ns3::Time Definition: nstime.h:828 ns3::UintegerValueHold an unsigned integer type. Definition: uinteger.h:46 ns3::LOG_LEVEL_INFODefinition: log.h:44 ns3::NetDeviceContainerholds a vector of ns3::NetDevice pointers Definition: net-device-container.h:41 ns3::ApplicationContainer::Startvoid Start(Time start)Arrange for all of the Applications in this container to Start() at the Time given as a parameter...Definition: application-container.cc:84 ns3::CommandLineParse command-line arguments. Definition: command-line.h:152 ns3::Simulator::Destroystatic void Destroy(void)Definition: simulator.cc:121 ns3::UdpEchoClientHelper::SetFillvoid SetFill(Ptr< Application > app, std::string fill)Definition: udp-echo-helper.cc:106 ns3::NodeContainerkeep track of a set of node pointers. Definition: node-container.h:38 ns3::ApplicationContainer::GetPtr< Application > Get(uint32_t i) const Get the Ptr stored in this container at a given index. Definition: application-container.cc:59 ns3::InternetStackHelper::Installvoid Install(std::string nodeName) const Definition: internet-stack-helper.cc:487 ns3::CsmaHelperbuild a set of CsmaNetDevice objects Definition: csma-helper.h:46 ns3::CsmaHelper::SetDeviceAttributevoid SetDeviceAttribute(std::string n1, const AttributeValue &v1)Definition: csma-helper.cc:63 ns3::Ipv6AddressHelperHelper class to auto-assign global IPv6 unicast addresses. Definition: ipv6-address-helper.h:77 ns3::ApplicationContainer::Stopvoid Stop(Time stop)Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...Definition: application-container.cc:93 ns3::Ipv4AddressHelper::AssignIpv4InterfaceContainer Assign(const NetDeviceContainer &c)Assign IP addresses to the net devices specified in the container based on the current network prefix...Definition: ipv4-address-helper.cc:131 ns3::NS_LOG_COMPONENT_DEFINENS_LOG_COMPONENT_DEFINE("PacketLossCounter") ns3::DataRateValuehold objects of type ns3::DataRate ns3::CommandLine::AddValuevoid AddValue(const std::string &name, const std::string &help, T &value)Definition: command-line.h:408 ns3::NodeContainer::GetPtr< Node > Get(uint32_t i) const Get the Ptr stored in this container at a given index. Definition: node-container.cc:88 ns3::Ipv6PrefixDescribes an IPv6 prefix. It is just a bitmask like Ipv4Mask. Definition: ipv6-address.h:364 ns3::LOG_LEVEL_ALLDefinition: log.h:53 ns3::CommandLine::Parsevoid Parse(int argc, char *argv[])Definition: command-line.cc:104 ns3::Ipv4AddressHelperA helper class to make life easier while doing simple IPv4 address assignment in scripts. Definition: ipv4-address-helper.h:45 ns3::AsciiTraceHelperForDevice::EnableAsciiAllvoid EnableAsciiAll(std::string prefix)Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...Definition: trace-helper.cc:579 ns3::NodeContainer::Createvoid Create(uint32_t n)Create n nodes and append pointers to them to the end of this NodeContainer. Definition: node-container.cc:93 ns3::UdpEchoClientHelper::SetAttributevoid SetAttribute(std::string name, const AttributeValue &value)Definition: udp-echo-helper.cc:98 ns3::UdpEchoClientHelper::InstallApplicationContainer Install(Ptr< Node > node) const Definition: udp-echo-helper.cc:124 ns3::Ipv6InterfaceContainer::GetAddressIpv6Address GetAddress(uint32_t i, uint32_t j) const Get the address for the specified index. Definition: ipv6-interface-container.cc:57 ns3::Ipv4AddressHelper::SetBasevoid SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")Set the base network number, network mask and base address. Definition: ipv4-address-helper.cc:60 ns3::LogComponentEnablevoid LogComponentEnable(char const *name, enum LogLevel level)Definition: log.cc:311 ns3::Ipv4InterfaceContainer::GetAddressIpv4Address GetAddress(uint32_t i, uint32_t j=0) const Definition: ipv4-interface-container.cc:39 mainint main(int argc, char *argv[])Definition: udp-echo.cc:39
  • examples
  • udp
  • udp-echo.cc
  • Generated on Sun Apr 20 2014 11:14:44 for ns-3 by doxygen 1.8.6

Từ khóa » Echo H152