Simple example for sending packets over a packet-socket

1,844 views
Skip to first unread message

Darius Malysiak

unread,
Apr 3, 2012, 11:26:16 AM4/3/12
to ns-3-users
Hello,

i am currently trying to send packets over a packet socket, i already
succeded in receiving packets via a packet socket. Yet, i had no
success in sending them. Each time i receive the following output /
error:

0
assert failed. cond="m_current + delta <= m_dataEnd", file=./ns3/
buffer.h, line=678

Here is my code:

void startFlowPacketSocket(Ptr<Socket> localSocket, uint32_t nBytes)
{
Ptr<ns3::Packet> packet = Create<Packet>(nBytes);
int accepted_bytes = localSocket->Send(packet);
printf("%d\n", accepted_bytes);
}

Ptr<Socket> initPacketSocket(Ptr<Node> c0, unsigned int ifIndex,
Address target)
{
Ptr<Socket> localSocket = Socket::CreateSocket(c0,
PacketSocketFactory::GetTypeId());
//only for packetsockets
ns3::PacketSocketAddress sinkAddress;
sinkAddress.SetSingleDevice (ifIndex); // set outgoing interface
for outgoing packets
sinkAddress.SetPhysicalAddress(target); // set destination address
for outgoing packets
sinkAddress.SetProtocol(2); // set arbitrary protocol for
outgoing packets
if(localSocket->Bind() ==-1)
{
printf("error during binding on source\n");
}

//only for packet sockets
if(localSocket->Connect(sinkAddress) ==-1)
{
printf("error during connect on source\n");
}

return localSocket;
}

int main (int argc, char *argv[])
{
//create a node container
NodeContainer c0;
//create two nodes inside the container
c0.Create (2);

//create csma channel and configure attributes
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue ("10Mbps"));
csma.SetChannelAttribute("Delay", TimeValue (MicroSeconds(500)));
NetDeviceContainer devices = csma.Install (c0);

//add internet stack to nodes
InternetStackHelper internet;
internet.Install (c0);
//add ip addresses to csma devices
Ipv4AddressHelper ipv4;
ipv4.SetBase("192.168.0.0", "255.255.255.0");
Ipv4InterfaceContainer ipInterfaces = ipv4.Assign(devices);
Ptr<Socket> localSocket = initPacketSocket(c0,devices.Get(0)-
>GetIfIndex(),devices.Get(1)->GetAddress());

Simulator::ScheduleNow(&startFlowPacketSocket, localSocket,
1);

Simulator::Stop (Seconds(15));
Simulator::Run ();
Simulator::Destroy ();
}

Hajar Hantouti

unread,
Aug 24, 2015, 7:19:39 AM8/24/15
to ns-3-users, darius....@googlemail.com
Hello Darius,
Thank you fot this code.
I run it  ans I get the following error:
error: ‘PacketSocketAddress’ is not a member of ‘ns3’
         ns3::PacketSocketAddress sinkAddress;
could you tell me wich files I have to include.
Cordially;
Hajar

Konstantinos

unread,
Aug 24, 2015, 7:33:47 AM8/24/15
to ns-3-users
Hi Hajar,

You replied to a post 3yr old that is (a) un-answered, (b) the code is not running as expected and (c) the API may have changed since it's been more than 6 ns-3 releases.
This is not wise to do in the future. 

Now, for your error, have you looked at the API (in the ns-3 release you are using)?
Assuming you use the latest release here is a link to the PacketSocketAddress

The header file defining this class is, as expected, "ns3/packet-socket-address.h".

Regards,
K.
Reply all
Reply to author
Forward
0 new messages