Reputation: 1
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.
Upvotes: 0
Views: 151
Reputation: 7002
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.
Upvotes: 0