Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ns3/abort.h"
- #include "ns3/core-module.h"
- #include "ns3/internet-module.h"
- #include "ns3/network-module.h"
- #include "ns3/emu-module.h"
- #include "ns3/applications-module.h"
- #include "ns3/ipv4-static-routing-helper.h"
- #include "ns3/ipv4-list-routing-helper.h"
- using namespace ns3;
- NS_LOG_COMPONENT_DEFINE ("SocketTryout2");
- void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
- {
- NS_LOG_INFO ("SendStuff () called ...");
- Ptr<Packet> p = Create<Packet> (reinterpret_cast<uint8_t const *> ("I am long 20 bytes!"), 20);
- p->AddPaddingAtEnd (100);
- sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
- return;
- }
- static void PingRtt (std::string context, Time rtt) {
- NS_LOG_UNCOND ("Received Response with RTT = " << rtt);
- }
- int main (int argc, char *argv[])
- {
- NS_LOG_INFO ("SocketTryout2");
- std::string deviceName ("eth0");
- std::string remote ("192.168.27.76");
- //std::string remote ("88.149.128.3"); // 84.149.128.3 maya.ngi.it
- CommandLine cmd;
- cmd.AddValue ("deviceName", "Device name", deviceName);
- cmd.AddValue ("remote", "Remote IP address (dotted decimal only please)", remote);
- cmd.Parse (argc, argv);
- Ipv4Address remoteIp (remote.c_str ());
- Ipv4Address localIp ("192.168.27.90");
- Ipv4Mask localMask ("255.255.255.0");
- GlobalValue::Bind ("SimulatorImplementationType", StringValue ("ns3::RealtimeSimulatorImpl"));
- GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true));
- Ptr<Node> node = CreateObject<Node> ();
- Ptr<EmuNetDevice> device = CreateObject<EmuNetDevice> ();
- device->SetAttribute ("Address", Mac48AddressValue (Mac48Address::Allocate ()));
- device->SetAttribute ("DeviceName", StringValue (deviceName));
- Ptr<Queue> queue = CreateObject<DropTailQueue> ();
- device->SetQueue (queue);
- node->AddDevice (device);
- InternetStackHelper internetStackHelper;
- internetStackHelper.Install (node);
- Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
- uint32_t interface = ipv4->AddInterface (device);
- Ipv4InterfaceAddress address = Ipv4InterfaceAddress (localIp, localMask);
- ipv4->AddAddress (interface, address);
- ipv4->SetMetric (interface, 1);
- ipv4->SetUp (interface);
- Ipv4Address gateway ("192.168.27.1");
- NS_ABORT_MSG_IF (gateway == "1.2.3.4", "You must change the gateway IP address before running this example");
- Ipv4StaticRoutingHelper ipv4RoutingHelper;
- Ptr<Ipv4StaticRouting> staticRouting = ipv4RoutingHelper.GetStaticRouting (ipv4);
- staticRouting->SetDefaultRoute (gateway, interface);
- Ptr<V4Ping> app = CreateObject<V4Ping> ();
- app->SetAttribute ("Remote", Ipv4AddressValue (remoteIp));
- node->AddApplication (app);
- app->SetStartTime (Seconds (1.0));
- app->SetStopTime (Seconds (10.0));
- Names::Add ("app", app);
- Config::Connect ("/Names/app/Rtt", MakeCallback (&PingRtt));
- Ptr<Socket> srcSocket = Socket::CreateSocket (node, TypeId::LookupByName ("ns3::UdpSocketFactory"));
- srcSocket->Bind ();
- srcSocket->BindToNetDevice (device);
- LogComponentEnableAll (LOG_PREFIX_TIME);
- LogComponentEnable ("SocketTryout2", LOG_LEVEL_INFO);
- Ipv4Address dstAddr = remoteIp;
- uint16_t dstPort = 9999;
- for (uint32_t i = 0; i < 20; i++) {
- Simulator::Schedule (Seconds (1 + i * 0.5), &SendStuff, srcSocket, dstAddr, dstPort);
- }
- NS_LOG_INFO ("Run Emulation.");
- Simulator::Stop (Seconds (10.0));
- Simulator::Run ();
- Simulator::Destroy ();
- NS_LOG_INFO ("Done.");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement