NS3 myfirst.cc Two node point-to-point communication

Source: Internet
Author: User

First locate the first.cc file under ns3.25/examples/tutorial/and copy it to the scratch directory.

And then, to make it easier to get the code out,

/*-*-mode:c++; C-file-style: "GNU"; indent-tabs-mode:nil;-*-*//** This is a free software, you can redistribute it and/or modify * it under the terms of the GNU general public License version 2 as * published by the free software Foundation; * * This program was distributed in the hope that it'll be useful, * but without any WARRANTY;  Without even the implied warranty of * merchantability or FITNESS for A particular PURPOSE. See the * GNU general public License for more details. * * You should has received a copy of the GNU general public License * along with this program; If not, write to the free software * Foundation, Inc., Temple Place, Suite, Boston, MA 02111-1307 USA*/#include"ns3/core-module.h"#include"ns3/network-module.h"#include"ns3/internet-module.h"#include"ns3/point-to-point-module.h"#include"ns3/applications-module.h"using namespacens3; Ns_log_component_define ("Firstscriptexample");intMain (intargcChar*argv[])  {time::setresolution (Time::ns); Logcomponentenable ("udpechoclientapplication", Log_level_info); Logcomponentenable ("udpechoserverapplication", Log_level_info);  Nodecontainer nodes; Nodes. Create (2);  Pointtopointhelper Pointtopoint; Pointtopoint.setdeviceattribute ("datarate", StringValue ("5Mbps")); Pointtopoint.setchannelattribute ("Delay", StringValue ("2ms"));  Netdevicecontainer devices; Devices=Pointtopoint.install (nodes);  Internetstackhelper Stack; Stack.  Install (nodes);  Ipv4addresshelper address; Address. SetBase ("10.1.1.0","255.255.255.0"); Ipv4interfacecontainer Interfaces=address.  Assign (Devices); Udpechoserverhelper Echoserver (9); Applicationcontainer Serverapps= Echoserver.install (nodes. Get (1)); Serverapps.start (Seconds (1.0)); Serverapps.stop (Seconds (10.0)); Udpechoclienthelper echoclient (interfaces. GetAddress (1),9); Echoclient.setattribute ("maxpackets", Uintegervalue (2)); Echoclient.setattribute ("Interval", TimeValue (Seconds (1.0))); Echoclient.setattribute ("PacketSize", Uintegervalue (1024x768)); Applicationcontainer ClientApps= Echoclient.install (nodes. Get (0)); Clientapps.start (Seconds (2.0)); Clientapps.stop (Seconds (10.0));  Simulator::run ();  Simulator::D Estroy (); return 0;}

First study the header file

#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/ Point-to-point-module.h "#include" ns3/applications-module.h "

These header files are stored in a NS3 directory under the build directory, and we can refer to the corresponding header file in accordance with the function we need.

using namespace Ns3;

Ns3 namespace so that we can no longer write ns3::xxx, or compile errors.

Ns_log_component_define ("Firstscriptexample");

The log component, which appears to be a bit of a mask, is actually a function of generating a log component named "Firstscriptexample".

Time::setresolution (Time::ns);

Set the time unit to nanoseconds, although I don't quite understand why to set the time unit to nanoseconds?? This is Baidu to get the existence of doubt

  Logcomponentenable ("Udpechoclientapplication", log_level_info);  Logcomponentenable ("Udpechoserverapplication", log_level_info);

is two log components in effect, and the log level is set to info, this two log is built into the echo client and Echo sever applications. It's going to be useful.

  Nodecontainer nodes;  Nodes. Create (2);

This sentence is almost the declaration of a node container nodes, and then in Li Yong Create (2) to generate two nodes.

  Pointtopointhelper Pointtopoint;  Pointtopoint.setdeviceattribute ("Datarate", StringValue ("5Mbps"));  Pointtopoint.setchannelattribute ("Delay", StringValue ("2ms"));

This pointtopointhelper class is responsible for setting network device and channel properties.

The "daterate" data rate in the network device is set to 5Mbps, and the delay of the channel attribute is set to 2ms. This wait will be generated later in the results, as well as your own knowledge of the network to explain where he is used.

  Netdevicecontainer devices;  devices = Pointtopoint.install (nodes);

