Networking & Internet Functions

Core Networking

Internet Protocols (HTTP, FTP)

DNS and Name Resolution

getaddrinfo

Resolves a hostname or service name to an address structure.

Parameters

  • pNodeName: The hostname or network name to resolve.
  • pServiceName: The service name or port number to resolve.
  • pHints: A pointer to an ADDRINFO structure that specifies criteria for the returned address structures.
  • ppResult: A pointer to a pointer that receives the address of a linked list of one or more ADDRINFO structures.

Return Value

  • On success, returns 0 (zero).
  • On failure, returns a non-zero error code.

Remarks

  • This is the recommended function for protocol-independent name resolution.
  • Frees the memory returned by getaddrinfo using freeaddrinfo.

Example


#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

#pragma comment(lib, "Ws2_32.lib")

int main() {
    WSADATA wsaData;
    ADDRINFO hints, *result, *ptr;
    int iResult;

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

    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo("www.example.com", "80", &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    printf("Successfully resolved www.example.com:\n");
    for(ptr=result; ptr != NULL ; ptr=ptr->ai_next) {
        if (ptr->ai_family == AF_INET) {
            struct sockaddr_in *ipv4 = (struct sockaddr_in *)ptr->ai_addr;
            printf("  IPv4 Address: %s\n", inet_ntoa(ipv4->sin_addr));
        } else if (ptr->ai_family == AF_INET6) {
            // Handle IPv6 if necessary
            printf("  IPv6 Address found (handling omitted for brevity).\n");
        }
    }

    freeaddrinfo(result);
    WSACleanup();
    return 0;
}
                

InternetOpen

Initializes an application for use with the Internet functions.

Parameters

  • lpszAgent: Pointer to a null-terminated string that specifies the name of the calling application or object.
  • dwAccessType: Specifies the type of access to the Internet. Can be INTERNET_OPEN_TYPE_PRECONFIG, INTERNET_OPEN_TYPE_DIRECT, or INTERNET_OPEN_TYPE_PROXY.
  • lpszProxyName: Pointer to a null-terminated string that specifies the name and, if necessary, the port number of the proxy server.
  • lpszProxyBypass: Pointer to a null-terminated string that specifies a semicolon-separated list of servers that bypass the proxy server.
  • dwFlags: Specifies whether to use asynchronous or synchronous operations.

Return Value

  • If the function succeeds, it returns a valid Internet handle.
  • If the function fails, it returns NULL.

Remarks

  • This function must be called before any other Internet functions (except InternetGetLastResponseInfo).
  • The returned handle should be closed by calling InternetCloseHandle.

Example


#include <windows.h>
#include <wininet.h>
#include <stdio.h>

#pragma comment(lib, "Wininet.lib")

int main() {
    HINTERNET hInternet;

    // Initialize Internet functions for direct access
    hInternet = InternetOpen(L"MyAwesomeApp/1.0",
                             INTERNET_OPEN_TYPE_DIRECT,
                             NULL,
                             NULL,
                             0); // 0 for synchronous operations

    if (hInternet == NULL) {
        printf("InternetOpen failed. Error: %lu\n", GetLastError());
        return 1;
    }

    printf("InternetOpen succeeded. Handle: %p\n", hInternet);

    // Use hInternet for subsequent Internet operations...

    // Close the handle when done
    InternetCloseHandle(hInternet);
    printf("Internet handle closed.\n");

    return 0;
}