How to send a customized msg in a packet

303 views
Skip to first unread message

RBM-IT

unread,
Jan 26, 2016, 5:33:53 AM1/26/16
to ns-3-users
Hello All,

This topic may be investigated a lot in this group but for different scenarios. Thus, I am asking my question here again.

I want to send customized message between nodes in the packet. I want to embed this message in the packet when sent. Then read it  in the receiver side.
I read about header and tag solution, but was hesitating to implement a whole solution, when it may be simpler. Since i think such messages should be in the data part not headers. ( please correct me if i am wrong)
For example i have too applications:  my server application and client application.
I want to send from the server to the client this message "go to location x".
In the client side , i want to read this message first , then send a simple packet to accept or reject this order.

I hope my scenario is clear.

Waiting for your recommandations.
Best Regards,

Konstantinos

unread,
Jan 26, 2016, 6:50:29 AM1/26/16
to ns-3-users
Hi,

The documentation and discussions in the list explain how you can add real data on a packet.
The fact that you have a different scenario does not make it a different solution. The principle is the same.
You have two options, (a) use a buffer to store your string and create the packet with that string or (b) use a header to have structured read/write operations.
The effect will be the same. I would recommend to use the header as it is simpler and more extensible.
For your scenario particularly it will be easier to pass the variable you need. Do not hesitate to implement it. 
The server application would create a packet with zero bytes, add this custom header and send it.
The client would read the header and act accordingly. You can have several options/fields in the header for different types of commands.

Regards,
K.

RBM-IT

unread,
Jan 26, 2016, 8:52:16 AM1/26/16
to ns-3-users
Dear Konstantinos,

Thank you for your answer. I will implement then the header solution as detailed here:

I will inform you if anything goes wrong.

Regards

RBM-IT

unread,
Jan 28, 2016, 4:56:55 AM1/28/16
to ns-3-users
Dear Konstantinos,

So i implemented the example detailed in the link below. I created MyHeader.h and Myheader.cc, Then i called in the sender application side:

Packet::EnablePrinting ();

    MyHeader sourceHeader;

    sourceHeader.SetData (m_header);

p = Create<Packet> (m_size);

      p->AddHeader (sourceHeader);

      p->Print (std::cout);

      std::cout << std::endl;


and in the receiver application side

MyHeader destinationheader;

            packet->RemoveHeader (destinationheader);

            destinationheader.Print(std::cout);

            std::cout << std::endl;


            uint16_t data= destinationheader.GetData ();

The problem is i send for example data=2

when I print the destinationheader i got data=2

But with the GetData() method i got 12.

data=2

At time 1718.01s participant received 1024 bytes from 10.1.1.1 port 49153 as 12


What is the problem?


Regards

Konstantinos

unread,
Jan 28, 2016, 5:30:21 AM1/28/16
to ns-3-users
Hi,

I just created a simple scenario based on the documentation and your sample code and it works just fine. 
I add "2" and I read "2". 
See the attached. Just place it in scratch and run. 
I use the same 'packet' to add/remove the header. 

K.
ns3user.cc

RBM-IT

unread,
Jan 28, 2016, 5:44:01 AM1/28/16
to ns-3-users
Hi.

Thank you for your prompt answer.
I compiled the provided file and it works great.
However in my case i call thenAddheader and remove header in separate files. add in the sender application (app-requestor)  and the removeheader in a receiver file (app-partcipant). These files are modified applications from the udp-echo-client and udp-echo-server applications.
To Send :

void 

AppRequestor::Send (void)

{

  NS_LOG_FUNCTION (this);


  NS_ASSERT (m_sendEvent.IsExpired ());

    double d = AppRequestor::distance(m_node1);


    if (d<=10) {

    Packet::EnablePrinting ();

    MyHeader sourceHeader;

    sourceHeader.SetData (m_header);


   Ptr<Packet> p;

        //Add the new header to this packet

  if (m_dataSize)

    {

      //

      // If m_dataSize is non-zero, we have a data buffer of the same size that we

      // are expected to copy and send.  This state of affairs is created if one of

      // the Fill functions is called.  In this case, m_size must have been set

      // to agree with m_dataSize

      //

      NS_ASSERT_MSG (m_dataSize == m_size, "AppRequestor::Send(): m_size and m_dataSize inconsistent");

      NS_ASSERT_MSG (m_data, "AppRequestor::Send(): m_dataSize but no m_data");

      p = Create<Packet> (m_data, m_dataSize);

      p->AddHeader (sourceHeader);

      p->Print (std::cout);

      std::cout << std::endl;

    }

  else

    {

      //

      // If m_dataSize is zero, the client has indicated that it doesn't care

      // about the data itself either by specifying the data size by setting

      // the corresponding attribute or by not calling a SetFill function.  In

      // this case, we don't worry about it either.  But we do allow m_size

      // to have a value different from the (zero) m_dataSize.

      //

      p = Create<Packet> (m_size);

      p->AddHeader (sourceHeader);

      p->Print (std::cout);

      std::cout << std::endl;

    }

  // call to the trace sinks before the packet is actually sent,

  // so that tags added to the packet can be sent as well

  m_txTrace (p);

  m_socket->Send (p);


  ++m_sent;


  if (Ipv4Address::IsMatchingType (m_peerAddress))

    {

      NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s requestor sent " << m_size << " bytes to " <<

                   Ipv4Address::ConvertFrom (m_peerAddress) << " port " << m_peerPort);

    }

    }

  if (m_sent < m_count) 

    {

      ScheduleTransmit (m_interval);

    }

}


When Receive:

void

AppParticipant::HandleRead (Ptr<Socket> socket)

{

  NS_LOG_FUNCTION (this << socket);


  Ptr<Packet> packet= Create<Packet> (m_size);

    Address from;

    //Ptr<Packet> packet = socket->RecvFrom (from);

  while ((packet = socket->RecvFrom (from)))

    {

      if (InetSocketAddress::IsMatchingType (from))

        {

            MyHeader destinationheader;

            packet->RemoveHeader (destinationheader);

            destinationheader.Print(std::cout);

            std::cout << std::endl;


            uint16_t data= destinationheader.GetData ();

            NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s participant received " << packet->GetSize () << " bytes from " <<

                       InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<

                         InetSocketAddress::ConvertFrom (from).GetPort () << " as " <<  std::cout << data << std::endl);

        }


      packet->RemoveAllPacketTags ();

      packet->RemoveAllByteTags ();


      NS_LOG_LOGIC ("Responding");

      socket->SendTo (packet, 0, from);


      if (InetSocketAddress::IsMatchingType (from))

        {

          NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s participant sent " << packet->GetSize () << " bytes to " <<

                       InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<

                       InetSocketAddress::ConvertFrom (from).GetPort ());

        }

    }

}


There is :

Ptr<Packet> packet= Create<Packet> (m_size); in the receiver side to read the received packet. Is this the source of error?
How to fix it.

Please tell me if you need to attach the whole files.

Regards,

Konstantinos

unread,
Jan 28, 2016, 6:23:46 AM1/28/16
to ns-3-users
Hi,

Indeed you should not create a new packet. Just the pointer is sufficient.
Ptr<Packet> packet;
This will be initialized with the packet=socket->RecvFrom(from).

Also in the NS_LOG_INFO you should not add the std::cout. 

RBM-IT

unread,
Jan 28, 2016, 6:26:54 AM1/28/16
to ns-3-users
Hi,

Thank you It works fine now :)

Regards,
Reply all
Reply to author
Forward
0 new messages