Winsock Server And Client Example: "getaddrinfo" was not declared in this scope
<p>I am trying out networking in C++ with a simple server and client, starting with some Winsock examples.</p>

<p>Whenever I try to compile the server with Code::Blocks, I get the following errors:</p>

<pre><code>||=== Build: Debug in Server Test (compiler: GNU GCC Compiler) ===|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|12|warning: ignoring #pragma comment  [-Wunknown-pragmas]|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp||In function 'int main()':|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|47|error: 'getaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|57|warning: format '%ld' expects argument of type 'long int', but argument 2 has type 'int' [-Wformat]|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|58|error: 'freeaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|67|error: 'freeaddrinfo' was not declared in this scope|
C:\Users\****\Desktop\Programming\CodeBlocks Projects\Server Test\main.cpp|73|error: 'freeaddrinfo' was not declared in this scope|
||=== Build failed: 4 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
</code></pre>

<p>I have tried the solution from <a href="https://stackoverflow.com/questions/4243027/">this question</a>, however, this results in 36 errors.</p>

<p>I am having the same issue with the client code.</p>

<p>Server Code:</p>

<pre><code>#undef UNICODE

#define WIN32_LEAN_AND_MEAN

#include &lt;windows.h&gt;
#include &lt;winsock2.h&gt;
#include &lt;ws2tcpip.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;

// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(void)
{
    WSADATA wsaData;
    int iResult;

    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;

    struct addrinfo *result = NULL;
    struct addrinfo hints;

    int iSendResult;
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }

    ZeroMemory(&amp;hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve the server address and port
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Create a SOCKET for connecting to server
    ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
    if (ListenSocket == INVALID_SOCKET) {
        printf("socket failed with error: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Setup the TCP listening socket
    iResult = bind( ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("bind failed with error: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // Accept a client socket
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        printf("accept failed with error: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // No longer need server socket
    closesocket(ListenSocket);

    // Receive until the peer shuts down the connection
    do {

        iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
        if (iResult &gt; 0) {
            printf("Bytes received: %d\n", iResult);

        // Echo the buffer back to the sender
            iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
            if (iSendResult == SOCKET_ERROR) {
                printf("send failed with error: %d\n", WSAGetLastError());
                closesocket(ClientSocket);
                WSACleanup();
                return 1;
            }
            printf("Bytes sent: %d\n", iSendResult);
        }
        else if (iResult == 0)
            printf("Connection closing...\n");
        else  {
            printf("recv failed with error: %d\n", WSAGetLastError());
            closesocket(ClientSocket);
            WSACleanup();
            return 1;
        }

    } while (iResult &gt; 0);

    // shutdown the connection since we're done
    iResult = shutdown(ClientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ClientSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(ClientSocket);
    WSACleanup();

    return 0;
}
</code></pre>