[英]Socket Creation Compile Error using Winsock2 in c++
編輯:
#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?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.