We have finished using Nodecontainer to create nodes, and pointtopointhelper to do the work of creating, configuring, installing. Now the two of them need to be connected together. You need a list of Netdevices objects that can be stored by Netdevicecontainer, just as you would use the first Nodecontainer object to hold the node you created.

Pointtopointhelper the Install () method a Nodecontainer object as a parameter, with a Netdevice container as the return object.

For each node of the Nodecontainer, as this is a point-to-point channel, so clear 2 nodes. A Pointtopointchannel object was created, and 2 pointtopointnetdevices connected to him.

So finally, two nodes, each node installed a point-to-dot network device, the two network devices are installed between the point-to channel, 2 devices will be configured on a channel with 2ms transmission delay at 5Mbps rate of transmission of data.

  Internetstackhelper Stack;  Stack. Install (nodes);

Internetstackhelper will install a network protocol for each node of Nodecontainer.

Ipv4addresshelper address;  Address. SetBase ("10.1.1.0", "255.255.255.0");

Ipv4addresshelper sets the IP address for the device on the node. The SetBase method tells it to assign an address from 10.1.1.0 to 255.255.255.0 with a subnet mask, and the address assignment defaults to a monotonically growing starting from 1.

  Ipv4interfacecontainer interfaces = address. Assign (Devices);

This code completes the address configuration, and NS3 uses the Ipv4interface object to associate an IP address with a device. And Ipv4interfacecontainer provides such a function as a container for Ipv4interface objects.

To this point, a network with the IP address class is configured, with a protocol stack installed. Now it's going to use it for data communication.

  Udpechoserverhelper Echoserver (9);  Applicationcontainer Serverapps = Echoserver.install (nodes. Get (1));  Serverapps.start (Seconds (1.0));  Serverapps.stop (Seconds (10.0));

Declares a udpechoserverhelper like any other help class this is not the application itself, but rather an object to create a real application where the port is Port 9th.

And in just nodecontainer inside of the number 2nd node here 0 is the 1th node, 1 is the 2nd number node. Loading the server application, the same return is a Application object, which requires a applicationcontainer as a container to store

The Application object requires a time parameter, starting and stopping, where two lines allow the Echo service to start at 1s and stop at 10s.

Udpechoclienthelper echoclient (interfaces. GetAddress (1), 9);  Echoclient.setattribute ("Maxpackets", Uintegervalue (2));  Echoclient.setattribute ("Interval", TimeValue (Seconds (1.0)));  Echoclient.setattribute ("PacketSize", Uintegervalue (1024));

The same client application, first to the client's Udpechoclienthelper class incoming to the client to send data to the IP address, as well as the port number, here is set the server's device IP address, and just set to accept port number 9th.

Maxpackets is the maximum number of packets that can be sent during the simulation

Interval the number of times to wait for a group of two

PacketSize How much data is hosted for each grouping. Here the parameter 1024 is byte, not bit!!!

Applicationcontainer ClientApps = Echoclient.install (nodes. Get (0)); Clientapps.start (Seconds (2.0)); Clientapps.stop (Seconds (10.0));

Then in the same way, a node as a client, the client application installed on a node,

App start service is 2s, end service is 10s

As to why the 2s started, I was a bit blindfolded, check the relevant information, said to wait for the server to take effect 1s after the start as a reasonable service. You can also change the above parameter to 1, this is the client becomes 1s when the start of service, that is, 1s time to start sending data.

  Simulator::run ();  Simulator::D Estroy ();

Start the emulator, Simulator::run (), and the system starts traversing the list of preset events and executes.

When execution is complete, call Simulator::D estory () to destroy the set of things that have just been created.

Execute under Eclipse

Run results

This is sending a packet, that is, the client sends a 1024-byte packet to the server, and the port of the client here I think should be set randomly, because there is no 49153 port from beginning to end.

And why it was 2.00369s. I did an operation on the basis of 2s 2s+1024*8/(5*10^6) (transmit delay) s+0.002s (transmission delay) =2.0036384s And 20.00369s there is a certain gap, but the gap has been very small, so the individual think that part of the delay can be treated as a processing delay (there is no queuing delay).

NS3 myfirst.cc Two node point-to-point communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.