Funny thing with WSAStartup()
Hi there!
I have a class named mySocket which is implemented as follow:
////////////////////////////////////////////////////////////////////////////////////////////////////
class mySocket{
private:
WSADATA wsadata;
// ...
public:
////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn int listen(int nPort)
///
/// @brief Create a socket listening on a specified port
///
/// @param nPort The port number
///
/// @return If success, returns 1. Otherwise, return -1.
////////////////////////////////////////////////////////////////////////////////////////////////////
int listen(int nPort);
// ...
}
int mySocket::listen(int nPort){
int iResult=WSAStartup(MAKEWORD(2,2), &wsadata);
if(iResult!=0){
printf("WSAStartup failsed: %d \n",iResult);
WSACleanup();
return -1;
};
// ...
}
////////////////////////////////////////////////////////////////////////////////////////////////////
In the code above, you can see that wsadata is declared as a property of mySocket. My problem is: WSAStartup() always fails with an error code of 10014 returned. :( But if I move the declaration of wsadata into the method listen(), there's nothing wrong happen. :)
I've had no idea about the error. Can somebody explain to me why?
Re: Funny thing with WSAStartup()
The problem might be caused by use of the MAKEWORD() macro in the argument list of the function call.
Try this instead:
Code:
int mySocket::listen(int nPort){
WORD wVersionRequested = MAKEWORD(2,2);
int iResult=WSAStartup(wVersionRequested , &wsadata);
if(iResult!=0){
printf("WSAStartup failsed: %d \n",iResult);
WSACleanup();
return -1;
};