(WINSOCK) Confusion with printing Errors - WSAGetLastError or iResult?

This is a discussion on (WINSOCK) Confusion with printing Errors - WSAGetLastError or iResult? within the Windows Programming forums, part of the Platform Specific Boards category; Hello, noobie here. Currently learning winsock programming from MSDN Microsoft pages. In their example, they use the following: Code: // ...

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    3

    (WINSOCK) Confusion with printing Errors - WSAGetLastError or iResult?

    Hello, noobie here.

    Currently learning winsock programming from MSDN Microsoft pages. In their example, they use the following:

    Code:
    // Resolve the local address and port to be used by the server
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    and

    Code:
        iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            printf("bind failed with error: %d\n", WSAGetLastError());
            freeaddrinfo(result);
            closesocket(ListenSocket);
            WSACleanup();
            return 1;
        }
    My problem here is, in the first code, the program prints "iResult" if there was to be an error whereas the second code prints WSAGetLastError(). Why is it like this? Also when is it appropriate to do one over the other?

    Complete Source Code found here:
    Complete Winsock Server Code (Windows)

    Kind regards.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    If in doubt, check the meaning of the return value in MSDN.

    getaddrinfo() returns WSAGetLastError() (so you dont have to call it) but bind() only returns success / fail.

    Why are they different?
    Remember this is Microsoft, where there is ERROR_SUCCESS (0)

    Quote Originally Posted by MSDN
    Bind()
    If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

    getaddrinfo()
    Success returns zero. Failure returns a nonzero Windows Sockets error code, as found in the Windows Sockets Error Codes.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WSAGetLastError() is returning 0
    By 39ster in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-16-2008, 06:57 AM
  2. WSAGetLastError returns 0
    By dandysxm in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2007, 07:23 AM
  3. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  4. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  5. Winsock confusion
    By Mithoric in forum Windows Programming
    Replies: 3
    Last Post: 01-21-2004, 08:03 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21