简体   繁体   English

具有Winsock和std :: thread的C ++多线程服务器

[英]C++ multithreaded server with winsock and std::thread

I have some problem with including thread module, when I add: 添加时,在包含thread模块时遇到一些问题:

#include <thread>

the call to the bind function gives me error: 绑定函数的调用给我错误:

Error 3 error C2440: '=' : cannot convert from 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>' to 'int' c:\\users\\ohadpeled\\documents\\visual studio 2012\\projects\\loginserver\\loginserver\\server.cpp 87 1 LoginServer 4 IntelliSense: no suitable conversion function from "std::_Bind" to "int" exists c:\\Users\\OhadPeled\\Documents\\Visual Studio 2012\\Projects\\LoginServer\\LoginServer\\Server.cpp 87 20 LoginServer 错误3错误C2440:'=“ \\ visual studio 2012 \\ projects \\ loginserver \\ loginserver \\ server.cpp 87 1 LoginServer 4 IntelliSense:不存在从“ std :: _ Bind”到“ int”的合适转换函数c:\\ Users \\ OhadPeled \\ Documents \\ Visual Studio 2012 \\ Projects \\ LoginServer \\ LoginServer \\ Server.cpp 87 20 LoginServer

I don't understand why it cause this error, without including the thread module the call works fine. 我不明白为什么它会导致此错误,而没有包含线程模块,则调用效果很好。 I'd be happy if someone will explain me what cause it. 如果有人能解释我的原因,我将非常高兴。

Here is part of the server class: 这是服务器类的一部分:

            /* Set TCP listening socket */
            ListenResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); // Error in this line
            if (ListenResult != SOCKET_ERROR) 
            {
                freeaddrinfo(result);

                ListenResult = listen(ListenSocket, SOMAXCONN);
                if (ListenResult != SOCKET_ERROR) 
                {
                    /* Accepting clients */
                    while(true)
                    {
                        ClientSocket = new SOCKET();
                        ADDR = new SOCKADDR_IN();
                        ADDRSize = sizeof(*ADDR);
                        *ClientSocket = accept(ListenSocket, (struct sockaddr*)ADDR, &ADDRSize);
                        if (*ClientSocket != INVALID_SOCKET) 
                        {
                            /* I want to thread the handler function over here! */
                            Handler(ClientSocket, ADDR);
                        }
                    }
                }
            }

I'm using win7, and set the socket with winsock. 我正在使用win7,并使用winsock设置套接字。

The problem is that the compiler is resolving the bind symbol to the C++ function std::bind() instead of the WinSock function bind() . 问题在于,编译器将bind符号解析为C ++函数std::bind()而不是WinSock函数bind() In order to fix this, you can do one of two things: 为了解决此问题,您可以执行以下两项操作之一:

  1. Remove all using namespace std; using namespace std;删除所有内容using namespace std; declarations in your source file; 源文件中的声明; or 要么
  2. Use the scope resolution operator :: to explicitly refer to the bind function in the global namespace: 使用范围解析运算符::显式引用全局名称空间中的bind函数:

     ListenResult = ::bind(...); 

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

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