How can I send a packet between client and server with TCP protocol in OMNeT++?

232 views
Skip to first unread message

ali mansoori

unread,
May 31, 2021, 11:51:03 AM5/31/21
to OMNeT++ Users

I am looking for the initial implementation of the TCP / IP protocol in OMNeT++ software. The problem I encountered is that when the connection between computer1 and computer2 is established, and computer1 acts as a client and wants to send a packet named volt, OMNeT++ displays the message

"No more events — simulation ended at event # 52, t = 0.5.".

Thanks for guiding me to solving this problem. It is noteworthy that the code is attached at the end of this text. We also used OMNeT++ version 4.4.1 with INET 2.5.0.

computer1 (client) code:

#include <string.h>
#include <omnetpp.h>
#include "TCPSocket.h"
#include "IPvXAddressResolver.h"
#include "UDPControlInfo_m.h"
#include "Alireza_m.h"
using namespace std;
class COMP1: public cSimpleModule
{
public:
    const char * connectAddress;
    int          connectPort;
    TCPSocket    tcpsocket;
protected:


    int                      localPort, destPort;
    std::vector<IPvXAddress> destAddresses;
    virtual ~COMP1();
    virtual int numInitStages() const
    {
        return 4;
    }
    virtual void initialize(int stage);
    virtual void handleMessage(cMessage *msg);

};


Define_Module(COMP1);

void COMP1::initialize(int stage)
{

    if (stage != 3)
    {
        return;
    }

    Alireza * pk=new Alireza("volt");
    pk -> setVoltX(5);

    tcpsocket.setOutputGate(gate("tcpOut"));
    tcpsocket.readDataTransferModePar(*this);
    tcpsocket.setDataTransferMode(TCP_TRANSFER_OBJECT);
    tcpsocket.bind(IPvXAddressResolver().resolve("Computer1"), 1001);
    IPvXAddress destination=IPvXAddressResolver().resolve("Computer2");
    connectPort=1002;
    tcpsocket.connect(destination, connectPort);

    scheduleAt(simTime() +0.5, pk);
}

void COMP1::handleMessage(cMessage *msg)
{
    ev << " I got a message Name=" << msg -> getName() << endl;
    EV << " clienttttttttttt como111111111 received message  \n " ;
    IPvXAddress destAddr = IPvXAddressResolver().resolve(par("destAddresses"));
    Alireza * pk=new Alireza("volt");
    pk -> setVoltX(1);
    tcpsocket.send(pk);

    }
COMP1::~COMP1()
{
}

---------------------------------------------------------------------------------------------------------------------------------------------

computer2 (Server) code:

#include <string.h>
#include <omnetpp.h>
#include "TCPSocket.h"
#include "UDPSocket.h"
#include "IPvXAddressResolver.h"
#include "UDPControlInfo_m.h"
#include "Alireza_m.h"
using namespace std;
class COMP2: public cSimpleModule
{
public:
    const char * connectAddress;
    int          connectPort;
    TCPSocket    tcpsocket;
protected:

    int                      localPort, destPort;
    std::vector<IPvXAddress> destAddresses;

    virtual ~COMP2();

    virtual int numInitStages() const
    {
        return 4;
    }
    virtual void initialize(int stage);
    virtual void handleMessage(cMessage *msg);

};


Define_Module(COMP2);

void COMP2::initialize(int stage)
{

    if (stage != 3)
    {
        return;
    }

    destPort  = par("destPort");
    localPort = par("localPort");

    tcpsocket.setOutputGate(gate("tcpOut"));
    tcpsocket.setDataTransferMode(TCP_TRANSFER_OBJECT);
    tcpsocket.bind(IPvXAddressResolver().resolve("Computer2"), 1002);
    tcpsocket.listen();
}

void COMP2::handleMessage(cMessage *msg)
{

    ev << " I got a message Name=" << msg -> getName() << endl;
    EV << " serverrrrrrrr comp22222222 received message \n" ;\

}
COMP2::~COMP2()
{
}





