diff --git a/examples/udp-echo.cc b/examples/udp-echo.cc index 7e169dc8a..fb91c3247 100644 --- a/examples/udp-echo.cc +++ b/examples/udp-echo.cc @@ -21,7 +21,7 @@ // ================= // LAN // -// - CBR/UDP flows from n0 to n1 and from n3 to n0 +// - UDP flows from n0 to n1 and back // - DropTail queues // - Tracing of queues and packet receptions to file "udp-echo.tr" @@ -136,11 +136,6 @@ main (int argc, char *argv[]) uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, Eui48Address("08:00:2e:00:00:03")); - - NS_DEBUG ("nd0 = " << nd0); - NS_DEBUG ("nd1 = " << nd1); - NS_DEBUG ("nd2 = " << nd2); - NS_DEBUG ("nd3 = " << nd3); // // We've got the "hardware" in place. Now we need to add IP addresses. // @@ -171,25 +166,29 @@ main (int argc, char *argv[]) NS_DEBUG("Create Applications."); // -// Create a UdpEchoServer application on node 1. +// Create a UdpEchoServer application on node one. // - Ptr server = Create (n1, "0.0.0.0", 80); -// -// Tell the application when to start and stop. -// - server->Start(Seconds(1.)); - server->Stop (Seconds(10.0)); + uint16_t port = 80; + + Ptr server = Create (n1, port); // // Create a UdpEchoClient application to send UDP datagrams from node zero to -// node 1. +// node one. // - Ptr client = Create (n0, "10.1.1.2", 80, - 1, Seconds(1.), 1024); + uint32_t packetSize = 1024; + uint32_t maxPacketCount = 1; + Time interPacketInterval = Seconds (1.); + + Ptr client = Create (n0, "10.1.1.2", port, + maxPacketCount, interPacketInterval, packetSize); // -// Tell the application when to start and stop. +// Tell the applications when to start and stop. // - client->Start(Seconds(2.0)); - client->Stop (Seconds(10.0)); + server->Start(Seconds(1.)); + client->Start(Seconds(2.)); + + server->Stop (Seconds(10.)); + client->Stop (Seconds(10.)); // // Configure tracing of all enqueue, dequeue, and NetDevice receive events. // Trace output will be sent to the file "udp-echo.tr" diff --git a/src/applications/udp-echo/udp-echo-client.cc b/src/applications/udp-echo/udp-echo-client.cc index 8bcd1c8ed..065b8bed1 100644 --- a/src/applications/udp-echo/udp-echo-client.cc +++ b/src/applications/udp-echo/udp-echo-client.cc @@ -99,7 +99,9 @@ UdpEchoClient::StartApplication (void) m_socket->Connect (m_peer); } - StopApplication (); + m_socket->SetRecvCallback((Callback, const Packet &, + const Address &>) MakeCallback(&UdpEchoClient::Receive, this)); + ScheduleTransmit (Seconds(0.)); } @@ -107,6 +109,14 @@ void UdpEchoClient::StopApplication () { NS_DEBUG ("UdpEchoClient::StopApplication ()"); + + if (!m_socket) + { + m_socket->SetRecvCallback((Callback, const Packet &, + const Address &>) NULL); + } + + Simulator::Cancel(m_sendEvent); } void @@ -136,4 +146,22 @@ UdpEchoClient::Send (void) } } +void +UdpEchoClient::Receive( + Ptr socket, + const Packet &packet, + const Address &from) +{ + NS_DEBUG ("UdpEchoClient::Receive (" << socket << ", " << packet << + ", " << from << ")"); + + if (InetSocketAddress::IsMatchingType (from)) + { + InetSocketAddress address = InetSocketAddress::ConvertFrom (from); + NS_DEBUG ("UdpEchoClient::Receive(): Received " << + packet.GetSize() << " bytes from " << address.GetIpv4()); + } +} + + } // Namespace ns3 diff --git a/src/applications/udp-echo/udp-echo-client.h b/src/applications/udp-echo/udp-echo-client.h index 7c5f2478a..91914287d 100644 --- a/src/applications/udp-echo/udp-echo-client.h +++ b/src/applications/udp-echo/udp-echo-client.h @@ -50,6 +50,8 @@ private: void ScheduleTransmit (Time dt); void Send (void); + void Receive(Ptr socket, const Packet &packet, const Address &from); + Ptr m_node; Ipv4Address m_serverAddress; uint16_t m_serverPort; diff --git a/src/applications/udp-echo/udp-echo-server.cc b/src/applications/udp-echo/udp-echo-server.cc index 26958b80f..8f51f9f1c 100644 --- a/src/applications/udp-echo/udp-echo-server.cc +++ b/src/applications/udp-echo/udp-echo-server.cc @@ -15,6 +15,7 @@ * 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/debug.h" #include "ns3/ipv4-address.h" #include "ns3/nstime.h" @@ -32,15 +33,14 @@ NS_DEBUG_COMPONENT_DEFINE ("UdpEchoServer"); UdpEchoServer::UdpEchoServer ( Ptr n, - Ipv4Address clientAddress, - uint16_t clientPort) + uint16_t port) : Application(n) { - NS_DEBUG ("UdpEchoServer::UdpEchoServer (" << n << ", " << clientAddress << - ", " << clientPort << ")"); + NS_DEBUG ("UdpEchoServer::UdpEchoServer (" << n << ", " << + port << ")"); - Construct (n, clientAddress, clientPort); + Construct (n, port); } UdpEchoServer::~UdpEchoServer() @@ -51,18 +51,15 @@ UdpEchoServer::~UdpEchoServer() void UdpEchoServer::Construct ( Ptr n, - Ipv4Address clientAddress, - uint16_t clientPort) + uint16_t port) { - NS_DEBUG ("UdpEchoServer::Construct (" << n << ", " << clientAddress << - ", " << clientPort << ")"); + NS_DEBUG ("UdpEchoServer::Construct (" << n << ", " << port << ")"); m_node = n; - m_clientAddress = clientAddress; - m_clientPort = clientPort; + m_port = port; m_socket = 0; - m_client = InetSocketAddress (clientAddress, clientPort); + m_local = InetSocketAddress (Ipv4Address::GetAny (), port); } void @@ -83,7 +80,7 @@ UdpEchoServer::StartApplication (void) Ptr socketFactory = GetNode ()->QueryInterface (iid); m_socket = socketFactory->CreateSocket (); - m_socket->Bind (m_client); + m_socket->Bind (m_local); } m_socket->SetRecvCallback((Callback, const Packet &, @@ -101,7 +98,8 @@ UdpEchoServer::StopApplication () } } -void UdpEchoServer::Receive( +void +UdpEchoServer::Receive( Ptr socket, const Packet &packet, const Address &from) @@ -114,6 +112,9 @@ void UdpEchoServer::Receive( InetSocketAddress address = InetSocketAddress::ConvertFrom (from); NS_DEBUG ("UdpEchoServer::Receive(): Received " << packet.GetSize() << " bytes from " << address.GetIpv4()); + + NS_DEBUG ("UdpEchoServer::Receive (): Echoing packet"); + socket->SendTo (from, packet); } } diff --git a/src/applications/udp-echo/udp-echo-server.h b/src/applications/udp-echo/udp-echo-server.h index d9de1221f..597fef8fe 100644 --- a/src/applications/udp-echo/udp-echo-server.h +++ b/src/applications/udp-echo/udp-echo-server.h @@ -32,27 +32,25 @@ class Packet; class UdpEchoServer : public Application { public: - UdpEchoServer (Ptr n, Ipv4Address clientAddr, uint16_t clientPort); + UdpEchoServer (Ptr n, uint16_t clientPort); virtual ~UdpEchoServer (); protected: virtual void DoDispose (void); private: - void Construct (Ptr n, Ipv4Address clientAddr, uint16_t clientPort); + void Construct (Ptr n, uint16_t clientPort); virtual void StartApplication (void); virtual void StopApplication (void); - void Receive(Ptr socket, const Packet &packet, - const Address &from); + void Receive(Ptr socket, const Packet &packet, const Address &from); Ptr m_node; - Ipv4Address m_clientAddress; - uint16_t m_clientPort; + uint16_t m_port; Ptr m_socket; - Address m_client; + Address m_local; }; } // namespace ns3