编辑:
#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中的所有错误,因为它没有声明)。有没有办法解决这个问题,而不是在参数中传递它?
任何帮助都是非常感谢的,
民宿
发布于 2015-10-06 02:13:54
您列出的错误不是由创建套接字引起的,而是由以下代码行引起的:
std::thread AcceptThread{ acceptFunction(ListenSocket) };
我不知道你在做什么,因为我不熟悉如何用大括号来初始化std::thread。也许是对Lambda的一种尝试?
https://stackoverflow.com/questions/32954597
复制相似问题