gpt4 book ai didi

c++ - 在C++中使用Winsock2进行套接字创建编译错误

转载 作者:行者123 更新时间:2023-12-02 11:02:35 25 4
gpt4 key购买 nike

编辑:

#undef UNICODE

#define WIN32_LEAN_AND_MEAN

#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iostream>
#include <string>
#include <thread>

#pragma comment (lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "1016"

//Globals
struct addrinfo *result = NULL;
struct addrinfo hints;

//Prototypes
int listenFunction();
int acceptFunction(SOCKET ListenSocket);

我最近开始重新编码无法正常工作的tchat服务器,以编写更清晰的代码,但遇到一个令我困惑的错误。我知道如何解决此问题,但我想知道为什么这样做。
所以基本上,我有两个功能,主要功能是:
int main()
{
int iResult;
std::cout << "At the begginning of main." << std::endl;
WSADATA wsaData;

//Initialize winsock
std::cout << "Initializing winsock..." << std::endl;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
std::cout << "WSAStartup failed." << std::endl;
return 1;
}
std::cout << "Sucess" << std::endl;

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

std::cout << "Going into listenFunction()" << std::endl;
listenFunction();

return 0;
}

和我的listenFunction:
int listenFunction()
{
int iResult;
std::cout << "In Listening function" << std::endl;

SOCKET ListenSocket = INVALID_SOCKET;

std::cout << "Calling addrinfo()" << std::endl;
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0){
std::cout << "getaddrinfo() failed with error code : " << std::endl;
WSACleanup();
return -1;
}

//Create a SOCKET for connecting to the server
std::cout << "Creating ListenSocket..." << std::endl;
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
std::cout << "Socket failed with error : " << WSAGetLastError() << std::endl;
freeaddrinfo(result);
WSACleanup();
return -1;
}

//Maybe setsockopt function here if bug

//Setup the TCP listening socket
std::cout << "Setting up the TCP listening socket..." << std::endl;
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
std::cout << "Bind failed with error : " << WSAGetLastError() << std::endl;
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return -1;
}

freeaddrinfo(result);

std::cout << "Listening on ListenSocket..." << std::endl;
//Listening
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR){
std::cout << "Listen failed with error : " << WSAGetLastError() << std::endl;
closesocket(ListenSocket);
WSACleanup();
return -1;
}

std::thread AcceptThread{ acceptFunction(ListenSocket) };

return 0;
}

基本上,当我在listenFunction中声明套接字时,出现此错误:
Error   1   error C2064: term does not evaluate to a function taking 0 arguments

仿佛我复制了相同的行并将其粘贴到main函数中一样,我不再收到该错误(当然,由于未声明它,所以我在listenFunction中得到了所有错误)。无论如何,有没有解决这个问题而不是将其传递给参数?

任何帮助是极大的赞赏,

心智

最佳答案

您列出的错误不是由创建套接字引起的,而是由以下行引起的:
std::thread AcceptThread{ acceptFunction(ListenSocket) };
我不确定您要在此处做什么,因为我不知道如何使用花括号初始化std::thread。也许尝试Lambda?

关于c++ - 在C++中使用Winsock2进行套接字创建编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com