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

  1. #1
    Registered User
    Join Date
    Feb 2014

    (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:

    // 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);
        return 1;

        iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            printf("bind failed with error: %d\n", WSAGetLastError());
            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
    near a computer
    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
    If no error occurs, bind returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

    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, 08: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, 09:03 PM

Tags for this Thread