Meng Wang
Meng Wang

Reputation: 111

Implement Simple TCP with NS3

hi, I'm new to NS3, I'm trying to modify first.cc to do TCP rather than UDP. I didn't get any error, but no data exchange happened as well. Could anyone help me on this?˜˜ many thanks˜~ here is the code: in the main:

    NS_LOG_INFO ("Creating Topology");
    Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (137));

    Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("14kb/s"));
  NodeContainer nodes;
  nodes.Create (2);
//
  PointToPointHelper pointToPoint; 
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("20ms"));
//
  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);
  InternetStackHelper stack;
  stack.Install (nodes);
    Ipv4AddressHelper address;
    address.SetBase ("109.11.12.0", "255.255.255.0");//address setting

    Ipv4InterfaceContainer interfaces = address.Assign (devices);

    OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address
                       (InetSocketAddress (Ipv4Address ("255.255.255.0"), 10)));
    onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
    onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));

    onOffHelper.SetAttribute ("DataRate",StringValue ("2Mbps"));
    onOffHelper.SetAttribute ("PacketSize",UintegerValue(2000));
    ApplicationContainer app = onOffHelper.Install (nodes.Get (0));

    // Start the application
    app.Start (Seconds (10.0));
    app.Stop (Seconds (100.0));

    // Create an optional packet sink to receive  packets

    PacketSinkHelper sink ("ns3::TcpSocketFactory",Address
                           (InetSocketAddress (Ipv4Address::GetAny (), 10)));
    app = sink.Install (nodes.Get(1));
    app.Start (Seconds (1.0));
    app.Stop (Seconds (100.0));

    pointToPoint.EnablePcapAll ("testtcp");

Upvotes: 4

Views: 9873

Answers (3)

Salah Amean
Salah Amean

Reputation: 1

Actually TCP is not meant to support Broadcasting that is why some protocol opt to use UDP rather that TCP, to overcome such problem . For instance, Bootp which makes its own way on UDP since the initial state of the protocol is not supported with TCP.

Upvotes: 0

Meng Wang
Meng Wang

Reputation: 111

ok,I think it is solved now, first, the IP was wrong at onoff helper, they should be the same as Ipv4AddressHelper. then the app start time is wrong, and the config onoff application code are not needed. here is the code now: it might not be very right, but at least I can read result from it now.

NodeContainer nodes;
  nodes.Create (2); //creat 2 nodes they are p2p

  PointToPointHelper pointToPoint; 
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("2Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("20ms"));

  NetDeviceContainer devices;// put nodes in ndc
  devices = pointToPoint.Install (nodes);
////give them an address 
  InternetStackHelper stack;
  stack.Install (nodes);
    Ipv4AddressHelper address;
    address.SetBase ("109.11.12.0", "255.255.255.0");//address setting

    Ipv4InterfaceContainer interfaces = address.Assign (devices);

    //sink for reciever????
    PacketSinkHelper sink ("ns3::TcpSocketFactory",Address
                           (InetSocketAddress (Ipv4Address::GetAny (), 10)));
    //set a node as reciever
    ApplicationContainer app = sink.Install (nodes.Get(0));

    app.Start (Seconds (1.0));
    app.Stop (Seconds (10.0));

    OnOffHelper onOffHelper ("ns3::TcpSocketFactory", Address
                       (InetSocketAddress (Ipv4Address ("109.11.12.1"), 10)));
    onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
    onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));

    onOffHelper.SetAttribute ("DataRate",StringValue ("2Mbps"));
    onOffHelper.SetAttribute ("PacketSize",UintegerValue(1280));
   // ApplicationContainer
    app    = onOffHelper.Install (nodes.Get (1));
    // Start the application
    app.Start (Seconds (1.0));
    app.Stop (Seconds (10.0));




    pointToPoint.EnablePcapAll ("testtcp");
  Simulator::Run ();

Upvotes: 3

mathieu
mathieu

Reputation: 3174

Are you really trying to send TCP traffic to ip address 255.255.255.0 ? That is unlikely to work ever. Maybe you should try instead 109.11.12.1

Upvotes: 0

Related Questions