【问题标题】:WINSOCK error 10022 on listen when include thread包含线程时侦听 WINSOCK 错误 10022
【发布时间】:2014-09-22 04:04:15
【问题描述】:

我正在实现一个简单的多线程 FTP 客户端服务器,我遇到了一个对我来说很奇怪的问题(因为我不是 C++ 和线程方面的大师)。

我写的代码在我#include <thread>之前正常工作。

一旦我包含线程类,程序在侦听时失败并给出 10022 错误。 (我还没有做任何与线程相关的事情,只导入)。

下面是代码。该方法是从 main() 调用的。

#include <winsock2.h>
#include <ws2tcpip.h>
#include <process.h>
#include <winsock.h>
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <thread>

using namespace std;

void initializeSockets()
    {
    try{
        logEvents("SERVER", "Initializing the server");
        WSADATA wsadata;
        if (WSAStartup(0x0202,&wsadata)!=0){
            cout<<"Error in starting WSAStartup()\n";
            logEvents("SERVER", "Error in starting WSAStartup()");
        }else{

            logEvents("SERVER", "WSAStartup was suuccessful");
        }

        gethostname(localhost,20);
        cout<<"hostname: "<<localhost<< endl;
        if((hp=gethostbyname(localhost)) == NULL) {
            cout << "gethostbyname() cannot get local host info?"
                << WSAGetLastError() << endl;
            logEvents("SERVER", "Cannot get local host info. Exiting....");
            exit(1);
        }

        //Create the server socket
        if((serverSocket = socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET) 
            throw "can't initialize socket";

        //Fill-in Server Port and Address info.
        serverSocketAddr.sin_family = AF_INET;
        serverSocketAddr.sin_port = htons(port);
        serverSocketAddr.sin_addr.s_addr = htonl(INADDR_ANY);

        //Bind the server port
        if (bind(serverSocket,(LPSOCKADDR)&serverSocketAddr,sizeof(serverSocketAddr)) == SOCKET_ERROR)
            throw "can't bind the socket";
        cout << "Bind was successful" << endl;
        logEvents("SERVER", "Socket bound successfully.");

        if(listen(serverSocket,10) == SOCKET_ERROR)
            throw "couldn't set up listen on socket";
        else 
            cout << "Listen was successful" << endl;
        logEvents("SERVER", "Socket now listening...");
        //Connection request accepted.
        acceptUserConnections();
    }

    catch(char* desc)
    { 
        cerr<<str<<WSAGetLastError()<<endl;
        logEvents("SERVER", desc);
    }

    logEvents("SERVER", "Closing client socket...");
    closesocket(clientSocket);
    logEvents("SERVER", "Closed. \n Closing server socket...");
    closesocket(serverSocket);
    logEvents("SERVER", "Closed. Performing cleanup...");

    WSACleanup();

}

int main(void){

    initializeSockets();
    return 0;
}

我已阅读线程Winsock Error 10022 on Listen,但我认为这不能解决我的问题。

【问题讨论】:

  • #include 行在哪里?
  • 包括了 :)

标签: sockets visual-c++ ftp winsock winsock2


【解决方案1】:

错误 10022 是 WSAEINVALdocumentation for listen() 明确指出:

WSAEINVAL
套接字尚未与 bind 绑定。

添加#include &lt;thread&gt; 时代码停止工作的原因是因为您对bind() 的调用被更改为不再调用WinSock 的bind() 函数,而是调用STL 的std::bind() 函数。你的using namespace std 声明掩盖了这个问题(这是using namespace std 如此糟糕做法的众多原因之一 - 教自己停止使用它!)。

所以你需要:

  1. 摆脱using namespace std

  2. 使用全局命名空间限定bind(),以便调用 WinSock 的函数:

    if (::bind(...) == SOCKET_ERROR)
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 2013-06-10
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多