Add a tcp server example
authorRaj Bhattacharjea <raj.b@gatech.edu>
Wed Jul 02 15:45:35 2008 -0400 (19 months ago)
changeset 3366923e47a54fe8
parent 3365 6409d2460601
child 3367 8587b5520e63
Add a tcp server example
examples/tcp-star-server.cc
examples/wscript
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/examples/tcp-star-server.cc	Wed Jul 02 15:45:35 2008 -0400
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     1.5 +/*
     1.6 + * This program is free software; you can redistribute it and/or modify
     1.7 + * it under the terms of the GNU General Public License version 2 as
     1.8 + * published by the Free Software Foundation;
     1.9 + *
    1.10 + * This program is distributed in the hope that it will be useful,
    1.11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.13 + * GNU General Public License for more details.
    1.14 + *
    1.15 + * You should have received a copy of the GNU General Public License
    1.16 + * along with this program; if not, write to the Free Software
    1.17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.18 + *
    1.19 + */
    1.20 +
    1.21 +
    1.22 +// Default Network topology, 9 nodes in a star
    1.23 +/*
    1.24 +          n2 n3 n4
    1.25 +           \ | /
    1.26 +            \|/
    1.27 +       n1---n0---n5
    1.28 +            /|\
    1.29 +           / | \
    1.30 +          n8 n7 n6
    1.31 +*/
    1.32 +// - CBR Traffic goes from the star "arms" to the "hub"
    1.33 +// - Tracing of queues and packet receptions to file
    1.34 +//   "tcp-star-server.tr"
    1.35 +// - pcap traces also generated in the following files
    1.36 +//   "tcp-star-server-$n-$i.pcap" where n and i represent node and interface
    1.37 +//   numbers respectively
    1.38 +// Usage examples for things you might want to tweak:
    1.39 +//       ./waf --run="tcp-star-server"
    1.40 +//       ./waf --run="tcp-star-server --nNodes=25"
    1.41 +//       ./waf --run="tcp-star-server --ns3::OnOffApplication::DataRate=10000"
    1.42 +//       ./waf --run="tcp-star-server --ns3::OnOffApplication::PacketSize=500"
    1.43 +// See the ns-3 tutorial for more info on the command line: 
    1.44 +// http://www.nsnam.org/tutorials.html
    1.45 +
    1.46 +
    1.47 +
    1.48 +
    1.49 +#include <iostream>
    1.50 +#include <fstream>
    1.51 +#include <string>
    1.52 +#include <cassert>
    1.53 +
    1.54 +#include "ns3/core-module.h"
    1.55 +#include "ns3/simulator-module.h"
    1.56 +#include "ns3/node-module.h"
    1.57 +#include "ns3/helper-module.h"
    1.58 +#include "ns3/global-route-manager.h"
    1.59 +
    1.60 +using namespace ns3;
    1.61 +
    1.62 +NS_LOG_COMPONENT_DEFINE ("TcpServer");
    1.63 +
    1.64 +int 
    1.65 +main (int argc, char *argv[])
    1.66 +{
    1.67 +  // Users may find it convenient to turn on explicit debugging
    1.68 +  // for selected modules; the below lines suggest how to do this
    1.69 +
    1.70 +  //LogComponentEnable ("TcpServer", LOG_LEVEL_INFO);
    1.71 +  //LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
    1.72 +  //LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);
    1.73 +  //LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
    1.74 +  //
    1.75 +  // Make the random number generators generate reproducible results.
    1.76 +  //
    1.77 +  RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
    1.78 +
    1.79 +  // Set up some default values for the simulation.
    1.80 +  Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (250));
    1.81 +  Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("5kb/s"));
    1.82 +  uint32_t N = 9; //number of nodes in the star
    1.83 +
    1.84 +  // Allow the user to override any of the defaults and the above
    1.85 +  // Config::SetDefault()s at run-time, via command-line arguments
    1.86 +  CommandLine cmd;
    1.87 +  cmd.AddValue("nNodes", "Number of nodes to place in the star", N);
    1.88 +  cmd.Parse (argc, argv);
    1.89 +
    1.90 +  // Here, we will create N nodes in a star.
    1.91 +  NS_LOG_INFO ("Create nodes.");
    1.92 +  NodeContainer serverNode;
    1.93 +  NodeContainer clientNodes;
    1.94 +  serverNode.Create(1);
    1.95 +  clientNodes.Create(N-1);
    1.96 +  NodeContainer allNodes = NodeContainer(serverNode, clientNodes);
    1.97 +
    1.98 +  // Install network stacks on the nodes
    1.99 +  InternetStackHelper internet;
   1.100 +  internet.Install (allNodes);
   1.101 +
   1.102 +  //Collect an adjacency list of nodes for the p2p topology
   1.103 +  std::vector<NodeContainer> nodeAdjacencyList(N-1);
   1.104 +  for(uint32_t i=0; i<nodeAdjacencyList.size(); ++i)
   1.105 +  {
   1.106 +    nodeAdjacencyList[i] = NodeContainer (serverNode, clientNodes.Get (i));
   1.107 +  }
   1.108 +
   1.109 +  // We create the channels first without any IP addressing information
   1.110 +  NS_LOG_INFO ("Create channels.");
   1.111 +  PointToPointHelper p2p;
   1.112 +  p2p.SetDeviceParameter ("DataRate", StringValue ("5Mbps"));
   1.113 +  p2p.SetChannelParameter ("Delay", StringValue ("2ms"));
   1.114 +  std::vector<NetDeviceContainer> deviceAdjacencyList(N-1);
   1.115 +  for(uint32_t i=0; i<deviceAdjacencyList.size(); ++i)
   1.116 +  {
   1.117 +    deviceAdjacencyList[i] = p2p.Install (nodeAdjacencyList[i]);
   1.118 +  }
   1.119 +
   1.120 +  // Later, we add IP addresses.  
   1.121 +  NS_LOG_INFO ("Assign IP Addresses.");
   1.122 +  Ipv4AddressHelper ipv4;
   1.123 +  std::vector<Ipv4InterfaceContainer> interfaceAdjacencyList(N-1);
   1.124 +  for(uint32_t i=0; i<interfaceAdjacencyList.size(); ++i)
   1.125 +  {
   1.126 +    std::ostringstream subnet;
   1.127 +    subnet<<"10.1."<<i+1<<".0";
   1.128 +    ipv4.SetBase (subnet.str().c_str(), "255.255.255.0");
   1.129 +    interfaceAdjacencyList[i] = ipv4.Assign (deviceAdjacencyList[i]);
   1.130 +  }
   1.131 +
   1.132 +  //Turn on global static routing
   1.133 +  GlobalRouteManager::PopulateRoutingTables ();
   1.134 +
   1.135 +  // Create a packet sink on the star "hub" to receive these packets
   1.136 +  uint16_t port = 50000;
   1.137 +  Address sinkLocalAddress(InetSocketAddress (Ipv4Address::GetAny (), port));
   1.138 +  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
   1.139 +  ApplicationContainer sinkApp = sinkHelper.Install (serverNode);
   1.140 +  sinkApp.Start (Seconds (1.0));
   1.141 +  sinkApp.Stop (Seconds (10.0));
   1.142 +
   1.143 +  // Create the OnOff applications to send TCP to the server
   1.144 +  OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());
   1.145 +  clientHelper.SetAttribute 
   1.146 +      ("OnTime", RandomVariableValue (ConstantVariable (1)));
   1.147 +  clientHelper.SetAttribute 
   1.148 +      ("OffTime", RandomVariableValue (ConstantVariable (0)));
   1.149 +  //normally wouldn't need a loop here but the server IP address is different
   1.150 +  //on each p2p subnet
   1.151 +  ApplicationContainer clientApps;
   1.152 +  for(uint32_t i=0; i<clientNodes.GetN(); ++i)
   1.153 +  {
   1.154 +    AddressValue remoteAddress
   1.155 +        (InetSocketAddress (interfaceAdjacencyList[i].GetAddress (0), port));
   1.156 +    clientHelper.SetAttribute ("Remote", remoteAddress);
   1.157 +    clientApps.Add(clientHelper.Install (clientNodes.Get(i)));
   1.158 +  }
   1.159 +  clientApps.Start (Seconds (1.0));
   1.160 +  clientApps.Stop (Seconds (10.0));
   1.161 +
   1.162 +
   1.163 +  //configure tracing
   1.164 +  std::ofstream ascii;
   1.165 +  ascii.open ("tcp-server.tr");
   1.166 +  PointToPointHelper::EnablePcapAll ("tcp-server");
   1.167 +  PointToPointHelper::EnableAsciiAll (ascii);
   1.168 +
   1.169 +  NS_LOG_INFO ("Run Simulation.");
   1.170 +  Simulator::Run ();
   1.171 +  Simulator::Destroy ();
   1.172 +  NS_LOG_INFO ("Done.");
   1.173 +
   1.174 +  return 0;
   1.175 +}
     2.1 --- a/examples/wscript	Wed Jul 02 03:16:36 2008 -0700
     2.2 +++ b/examples/wscript	Wed Jul 02 15:45:35 2008 -0400
     2.3 @@ -52,6 +52,10 @@
     2.4          ['point-to-point', 'internet-stack'])
     2.5      obj.source = 'tcp-large-transfer.cc'
     2.6  
     2.7 +    obj = bld.create_ns3_program('tcp-star-server',
     2.8 +        ['point-to-point', 'internet-stack'])
     2.9 +    obj.source = 'tcp-star-server.cc'
    2.10 +
    2.11      obj = bld.create_ns3_program('wifi-adhoc',
    2.12                                   ['core', 'simulator', 'mobility', 'wifi'])
    2.13      obj.source = 'wifi-adhoc.cc'