#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/csma-module.h"
#include <string>
#include <iostream>
using namespace std;
using namespace ns3;
NS_LOG_COMPONENT_DEFINE( "MyFirstNS3Project" );
int main(int argc, char *argv[])
{
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper p2pHelper;
p2pHelper.SetDeviceAttribute( "DataRate", StringValue("5Mbps") );
p2pHelper.SetChannelAttribute( "Delay", StringValue("2ms") );
NetDeviceContainer devices;
devices = p2pHelper.Install( nodes );
//Rate Error Model
Ptr<RateErrorModel> em = CreateObject<RateErrorModel>();
em->SetAttribute( "ErrorRate", DoubleValue(0.00001) );
devices.Get(1)->SetAttribute( "RecieveErrorModel", PointerValue(em) );
//Agreement Stack
InternetStackHelper stack;
stack.Install( nodes );
//Set IP Address
Ipv4AddressHelper addressHelper;
addressHelper.SetBase( "10.1.1.0", "255.255.255.252" );
Ipv4InterfaceContainer interfaces = addressHelper.Assign( devices ); //interfaces
uint16_t sinkPort = 8080;
Address sinkAddress( InetSocketAddress( interfaces.GetAddress(1), sinkPort ) );
PacketSinkHelper packetSinkHelper( "TcpSocketFactory", InetSocketAddress( Ipv4Address::GetAny(), sinkPort ) );
ApplicationContainer sinkApps = packetSinkHelper.Install( nodes.Get(1) ); //Set send-message-application in nodes.Get(1); port : 8080
sinkApps.Start( Seconds(0.0) );
sinkApps.Stop( Seconds(10.0) );
uint32_t a = 1040, b = 1000;
Ptr<MyApp> app = CreateObject <MyApp> ();
Ptr<Socket> p = TcpSocket;
DataRate d("1Mbps");
app->Setup(p, sinkAddress, a, b, d);
nodes.Get(0)->AddApplication(app); //Set receive-message-application in nodes.Get(0);
app->SetStartTime( Seconds(1.0) );
app->SetStopTime( Seconds(10.0) );
AnimationInterface anim("MyFirstNS3Project.xml");
Simulator::Run();
Simulator::Destroy();
return 0;
}
// 2016/7/13 wasdns
void
MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
{
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
}
//uint32_t a = 1040, b = 1000;
Ptr<MyApp> app = CreateObject <MyApp> ();
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket(nodes.Get(0), TcpSocketFactory::GetTypeId());
app->Setup(ns3TcpSocket, sinkAddress);
void Setup (Ptr<Socket>socket, Address address, uint32_t packetSize = 1040, uint32_t nPackets = 1000);
void
MyApp::Setup (Ptr<Socket>socket, Address address, uint32_t packetSize, uint32_t nPackets)
{
DataRate dataRate("1Mbps");
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
}
Build Successfully!