kwkwkw
kwkwkw

Reputation: 76

How to send custom packets in omnet++?

Let's say i created my own packet called myPacket. Is there a way i can send it using socket.sendTo()?

I know socket.sendTo() takes in an INET packet so is there a way to convert myPacket into an INET packet?

The module that is going to receive the packet is Radio. I checked Radio's functions and they take in an inet packet so what can i do about it?

Signal *Radio::createSignal(Packet *packet) const
{
    encapsulate(packet);
    if (sendRawBytes) {
        auto rawPacket = new Packet(packet->getName(), packet->peekAllAsBytes());
        rawPacket->copyTags(*packet);
        delete packet;
        packet = rawPacket;
    }
    Signal *signal = check_and_cast<Signal *>(medium->transmitPacket(this, packet));
    ASSERT(signal->getDuration() != 0);
    return signal;
}

Upvotes: 3

Views: 752

Answers (3)

gehirndienst
gehirndienst

Reputation: 463

You need to do 4 steps:

  1. Define your own .msg class and extend some of the inet predefined classes. See inet/applications/base/ApplicationPacket.msg as an example.
  2. Define your communication protocol aka inet socket object to pass the messages. Look at this guide. Don't forget to pass destination address and port, normally they are defined as .NED parameters and injected through the omnetpp.ini file.
  3. Then you need to write a method, which builds your packet and sends it to the destination address. Take a look at the method UdpBasicApp::sendPacket() at inet/applications/udpapp/UdpBasicApp.cc as an example.
  4. At receiver side I usually have a bunch of processing methods switched in handleMessage method or similar one to catch all possible messages my receiver can receive and process. All such methods take cMessage* msg as an argument and then at the beginning:
Packet* packet = check_and_cast<Packet*>(msg);
if (!packet) {
    return;
}
const auto& payload = packet->peekAtFront<YourOwnPacketClass>();
// work with you message body...

Upvotes: 0

imtithal
imtithal

Reputation: 174

Basically, messages sent by using the cSimpleModule basic member function send(). This method is used to send messages to other modules through gates. One can also use the scheduleAt() to send a message at specific point in time.

If you use a higher level application such as http or tcp applications, so you are most probably going to use sockets. Sockets also use send() and scheduleAt() to send messages through gates.

Upvotes: 0

imtithal
imtithal

Reputation: 174

Sending messages using sockets needs a socket on the other side. If you have a socket at the other side so go ahead and send your message using a socket.

Upvotes: 0

Related Questions