/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * This program is 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 is distributed in the hope that it will 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 have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, 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 namespace ns3; NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); void ReceivePacket (Ptr socket) { static int pktCount = 100000; Address from; while (socket->RecvFrom (from)) { // NS_LOG_UNCOND (pktCount << " - Received one packet!"); if (pktCount > 0) { socket->SendTo (Create (1024), 0, from); pktCount --; } else { socket->Close (); } } } int main (int argc, char *argv[]) { Time::SetResolution (Time::NS); LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); NodeContainer nodes; nodes.Create (1); InternetStackHelper stack; stack.Install (nodes); TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); Ptr recvSink = Socket::CreateSocket (nodes.Get (0), tid); InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); recvSink->Bind (local); recvSink->SetRecvCallback (MakeCallback (&ReceivePacket)); Ptr source = Socket::CreateSocket (nodes.Get (0), tid); InetSocketAddress remote = InetSocketAddress (Ipv4Address ("127.0.0.1"), 80); source->SetAllowBroadcast (true); source->Connect (remote); source->SetRecvCallback (MakeCallback (&ReceivePacket)); source->Send (Create (1024)); Simulator::Run (); Simulator::Destroy (); return 0; }