2014-09-22 71 views
0

我正在实现一个简单的多线程FTP客户端服务器,我面临的问题对我来说很奇怪(因为我不是C++和线程的主人)。当包含线程时,监听WINSOCK错误10022

我写的代码通常工作,直到我#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但我不认为这有解决我的问题。

+0

哪里#include行? – 2014-09-22 04:08:09

+0

包括在内:) – user3275095 2014-09-22 04:14:12

回答

3

错误10022是WSAEINVAL。该documentationlisten()明确规定:

WSAEINVAL
套接字尚未绑定与绑定。

当你添加#include <thread>代码停止工作的原因是因为你对bind()呼叫正在改变,不再叫的WinSock的bind()功能,而是改为调用STL的​​功能。你的using namespace std声明掩盖了这个问题(这是为什么using namespace std是这样的一个不好的练习的许多原因之一 - 教你自己停止使用它!)。

所以,你需要:

  1. 摆脱using namespace std

  2. 资格bind()到全局命名空间,因此调用的WinSock的功能:

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

谢谢雷米。它现在有效。 :) – user3275095 2014-09-22 04:36:23