C++明确使用来自Omnet中随附的HeaderFile的函数++
我有一个疑问,请使用我包含在Omnet ++类“ Computer”中的“发送”函数(在HeaderFile winsock2.h中定义)。 找不到该功能,因为编译器似乎仅在类/模块“计算机”中搜索它。
#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)
{
我收到错误:呼叫“计算机:: send(socket&amp; const char*,std :: __ cxx11 :: basic_string :: size_type,int)'
仅'仅带有“发送”相关的最后一部分。其余的工作正常。
当然,“发送”不是“计算机”的函数,而是winsock2.h的函数。 有人知道我如何在代码中澄清这一点吗?
问题在计算机中
#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
。 ( htttps> a>)
最好的问候, 卢卡斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎这是一个范围的问题,其中
Computer
中的成员函数隐藏了Winsock中的 send 函数。可以使用::
的显式示波器分辨率来解决这一问题。因此,要召集班级并达到全局功能,您可以使用
:: send(parameters)
。Seems like it was a scoping problem, where a member function in
Computer
hid the globalsend
function from Winsock. This can be resolved by an explicit scope resolution using::
.So, to call out of the class, and reach a global function, you use
::send(parameters)
.