Alfonso Ariza Quintana

unread,
May 31, 2021, 2:11:08 PM5/31/21
to omn...@googlegroups.com
    You need to program the next event



class COMP1: public cSimpleModule
{
public:
    const char * connectAddress;
    int          connectPort;
    TCPSocket    tcpsocket;
protected:
    cMessage *timer;

......


void COMP1::initialize(int stage)
{

    if (stage != 3)
    {
        return;
    }

     timer = new cMessage("Timer");


    tcpsocket.setOutputGate(gate("tcpOut"));
    tcpsocket.readDataTransferModePar(*this);
    tcpsocket.setDataTransferMode(TCP_TRANSFER_OBJECT);
    tcpsocket.bind(IPvXAddressResolver().resolve("Computer1"), 1001);
    IPvXAddress destination=IPvXAddressResolver().resolve("Computer2");
    connectPort=1002;
    tcpsocket.connect(destination, connectPort);

    scheduleAt(simTime() +0.5, timer);
}


void COMP1::handleMessage(cMessage *msg)
{
    if (msg->isSelfMessage()  && msg == timer) {
        EV << "Send next frame" << endl;
          IPvXAddress destAddr = IPvXAddressResolver().resolve(par("destAddresses"));
         Alireza * pk=new Alireza("volt");
          pk -> setVoltX(1);
          tcpsocket.send(pk);
          scheduleAt(simTime() +0.5, pk);
    }
    else {
      // received packet
      delete msg;
   }
}


De: omn...@googlegroups.com <omn...@googlegroups.com> en nombre de ali mansoori <mansoori...@gmail.com>
Enviado: lunes, 31 de mayo de 2021 17:51
Para: OMNeT++ Users <omn...@googlegroups.com>
Asunto: [Omnetpp-l] How can I send a packet between client and server with TCP protocol in OMNeT++?
 
--
You received this message because you are subscribed to the Google Groups "OMNeT++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to omnetpp+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/omnetpp/05e01e78-cc17-4b02-be0a-998488c4a9e6n%40googlegroups.com.

ali mansoori

unread,
Jun 1, 2021, 1:25:47 AM6/1/21
to OMNeT++ Users
Thank you for your attention Dear Alfonso

but when i implemented your suggestion, an error with title:
"Error in module (COMP1) NET2.Computer1.comp1 (id=12) at event #49, t=0.5: scheduleAt(): cannot schedule message (cMessage)Timer, it is currently in scheduled-events, being underway between two modules."occured.

What do you suggest to solve this problem? 

Alfonso Ariza Quintana

unread,
Jun 1, 2021, 6:56:03 AM6/1/21
to omn...@googlegroups.com

          scheduleAt(simTime() +0.5, timer);

 

It must be the timer that is scheduled

 

Enviado desde Correo para Windows 10

ali mansoori

unread,
Jun 10, 2021, 4:22:28 AM6/10/21
to OMNeT++ Users

thank you Dear Alfonso.

 I implemented scheduleAt() for timer in handleMessage() but it gave the same error message as before,i.e " "Error in module (COMP1) NET2.Computer1.comp1 (id=12) at event #49, t=0.5: scheduleAt(): cannot schedule message (cMessage)Timer, it is currently in scheduled-events, being underway between two modules."

 What solution do you suggest to enable scheduleAt() for packets such as timer and alireza, so that after communication stablishment, it can be sent to Computer2 via tcp gate?

Alfonso Ariza Quintana

unread,
Jun 10, 2021, 5:28:07 AM6/10/21
to omn...@googlegroups.com
With this information is not easy, but I suspect that you have sent the timer, or, you try to re-schedule the timer before the end of the timer.

Enviado: jueves, 10 de junio de 2021 10:22
Para: OMNeT++ Users <omn...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages