Hello.....
I've written this simple C program to return the IP of a hostname ... this program compiles perfectly as well the Output is built...however, on executing the output, the Output crashes..... Can someone tell me what's the problem?
Regards.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <winsock.h>

#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "advapi32.lib")

void main()
{

struct hostent *he;
char *addr;
addr = "www.google.com";
he = gethostbyname(addr);
printf("%s", he -> h_name);
getchar();
}

<< moderator edit: added code tags: [code][/code] >>
Edit/Delete Message

hi..
well i called the WSAStartup now...and its not giving any error at that stage ..but still i am not getting the Output..... I checked the program for various possible errors...but its returning some unknown error(have a look at the code....it returns the default error msg)... what could possibly be the problem now?? Someone told me that I need to configure DNS server on my machine for this .....is it so??
thanks.

#include <afx.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <winsock2.h>
#include <ctype.h>
#include <cstring>


#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(linker, "/delayload:Ws2_32.dll")


void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;


struct hostent *he = NULL;
char addr [] = "http://www.google.com";

 
wVersionRequested = MAKEWORD( 2, 2 );
 
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
	printf("\ncould not find a usable WinSock DLL.\n");
        return;
}
 

if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
	        WSACleanup( );
    return; 
}


he = gethostbyname(addr);
if(he != NULL)
  printf("%s", he->h_name);
else
{
	printf("\nNULL....");
     int err = WSAGetLastError();
     CString cserr = _T("");
	switch(err)
	{
	case WSANOTINITIALISED:
		cserr.Format("A successful WSAStartup must occur before using this function.");
		printf("\n reached here1");
		break;
	case WSAENETDOWN:
		cserr.Format("The network subsystem has failed.");
		printf("\n reached here2");
	case WSAEADDRINUSE :
		cserr.Format("A process on the machine is already bound to the same fully-qualified address and the socket has not been marked to allow address re-use with SO_REUSEADDR. For example, IP address and port are bound in the af_inet case) . (See the SO_REUSEADDR socket option under setsockopt.)");
		printf("\n reached here3");
		break;
	case WSAEADDRNOTAVAIL:
		cserr.Format("The specified address is not a valid address for this machine");
		printf("\n reached here4");
		break;
	case WSAEFAULT:
		cserr.Format("The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, the name parameter contains incorrect address format for the associated address family, or the first two bytes of the memory block specified by name does not match the address family associated with the socket descriptor");
		printf("\n reached here5");
		break;
	case WSAEINPROGRESS:
		cserr.Format("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function");
		printf("\n reached here6");
		break;
	case WSAEINVAL:
		cserr.Format("The socket is already bound to an address. (accept case) The listen function was not invoked prior to accept.");
		printf("\n reached here7");
		break;
	case WSAENOBUFS:
		cserr.Format("Not enough buffers available, too many connections");
		printf("\n reached here8");
		break;
	case WSAENOTSOCK:
		cserr.Format("The descriptor is not a socket.");
		printf("\n reached here9");
		break;
	case WSAEISCONN:
		cserr.Format("The socket is already connected.");
		printf("\n reached here10");
		break;
	case WSAEMFILE:
		cserr.Format("No more socket descriptors are available or (accept case) The queue is nonempty upon entry to accept and there are no descriptors available.");
		printf("\n reached here11");
		break;
	case WSAEOPNOTSUPP:
		cserr.Format("The referenced socket is not of a type that supports the operation.");
		printf("\n reached here12");
		break;
	case WSAEINTR:
		cserr.Format("A blocking Windows Sockets 1.1 call was canceled through WSACancelBlockingCall.");
		printf("\n reached here13");
		break;
	case WSAEWOULDBLOCK:
		cserr.Format("The socket is marked as nonblocking and no connections are present to be accepted.");
		printf("\n reached here14");
		break;
	case WSAEALREADY:
		cserr.Format("A nonblocking connect call is in progress on the specified socket");
		printf("\n reached here15");
		break;
	case WSAEAFNOSUPPORT:
		cserr.Format("Addresses in the specified family cannot be used with this socket.");
		printf("\n reached here16");
		break;
	case WSAECONNREFUSED:
		cserr.Format("The attempt to connect was forcefully rejected.");
		printf("\n reached here17");
		break;
	case WSAENETUNREACH:
		cserr.Format("The network cannot be reached from this host at this time.");
		printf("\n reached here18");
		break;
	case WSAETIMEDOUT:
		cserr.Format("Attempt to connect timed out without establishing a connection.");
		printf("\n reached here19");
		break;
	case WSAENOTCONN:
		cserr.Format("The socket is not connected");
		printf("\n reached here20");
		break;
	case WSAENETRESET:
		cserr.Format("The connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress");
		printf("\n reached here21");
		break;
	case WSAESHUTDOWN:
		cserr.Format("The socket has been shut down; it is not possible to recv on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH.");
		printf("\n reached here22");
		break;
	case WSAEMSGSIZE:
		cserr.Format("The message was too large to fit into the specified buffer and was truncated.");
		printf("\n reached here23");
		break;
	case WSAECONNABORTED:
		cserr.Format("The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable.");
		printf("\n reached here24");
		break;
	case WSAECONNRESET:
		cserr.Format("The virtual circuit was reset by the remote side executing a \"hard\" or \"abortive\" close. The application should close the socket as it is no longer usable. On a UDP datagram socket this error would indicate that a previous send operation resulted in an ICMP \"Port Unreachable\" message.");
		printf("\n reached here25");
		break;
	default:
		cserr.Format("Unknown error");
		printf("\n reached here26");
		
	}
}
getchar();
}

I found out the error...... it is "Valid name, no data record of requested type" !!...Now what dies this mean and how to solve it??

Hey people....
I finally resolved all errors...and the program is working fine now.....thanks a lott for the help...
Now, I wish to make a DNS query to a web server, which depending upon my query will take some action... I have no idea how to go about doing it..... I think we'll need a DNS server on the web server computer...or something like that......please give me some idea how to proceed...
how does gethostbyname work? does it approach the website in question?
regards.

This article has been dead for over six months. Start a new discussion instead.