I am using the following piece of code
void ReceivePacket (Ptr<Socket> socket)
{
NS_LOG_UNCOND("Received a Packet!");
}
....
....
NodeContainer c;
........
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (1), tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (),
80);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
I want to retrieve the IP address (of the receiver) associated with
the socket in the ReceivePacket() method. How do I do that in NS3?
Thanks for your help
Aditya Bhave
Address addr;
recvSink->GetSockName (addr);
InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
--Aditya Bhave
try:
std::cout << iaddr.GetIpv4 () << ":" << iaddr.GetPort () << std::endl;
> Also I want to print the address of the node that sent the packet. Is
> this possible with my current setup, or will I need to code some other
> type of packet sink?
The plan is to put a tag on the packet to convey IP_PKTINFO but this
patch needs to be finished off. See this tracker issue:
http://www.nsnam.org/bugzilla/show_bug.cgi?id=671
- Tom
void
ReceiverReceiveCb (Ptr<Socket> socket)
{
Address addr;
socket->GetPeerName (addr);
InetSocketAddress iaddr = InetSocketAddress::ConvertFrom (addr);
std::cout << "Received one packet! Socket: " << iaddr.GetIpv4 () << " port: " << iaddr.GetPort ();
}