简体   繁体   English

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

[英]Socket Creation Compile Error using Winsock2 in c++

EDIT : 编辑:

#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);

I recently started recoding a tchat server that I never got to work in order to make a cleaner code but I ran in to an error that kinda confused me. 我最近开始重新编码无法正常工作的tchat服务器,以编写更清晰的代码,但遇到一个令我感到困惑的错误。 I know how to fix this but I would like to know why it is doing this. 我知道如何解决此问题,但我想知道为什么要这样做。 So basically, I have two functions, my main function : 所以基本上,我有两个功能,主要功能是:

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;
}

And my listenFunction : 和我的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;
}

Bsically, when I declare the socket in the listenFunction, I get this error : 基本上,当我在listenFunction中声明套接字时,出现此错误:

Error   1   error C2064: term does not evaluate to a function taking 0 arguments

Where as if I copy that same exact line and paste it in the main function, I don't get that error anymore (of course I get all the errors in the listenFunction because its not declared). 仿佛我复制了同一行并将其粘贴到main函数中一样,我不再收到该错误(当然,由于未声明它,所以我在listenFunction中得到了所有错误)。 Is there anyway to fix this rather than passing it in an argument ? 无论如何,有没有解决这个问题而不是将其传递给参数?

Any help is greatly appreciated, 任何帮助是极大的赞赏,

mindoo 心智

The error you list is not caused by creating the socket, its caused by the line: 您列出的错误不是由创建套接字引起的,而是由以下行引起的:

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

I'm not sure what you are trying to do there, as I am unfamiliar with how curly braces would be used to initialize a std::thread . 我不确定您要在其中做什么,因为我不知道如何使用花括号初始化std::thread Maybe an attempt at a Lambda? 也许尝试Lambda?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM