getting ip by hostname

This is a discussion on getting ip by hostname within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I have been working on this since a few hours, and I am stuck. If gethostbyaddr fails it should ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    getting ip by hostname

    Hi, I have been working on this since a few hours, and I am stuck. If gethostbyaddr fails it should try gethostbyname, but neither of them is working.

    Can anyone point me to the problem?


    Code:
    const char *host = "localhost";
    
    int main(void) {
    				char buf[128];
    				LPHOSTENT hostent = NULL;
    				IN_ADDR iaddr;
    				DWORD addr = inet_addr(host); 
    
    				if (addr != INADDR_NONE) {
    					hostent = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);
    					if (hostent != NULL) {
    						sprintf(buf, ".. -> %s.", hostent->h_name);
    						puts(buf);
    					}
    				}
    				else {
    					hostent = gethostbyname(host);
    					if (hostent != NULL) {
    						iaddr = *((LPIN_ADDR)*hostent->h_addr_list);
    						sprintf(buf, ".. -> %s.", inet_ntoa(iaddr));
    						puts(buf);
    					}
    				}
    				if (hostent == NULL) {
    					sprintf(buf,"..  Couldn't resolve hostname.");
    					puts(buf);
    				}
    				
    
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Read the manual pages to find out how those functions pass back extended error information, then use that to print something more informative than "it didn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    If gethostbyaddr fails it should try gethostbyname
    Your program's logic reads differently. If gethostbyaddr() is merely called, gethostbyname() will never get called, regardless of gethostbyaddr()'s result. Check the logic on your outermost if()/else statements. gethostbyname() gets called if inet_addr() is called, and does not return INADDR_NONE. Your implementation seems correct (much like example code out there), you understanding, however, is incorrect.

    Some nitpicks: You also called sprintf/puts together - why not just printf()? Also, those fancy double arrows don't display in my (Windows) 'terminal'...

    Finally, since you seem to be using windows: First, choose whether to use the windows typenames, or the standard ones. (LPHOSTENT or struct hostent *) (Use the latter - they're portable!) Finally: Are you called WSAStartup()/WSACleanup() - everything will mysteriously fail until you do. Hint: Call WSAGetLastError(), as Salem suggests:
    Code:
    WSAGetLastError(): 10093
    ...
    #define WSANOTINITIALISED	(WSABASEERR+93)
    Last edited by Cactus_Hugger; 05-27-2007 at 12:01 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. obtain hostname or IP
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 12-01-2004, 07:46 AM
  3. Outputting your IP and hostname
    By Nakeerb in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2002, 09:24 PM
  4. ip or hostname
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 04-08-2002, 09:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21