udp socket

145 views
Skip to first unread message

salma

unread,
Sep 10, 2016, 3:33:31 PM9/10/16
to ns-3-users
Hi, 
I use socket to send a packet from a node to another. I verified that the packet is received through callback method. But, when I want to read the packet from the receiver, I obtain no thing. Can you please help me to find the problem. This is the code 

ns3::PacketMetadata::Enable () ;
Ptr<Socket> recvSink = Socket::CreateSocket (container.Get (1), tid);
InetSocketAddress local = InetSocketAddress ((Ipv4Address::GetAny()), dlPort);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
Ptr<Socket> source;
source = Socket::CreateSocket (container.Get (0), tid);

InetSocketAddress remote = InetSocketAddress (wifiInterfaces.GetAddress(0), dlPort);
source->Connect(remote);
Ptr <Packet> p=Create<Packet> (1024);
  source->Send (p);
Ptr <Packet> packet;
Address address= wifiInterfaces.GetAddress(u);
LogComponentEnable("UdpSocket",LOG_LEVEL_INFO);
packet = recvSink->RecvFrom (address);
std::cout<<p->GetSize();
while ((packet = recvSink->RecvFrom (address)))
    {
//packet = recvSink->RecvFrom (address);
      if (packet->GetSize () > 0)
        {
          SeqTsHeader seqTs;
          packet->RemoveHeader (seqTs);
        //  uint32_t currentSequenceNumber = seqTs.GetSeq ();
          if (InetSocketAddress::IsMatchingType (address))
            {
              std::cout<<"At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
                       InetSocketAddress::ConvertFrom (address).GetIpv4 () << " port " <<
                       InetSocketAddress::ConvertFrom (address).GetPort ();


            }
}
}

Tommaso Pecorella

unread,
Sep 10, 2016, 7:17:16 PM9/10/16
to ns-3-users
Hi,

probably you didn't fully grasp what a discrete event simulator is. I'd strongly suggest to read the manual first chapters.

T.

salma

unread,
Sep 11, 2016, 5:41:26 AM9/11/16
to ns-3-users
Hi Tommaso, 
Thank you for your reply. I read about events and simulator in ns-3. I undrestood that the packet was already received and thus when I used the loop while (while ((packet = recvSink->RecvFrom (address)))), it will return no thing because the packet transmission is finished. Is there another way to retrieve the received packet from the receiver and to obtain the reception time?
1

Tommaso Pecorella

unread,
Sep 11, 2016, 8:08:53 AM9/11/16
to ns-3-users
Hi,

of course there is. Simply don't try to use the simulator in the "right" way. I'd suggest to study the tutorial and examples, you'll find plenty of code showing how to handle packet reception and process the data.

T.
Message has been deleted

salma

unread,
Sep 14, 2016, 10:16:52 AM9/14/16
to ns-3-users
Hi,
I created the following void handleRead and I record the information in a file. But, when I call it inside my script (through callback function) and I read the file, I found that it is empty.
HandleRead (Ptr<Socket> socket)
{
 
std::ofstream outfile ("data.txt");

  Ptr<Packet> packet;
  Address from;
  while ((packet = socket->RecvFrom (from)))
    {
      if (packet->GetSize () > 0)
        {
          SeqTsHeader seqTs;
          packet->RemoveHeader (seqTs);
          if (InetSocketAddress::IsMatchingType (from))
            {
              std::cout<<"At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
                       InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
                       InetSocketAddress::ConvertFrom (from).GetPort ();

outfile << "true" << std::endl;
outfile <<  InetSocketAddress::ConvertFrom (from).GetIpv4 () << std::endl;
outfile<< Simulator::Now ().GetSeconds () << std::endl;
outfile.close();

Tommaso Pecorella

unread,
Sep 14, 2016, 7:14:53 PM9/14/16
to ns-3-users
Hi,

most probably you did not connect the callback to the handler in the right way. Please note that Config will not complain about bad paths.
I'd strongly suggest to double check that.

T.

salma

unread,
Sep 15, 2016, 3:52:06 AM9/15/16
to ns-3-users
Hi, 
In my script, I create the source and sink sockets and I connect the callback to the handler as follows:

Ptr<Socket> recvSink = Socket::CreateSocket (container.Get (0), tid);
InetSocketAddress local = InetSocketAddress ((Ipv4Address::GetAny()), 80);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&handleRead)); //callback
Ptr<Socket> source = Socket::CreateSocket (container.Get (1), tid);
   InetSocketAddress remote = InetSocketAddress (wifiInterfaces.GetAddress(0), 80);
    source->Connect (remote);
Ptr <Packet> p=Create<Packet> (1024);
  source->Send (p);

Tommaso Pecorella

unread,
Sep 16, 2016, 5:10:41 AM9/16/16
to ns-3-users
Are you sure that the sink is receiving packets ?

T.
Message has been deleted

salma

unread,
Sep 16, 2016, 12:28:04 PM9/16/16
to ns-3-users
Hi,
Yes, when the script is running, the std::cout message if the callback message is printed. But, when I want to read the content of the created file, I found it empty. After the simulation was finished, I found that the file was created. 

salma

unread,
Sep 17, 2016, 2:10:47 PM9/17/16
to ns-3-users
Hi, 
This is my script.
script.cc

Tommaso Pecorella

unread,
Sep 19, 2016, 6:32:04 PM9/19/16
to ns-3-users
The file has one line (because you did send one packet only).
Your problem is not in the ns-3 part. It is rather bound to how you handle the file opening and closing. I'd suggest to check this:

In particular pay attention to the append option.

T.

salma

unread,
Sep 20, 2016, 4:13:49 PM9/20/16
to ns-3-users
hi,
thank you for your reply
When I run again the simulation, I found that the file was created and contained the information of the first simulation (I changed the id of the nodes used as sink and source). I think that the data is stored temporarily in a buffer  and the file is created at the end of the simulation. So, it is not possible to get the data from the file in the current simulation. 

Tommaso Pecorella

unread,
Sep 20, 2016, 8:18:05 PM9/20/16
to ns-3-users
The file is flushed (i.e., all the pending content is written) when it's closed... well, at least it should.
In any case the answer is: I'd rather not do that. Why one should use a temporary file to do that ?

Cheers,

T.

salma

unread,
Sep 21, 2016, 3:11:34 PM9/21/16
to ns-3-users
hi, 
I need to obtain information related to the received packet (source, reception time)  in my script. For this reason, I thought that the only possible solution is to store data in a file.

Tommaso Pecorella

unread,
Sep 21, 2016, 6:38:43 PM9/21/16
to ns-3-users
Wrong thought.

T.
Reply all
Reply to author
Forward
0 new messages