diff --git a/examples/csma-broadcast.cc b/examples/csma-broadcast.cc index b032f0aea..c0ad09770 100644 --- a/examples/csma-broadcast.cc +++ b/examples/csma-broadcast.cc @@ -77,6 +77,7 @@ int main (int argc, char *argv[]) DebugComponentEnable("Ipv4L3Protocol"); DebugComponentEnable("OnOffApplication"); DebugComponentEnable("PacketSocket"); + DebugComponentEnable("Socket"); DebugComponentEnable("UdpSocket"); DebugComponentEnable("UdpL4Protocol"); DebugComponentEnable("Ipv4L3Protocol"); diff --git a/examples/csma-multicast.cc b/examples/csma-multicast.cc index 6bae090ff..df1c51f01 100644 --- a/examples/csma-multicast.cc +++ b/examples/csma-multicast.cc @@ -76,6 +76,7 @@ main (int argc, char *argv[]) DebugComponentEnable("Ipv4L3Protocol"); DebugComponentEnable("OnOffApplication"); DebugComponentEnable("PacketSocket"); + DebugComponentEnable("Socket"); DebugComponentEnable("UdpSocket"); DebugComponentEnable("UdpL4Protocol"); DebugComponentEnable("Ipv4L3Protocol"); diff --git a/examples/csma-one-subnet.cc b/examples/csma-one-subnet.cc index 19d9200ff..ef3496a12 100644 --- a/examples/csma-one-subnet.cc +++ b/examples/csma-one-subnet.cc @@ -72,6 +72,7 @@ main (int argc, char *argv[]) DebugComponentEnable("NetDevice"); DebugComponentEnable("PacketSocket"); DebugComponentEnable("OnOffApplication"); + DebugComponentEnable("Socket"); DebugComponentEnable("UdpSocket"); DebugComponentEnable("UdpL4Protocol"); DebugComponentEnable("Ipv4L3Protocol"); diff --git a/examples/simple-point-to-point.cc b/examples/simple-point-to-point.cc index 4b9de40f4..91214f53a 100644 --- a/examples/simple-point-to-point.cc +++ b/examples/simple-point-to-point.cc @@ -85,6 +85,7 @@ main (int argc, char *argv[]) DebugComponentEnable("NetDevice"); DebugComponentEnable("PacketSocket"); DebugComponentEnable("OnOffApplication"); + DebugComponentEnable("Socket"); DebugComponentEnable("UdpSocket"); DebugComponentEnable("UdpL4Protocol"); DebugComponentEnable("Ipv4L3Protocol"); diff --git a/examples/udp-echo.cc b/examples/udp-echo.cc index b59c2b5a8..7e169dc8a 100644 --- a/examples/udp-echo.cc +++ b/examples/udp-echo.cc @@ -59,8 +59,8 @@ main (int argc, char *argv[]) // Users may find it convenient to turn on explicit debugging // for selected modules; the below lines suggest how to do this // -#if 1 - DebugComponentEnable("CsmaOneSubnet"); +#if 0 + DebugComponentEnable("UdpEcho"); DebugComponentEnable("Object"); DebugComponentEnable("Queue"); @@ -72,6 +72,7 @@ main (int argc, char *argv[]) DebugComponentEnable("NetDevice"); DebugComponentEnable("PacketSocket"); DebugComponentEnable("OnOffApplication"); + DebugComponentEnable("Socket"); DebugComponentEnable("UdpSocket"); DebugComponentEnable("UdpL4Protocol"); DebugComponentEnable("Ipv4L3Protocol"); @@ -80,7 +81,12 @@ main (int argc, char *argv[]) DebugComponentEnable("ArpIpv4Interface"); DebugComponentEnable("Ipv4LoopbackInterface"); DebugComponentEnable("UdpEchoClient"); + DebugComponentEnable("UdpEchoServer"); #endif + + DebugComponentEnable("UdpEcho"); + DebugComponentEnable("UdpEchoClient"); + DebugComponentEnable("UdpEchoServer"); // // Set up default values for the simulation. Use the DefaultValue::Bind() // technique to tell the system what subclass of Queue to use. The Bind @@ -165,6 +171,15 @@ main (int argc, char *argv[]) NS_DEBUG("Create Applications."); // +// Create a UdpEchoServer application on node 1. +// + 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)); +// // Create a UdpEchoClient application to send UDP datagrams from node zero to // node 1. // @@ -173,18 +188,9 @@ main (int argc, char *argv[]) // // Tell the application when to start and stop. // - client->Start(Seconds(1.0)); + client->Start(Seconds(2.0)); client->Stop (Seconds(10.0)); // -// Create a UdpEchoServer application on node 1. -// - Ptr server = Create (n0, "0.0.0.0", 80); -// -// Tell the application when to start and stop. -// - server->Start(Seconds(0.)); - server->Stop (Seconds(10.0)); -// // 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 317a91065..8bcd1c8ed 100644 --- a/src/applications/udp-echo/udp-echo-client.cc +++ b/src/applications/udp-echo/udp-echo-client.cc @@ -23,64 +23,8 @@ #include "ns3/simulator.h" #include "ns3/socket-factory.h" #include "ns3/packet.h" - #include "udp-echo-client.h" -#if 0 -int sock; /* Socket descriptor */ -struct sockaddr_in echoServAddr; /* Echo server address */ -struct sockaddr_in fromAddr; /* Source address of echo */ -unsigned short echoServPort; /* Echo server port */ -unsigned int fromSize; /* In-out of address size for recvfrom() */ -char *servIP; /* IP address of server */ -char *echoString; /* String to send to echo server */ -char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */ -int echoStringLen; /* Length of string to echo */ -int respStringLen; /* Length of received response */ - -if ((echoStringLen = strlen(echoString)) > ECHOMAX) /* Check input length */ - DieWithError("Echo word too long"); - -if (argc == 4) - echoServPort = atoi(argv[3]); /* Use given port, if any */ - else - echoServPort = 7; /* 7 is the well-known port for the echo service */ - -/* Create a datagram/UDP socket */ -if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) - DieWithError("socket() failed"); - -/* Construct the server address structure */ -memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ -echoServAddr.sin_family = AF_INET; /* Internet addr family */ -echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ -echoServAddr.sin_port = htons(echoServPort); /* Server port */ - -/* Send the string to the server */ -if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *) - &echoServAddr, sizeof(echoServAddr)) != echoStringLen) - DieWithError("sendto() sent a different number of bytes than expected"); - -/* Recv a response */ -fromSize = sizeof(fromAddr); -if ((respStringLen = recvfrom(sock, echoBuffer, ECHOMAX, 0, - (struct sockaddr *) &fromAddr, &fromSize)) != echoStringLen) - DieWithError("recvfrom() failed"); - -if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr) - { - fprintf(stderr,"Error: received a packet from unknown source.\n"); - exit(1); - } - -/* null-terminate the received data */ -echoBuffer[respStringLen] = '\0'; -printf("Received: %s\n", echoBuffer); /* Print the echoed arg */ - -close(sock); -exit(0); -#endif - namespace ns3 { NS_DEBUG_COMPONENT_DEFINE ("UdpEchoClient"); @@ -183,6 +127,9 @@ UdpEchoClient::Send (void) m_socket->Send (p); ++m_sent; + NS_DEBUG ("UdpEchoClient::Send (): Sent " << m_size << " bytes to " << + m_serverAddress); + if (m_sent < m_count) { ScheduleTransmit (m_interval); diff --git a/src/node/socket.cc b/src/node/socket.cc index 58f83947f..efb28002a 100644 --- a/src/node/socket.cc +++ b/src/node/socket.cc @@ -19,83 +19,114 @@ * Authors: George F. Riley * Mathieu Lacage */ -#include "socket.h" + +#include "ns3/debug.h" #include "ns3/packet.h" +#include "socket.h" + +NS_DEBUG_COMPONENT_DEFINE ("Socket"); namespace ns3 { Socket::~Socket () -{} +{ + NS_DEBUG("Socket::~Socket ()"); +} void Socket::SetCloseCallback (Callback > closeCompleted) { + NS_DEBUG("Socket::SetCloseCallback ()"); m_closeCompleted = closeCompleted; } + void -Socket::SetConnectCallback (Callback > connectionSucceeded, - Callback > connectionFailed, - Callback > halfClose) +Socket::SetConnectCallback ( + Callback > connectionSucceeded, + Callback > connectionFailed, + Callback > halfClose) { + NS_DEBUG("Socket::SetConnectCallback ()"); m_connectionSucceeded = connectionSucceeded; m_connectionFailed = connectionFailed; m_halfClose = halfClose; } + void -Socket::SetAcceptCallback (Callback, const Address &> connectionRequest, - Callback, const Address&> newConnectionCreated, - Callback > closeRequested) +Socket::SetAcceptCallback ( + Callback, const Address &> connectionRequest, + Callback, const Address&> newConnectionCreated, + Callback > closeRequested) { + NS_DEBUG("Socket::SetAcceptCallback ()"); m_connectionRequest = connectionRequest; m_newConnectionCreated = newConnectionCreated; m_closeRequested = closeRequested; } + void Socket::SetSendCallback (Callback, uint32_t> dataSent) { + NS_DEBUG("Socket::SetSendCallback ()"); m_dataSent = dataSent; } + void Socket::SetRecvCallback (Callback, const Packet &,const Address&> receivedData) { + NS_DEBUG("Socket::SetRecvCallback ()"); m_receivedData = receivedData; } void Socket::NotifyCloseCompleted (void) { + NS_DEBUG("Socket::NotifyCloseCompleted ()"); + if (!m_closeCompleted.IsNull ()) { m_closeCompleted (this); } } + void Socket::NotifyConnectionSucceeded (void) { + NS_DEBUG("Socket::NotifyConnectionSucceeded ()"); + if (!m_connectionSucceeded.IsNull ()) { m_connectionSucceeded (this); } } + void Socket::NotifyConnectionFailed (void) { + NS_DEBUG("Socket::NotifyConnectionFailed ()"); + if (!m_connectionFailed.IsNull ()) { m_connectionFailed (this); } } + void Socket::NotifyHalfClose (void) { + NS_DEBUG("Socket::NotifyHalfClose ()"); + if (!m_halfClose.IsNull ()) { m_halfClose (this); } } + bool Socket::NotifyConnectionRequest (const Address &from) { + NS_DEBUG("Socket::NotifyConnectionRequest ()"); + if (!m_connectionRequest.IsNull ()) { return m_connectionRequest (this, from); @@ -106,39 +137,49 @@ Socket::NotifyConnectionRequest (const Address &from) return false; } } + void Socket::NotifyNewConnectionCreated (Ptr socket, const Address &from) { + NS_DEBUG("Socket::NotifyNewConnectionCreated ()"); + if (!m_newConnectionCreated.IsNull ()) { m_newConnectionCreated (socket, from); } } + void Socket::NotifyCloseRequested (void) { + NS_DEBUG("Socket::NotifyCloseRequested ()"); + if (!m_closeRequested.IsNull ()) { m_closeRequested (this); } } + void Socket::NotifyDataSent (uint32_t size) { + NS_DEBUG("Socket::NotifyDataSent ()"); + if (!m_dataSent.IsNull ()) { m_dataSent (this, size); } } + void Socket::NotifyDataReceived (const Packet &p, const Address &from) { + NS_DEBUG("Socket::NotifyDataReceived ()"); + if (!m_receivedData.IsNull ()) { m_receivedData (this, p, from); } } - - }//namespace ns3