Ns 134

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Roll no.

134

Q. 1 A]Program to simulate UDP serverAddress = Address (i.GetAddress (1));


Server client. }
else
#include <fstream> {
#include "ns3/core-module.h" Ipv6AddressHelper ipv6;
#include "ns3/core-module.h" ipv6.SetBase ("2001:0000:f00d:cafe::",
#include "ns3/csma-module.h" Ipv6Prefix (64));
#include "ns3/applications-module.h" Ipv6InterfaceContainer i6 = ipv6.Assign (d);
#include "ns3/internet-module.h" serverAddress = Address(i6.GetAddress (1,1));
}
using namespace ns3;
NS_LOG_INFO ("Create Applications.");
NS_LOG_COMPONENT_DEFINE //
("UdpClientServerExample"); // Create one udpServer applications on node one.
//
int uint16_t port = 4000;
main (int argc, char *argv[]) UdpServerHelper server (port);
{ ApplicationContainer apps = server.Install (n.Get
(1));
LogComponentEnable ("UdpClient", apps.Start (Seconds (1.0));
LOG_LEVEL_INFO); apps.Stop (Seconds (10.0));
LogComponentEnable ("UdpServer",
LOG_LEVEL_INFO); //
// Create one UdpClient application to send UDP
bool useV6 = false; datagrams from node zero to
Address serverAddress; // node one.
//
CommandLine cmd; uint32_t MaxPacketSize = 1024;
cmd.AddValue ("useIpv6", "Use Ipv6", useV6); Time interPacketInterval = Seconds (0.05);
cmd.Parse (argc, argv); uint32_t maxPacketCount = 320;
NS_LOG_INFO ("Create nodes."); UdpClientHelper client (serverAddress, port);
NodeContainer n; client.SetAttribute ("MaxPackets", UintegerValue
n.Create (2); (maxPacketCount));
client.SetAttribute ("Interval", TimeValue
InternetStackHelper internet; (interPacketInterval));
internet.Install (n); client.SetAttribute ("PacketSize", UintegerValue
(MaxPacketSize));
NS_LOG_INFO ("Create channels."); apps = client.Install (n.Get (0));
CsmaHelper csma; apps.Start (Seconds (2.0));
csma.SetChannelAttribute ("DataRate", apps.Stop (Seconds (10.0));
DataRateValue (DataRate (5000000)));
csma.SetChannelAttribute ("Delay", TimeValue //
(MilliSeconds (2))); // Now, do the actual simulation.
csma.SetDeviceAttribute ("Mtu", UintegerValue //
(1400)); NS_LOG_INFO ("Run Simulation.");
NetDeviceContainer d = csma.Install (n); Simulator::Run ();
NS_LOG_INFO ("Assign IP Addresses."); Simulator::Destroy ();
if (useV6 == false) NS_LOG_INFO ("Done.");
{ }
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0"); Output:-
Ipv4InterfaceContainer i = ipv4.Assign (d);
Roll no. 134

NS_LOG_COMPONENT_DEFINE
("SecondScriptExample");

int main (int argc, char *argv[])

{ bool verbose = true;

uint32_t nCsma = 3;

CommandLine cmd ( FILE );

cmd.AddValue ("nCsma", "Number of \"extra\"


CSMA nodes/devices", nCsma);cmd.AddValue
("verbose", "Tell echo applications to log if true",
verbose);

cmd.Parse (argc,argv);

if (verbose)

LogComponentEnable
("UdpEchoClientApplication",
LOG_LEVEL_INFO);LogComponentEnable

("UdpEchoServerApplication",
Q.1 B] Animate three way LOG_LEVEL_INFO);

handshake for TCP Connection }

using nCsma = nCsma == 0 ? 1 : nCsma;

#include "ns3/coremodule.h" NodeContainer p2pNodes;

#include "ns3/network-module.h" p2pNodes.Create (2);

#include "ns3/csmamodule.h" NodeContainer csmaNodes;

#include "ns3/internet-module.h" csmaNodes.Add (p2pNodes.Get (1));

#include "ns3/point-to-pointmodule.h" csmaNodes.Create (nCsma);

#include "ns3/applications-module.h" PointToPointHelper pointToPoint;

#include "ns3/ipv4-globalrouting-helper.h" pointToPoint.SetDeviceAttribute ("DataRate",


StringValue ("5Mbps"));
//Inculding Header File pointToPoint.SetChannelAttribute ("Delay",
#include "ns3/netanim-module.h" StringValue ("2ms"));

using namespace ns3; NetDeviceContainer p2pDevices;

p2pDevices = pointToPoint.Install (p2pNodes);


Roll no. 134

CsmaHelper csma; echoClient.SetAttribute ("Interval", TimeValue


(Seconds (1.0)));
csma.SetChannelAttribute ("DataRate",
StringValue ("100Mbps")); echoClient.SetAttribute ("PacketSize",
csma.SetChannelAttribute ("Delay", TimeValue UintegerValue (1024));
(NanoSeconds (6560))); ApplicationContainer clientApps =
echoClient.Install (p2pNodes.Get (0));
NetDeviceContainer csmaDevices;
clientApps.Start (Seconds (2.0));
csmaDevices = csma.Install (csmaNodes);
clientApps.Stop (Seconds (10.0));
InternetStackHelper stack;
Ipv4GlobalRouting Helper::PopulateRo
stack.Install (p2pNodes.Get (0)); utingTables ();
stack.Install (csmaNodes); pointToPoint.Enabl ePcapAll ("pract9");
Ipv4AddressHelper address; csma.EnablePcap ("pact9", csmaDevices.Get (1),
true);
address.SetBase ("10.1.1.0", "255.255.255.0");
//Including Animation AnimationInterface
Ipv4InterfaceContainer p2pInterfaces;
anim("pract9.xml");
p2pInterfaces = address.Assign (p2pDevices);
Simulator::Run ();
address.SetBase ("10.1.2.0", "255.255.255.0");
Simulator::Destroy ();
Ipv4InterfaceContainer csmaInterfaces;
return 0;
csmaInterfaces = address.Assign (csmaDevices);

UdpEchoServerHelper echoServer (9); }


ApplicationContainer serverApps = Output:
echoServer.Install (csmaNodes.Get
(nCsma));serverApps.Start (Seconds (1.0)); Running the file and visualising
serverApps.Stop (Seconds (10.0));
UdpEchoClientHelper echoClient
(csmaInterfaces.GetAddress (nCsma), 9);
echoClient.SetAttribute ("MaxPackets",
UintegerValue (1));
Name – Rohit Wangnekar
Roll No - 134

Q.1 c) Output :

You might also like