CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2008
    Posts
    5

    Unhappy 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?
    Last edited by anhldbk; September 23rd, 2010 at 02:02 AM.

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured