Skip to content

Instantly share code, notes, and snippets.

@Luavis
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luavis/c02c4acd5e1fbe3fb583 to your computer and use it in GitHub Desktop.
Save Luavis/c02c4acd5e1fbe3fb583 to your computer and use it in GitHub Desktop.
tcp-example
$ tcpdump -qns 0 -A -r ./tcp-transfer-0-0.pcap
reading from file ./tcp-transfer-0-0.pcap, link-type PPP (PPP)
09:00:00.000000 IP 10.1.3.1.49153 > 10.1.3.2.80: tcp 0
.!E..8....@...
...
......P....................
...........
09:00:00.020079 IP 10.1.3.2.80 > 10.1.3.1.49153: tcp 0
.!E..(....@...
...
....P..........P.......
09:00:00.020079 IP 10.1.3.1.49153 > 10.1.3.2.80: tcp 0
.!E..(....@...
...
......P........P.......
$ tcpdump -qns 0 -A -r ./tcp-transfer-1-0.pcap
reading from file ./tcp-transfer-1-0.pcap, link-type PPP (PPP)
09:00:00.010046 IP 10.1.3.1.49153 > 10.1.3.2.80: tcp 0
.!E..8....@...
...
......P....................
...........
09:00:00.010046 IP 10.1.3.2.80 > 10.1.3.1.49153: tcp 0
.!E..(....@...
...
....P..........P.......
09:00:00.030113 IP 10.1.3.1.49153 > 10.1.3.2.80: tcp 0
.!E..(....@...
...
......P........P.......
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include <iostream>
#include <fstream>
#include <string>
#include "ns3/core-module.h"
#include "ns3/applications-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/ipv4-global-routing-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("TcpTransfer");
void StartFlow (Ptr<Socket> localSocket,
Ipv4Address servAddress,
uint16_t servPort);
void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t txSpace);
static const uint32_t totalTxBytes = 10000;
static const uint32_t writeSize = 1040;
static uint32_t currentTxBytes = 0;
uint8_t data[writeSize];
int main (int argc, char *argv[])
{
NodeContainer n0n1;
n0n1.Create (2);
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (10000000)));
p2p.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10)));
// And then install devices and channels connecting our topology.
NetDeviceContainer dev0 = p2p.Install (n0n1);
// Now add ip/tcp stack to all nodes.
InternetStackHelper internet;
internet.InstallAll ();
// Later, we add IP addresses.
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.3.0", "255.255.255.0");
Ipv4InterfaceContainer ipInterfs = ipv4.Assign (dev0);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
uint16_t servPort = 80;
Ptr<Socket> localSocket = Socket::CreateSocket (n0n1.Get(0), TcpSocketFactory::GetTypeId());
localSocket->Bind ();
Simulator::ScheduleNow (&StartFlow, localSocket, ipInterfs.GetAddress(1), servPort);
localSocket->SetAttribute("SndBufSize", UintegerValue(4096));
//Ask for ASCII and pcap traces of network traffic
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream ("tcp-transfer.tr"));
p2p.EnablePcapAll ("tcp-transfer");
// Finally, set up the simulator to run. The 1000 second hard limit is a
// failsafe in case some change above causes the simulation to never end
Simulator::Stop (Seconds (1000));
Simulator::Run ();
Simulator::Destroy ();
}
void StartFlow (Ptr<Socket> localSocket,
Ipv4Address servAddress,
uint16_t servPort)
{
localSocket->Connect (InetSocketAddress (servAddress, servPort));
localSocket->SetSendCallback (MakeCallback (&WriteUntilBufferFull));
WriteUntilBufferFull (localSocket, localSocket->GetTxAvailable());
}
void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t txSpace)
{
while (currentTxBytes < totalTxBytes && localSocket->GetTxAvailable () > 0) {
uint32_t left = totalTxBytes - currentTxBytes;
uint32_t dataOffset = currentTxBytes % writeSize;
uint32_t toWrite = writeSize - dataOffset;
toWrite = std::min (toWrite, left);
toWrite = std::min (toWrite, localSocket->GetTxAvailable ());
int amountSent = localSocket->Send (&data[dataOffset], toWrite, 0);
if(amountSent < 0) {
// we will be called again when new tx space becomes available.
return;
}
currentTxBytes += amountSent;
}
localSocket->Close ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment