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

// link with Ws2_32.lib

int main(int argc, char **argv)
{
    // Declare and initialize variables
	WSADATA wsaData;
	int iResult, i;
	char * ptr;
	unsigned long ulAddr = INADDR_NONE;
	
	// Validate the parameters
	if (argc != 2)
	{
		printf("usage: %s <IPv4 address>\n", argv[0]);
        printf("  inetaddr converts a string containing an\n");
        printf("  IPv4 address in one of the supported formats\n");
        printf("  to a unsigned long representing an IN_ADDR.\n\n");
		printf("Sample: %s 192.168.16.34\n", argv[0]);
        return 1;
    }

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

	// Call inet_addr(). If the call succeeds,
	// the result variable will hold a IN_ADDR
    ulAddr = inet_addr(argv[1]);
    if ( ulAddr == INADDR_NONE ) {
        printf("inet_addr failed and returned INADDR_NONE\n");
        WSACleanup();
        return 1;
    }   
    
    if (ulAddr == INADDR_ANY) {
        printf("inet_addr failed and returned INADDR_ANY\n");
        WSACleanup();
        return 1;  
    }

    printf("inet_addr returned success\n");
    
    // Retrieve each address and print out the hex bytes
	printf("adress: ");
	ptr = (char *)&ulAddr;
	for(i=0; i < 4; i++, ptr++)
	{
		printf("%0x ", ptr[i]);
	}

    WSACleanup();
    return 0;
}