I have a question about using the function "send" (which is defined in the headerfile winsock2.h) I included in by omnet++ class "Computer". The function is not found because the compiler seems to only search for it in the class/module "Computer".
#include "Computer.h"
#include <iostream>
#include <string>
#include <winsock2.h>
#include <ws2tcpip.h>
#define NS_INADDRSZ 4
#define NS_IN6ADDRSZ 16
#define NS_INT16SZ 2
#pragma comment(lib, "ws2_32.lib")
using namespace std;
Define_Module(Computer);
void Computer::initialize()
{
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;
}
//Create Socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
cerr << "Can't create socket, Err #" << WSAGetLastError() << endl;
WSACleanup();
return;
}
// 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();
return;
}
//Do-while loop to send and receive data
char buf[4096];
string userInput;
do
{
// Prompt the usedr for some text
cout << "> ";
getline(cin, userInput);
if (userInput.size() > 0) // Make sure the user has typed in something
{
// Send the text
int sendResult = send(sock, userInput.c_str(), userInput.size() + 1, 0);
if (sendResult != SOCKET_ERROR)
{
I get the error: no matching function for call to 'Computer::send(SOCKET&, const char*, std::__cxx11::basic_string::size_type, int)'
Only the last part with "send" is relevant. The rest works fine.
Of course, "send" is not a function of "Computer" but of winsock2.h. Does anyone know how I can clarify this in my code?
Problem is in Computer.h, Computer is a "cSimpleModule:
#ifndef __TCP_COMPUTER_H_
#define __TCP_COMPUTER_H_
#include <omnetpp.h>
using namespace omnetpp;
/**
* TODO - Generated class
*/
class Computer : public cSimpleModule
{
protected:
virtual void initialize();
virtual void handleMessage(cMessage *msg);
};
#endif
And cSimpleModule class has its own function "send". (https://doc.omnetpp.org/omnetpp/api/classomnetpp_1_1cSimpleModule.html)
Best regards, Lukas
Computer
have its ownsend
method? This question would benefit greatly from being turned into a minimal reproducible example--both minimal by removing all the stuff that is presumably working fine and not relevant to your question, and reproducible by giving people enough information to see what's going on.::send(parameters)
.