2

within an endless loop function in C++/omnet++ I try to initialize a socket object 'sock' and afterwards I want to retrieve the incoming data in my endless loop. This would be easy of course in normal C++ code...But well we're within omnet++ environment and I can't initialize and afterwards run an endless loop within a main function. I only have one endless loop "Network_func". The problem is the compiler says that " 'sock' is not declared in this scope" when I want to call it in a "recv" function outside the initializing if-statement within the mentioned endless loop.

(int bytesReceived = recv(sock,buf,4096,0);).

Here's my code:

void VeinsInetSampleApplication::Network_func()
{
static bool runOnlyOnce = false;

if (!runOnlyOnce) {
string ipAddress = "127.0.0.1";  //IP Address of the server
int port = 59006;                //Listening port # on the server

//Initialize WinSock
WSAData data;
WORD ver = MAKEWORD(2, 2);
int wsResult = WSAStartup(ver, &data);
if (wsResult != 0)
{
     cerr << "Can't start winsock, Err #" << wsResult << endl;
     //return 0;
}

//Create Socket
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
     cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
     WSACleanup();

}
// Fill in a hint structure
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(port);
//inet_pton(AF_INET, ipAddress.c_str(), &hint.sin_addr);
hint.sin_addr.s_addr = inet_addr("127.0.0.1");

// Connect to server
int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
if (connResult == SOCKET_ERROR)
{
   cerr << "Can't connect to server, Err #" << WSAGetLastError() << endl;
   closesocket(sock);
   WSACleanup();

}

runOnlyOnce = true;
}


//Do-while loop to send and receive data
  char buf[4096];
  string userInput;

  ZeroMemory(buf, 4096);
  int bytesReceived = recv(sock,buf,4096,0);
  if (bytesReceived > 0)
  {
      //...do something with received data...
  }


}

Limitation seems to be that I don't have access to the main function in omnet++, as it is part of oppenvir library (see: The main function in Omnet++) thus I think separating the socket initialization and afterwards running some sort of endless loop for data handling does not work. If someone is an expert in omnet++ please correct me but I think everything has to be handled in my endless loop function.

In summary: Is there a possiblity to make 'sock' available to the whole function? (Of course I know that if the if-statement was not called, 'sock' would not be declared, but that won't happen. If I don't connect within the if-statement I of course get the error message "can't connect" for every iteration of the loop).

veins_inet/VeinsInetSampleApplication.cc:136:32: error: 'sock' was not declared in this scope int bytesReceived = recv(sock,buf,4096,0);

EDIT: Some background: What might be confusing when reading the question. Omnet++ is a network simulator set up in Eclipse. My code snippet shall NOT set up a TCP connection within the network simulator but provide a "live" connection to an external TCP Server. In summary, I want to connect to the external world from within the network simulator.

  • Reading the title, I wonder if the questions is specific to omnet++ or even sockets. – Ulrich Eckhardt May 29 '22 at 11:19
  • @UlrichEckhardt: tried to provide some background in "EDIT" – user18944383 May 29 '22 at 11:24
  • That's nice, but my guess is that it's irrelevant. C++ doesn't suddenly treat an object differently just because it's a socket or because it's somehow interacting with OMNeT++. – Ulrich Eckhardt May 29 '22 at 11:27
  • Yes, of course C++ rules apply. But as I'm within an endless loop I cannot do within a main function: 1. Initialization (connect to socket) 2. within wile(true) loop: handle messages --> this would work fine. Using my code I can perfectly connect to my tcp server from within eclipse/c++ without omnet++ – user18944383 May 29 '22 at 11:35
  • @AlexF: I know, that this is the problem...but how can I connect to the socket ONLY ONCE in an endless loop but still see "socket" within my endless loop. IfI take it out of the if-statement this error disappears but the new error is that I permanently try to connect to the server... – user18944383 May 29 '22 at 12:34

1 Answers1

0

First of all, one mustn't create an endless loop in any of OMNeT++ method. It is against the idea of how OMNeT++ works. An endless loop prevents processing events.
Second, if your intention is to send a message from a simple module via the real network interface, I suggest looking at Network Emulation.

Jerzy D.
  • 6,707
  • 2
  • 16
  • 22
  • thank you, but seems to be difficult...on windows. My effort trying direct function programming within VeinsInetSampleApplication or a module class resulted from an answer I got below a ytb tutorial stating: So you would need to make a custom node that would be part of your hosts (in OMNET++) that would create a connection to your python server and then communicate to it similarly. When connected they can then listen in to receive events from the server.(https://www.youtube.com/watch?v=7MLPVfPqFc8&list=PLaBPUIXZ8s4AwAk5EelikvvyG4EzX2hpx&index=9) Is that possible? – user18944383 May 29 '22 at 18:59