Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

gethostbyaddr

Posted on 2001-09-10
10
Medium Priority
?
837 Views
Last Modified: 2012-08-14
Hey,

I have the following code :

/********/
char server_ip[64] = "1.2.3.4";
struct hostent *hp;
unsigned int addr;

addr = inet_addr(server_ip);
hp = gethostbyaddr((char *)&addr,4,AF_INET);
if (hp == NULL)
{
   printf("Cannont resolve address [%s]: Error %d",server_ip,WSAGetLastError());
}
/*********/

and when I run it, I get the error

Cannot resolve address [1.2.3.4]: Error 11004

Why is that ? why does it try to resolve the address ?
Do I need DNS here ? and is so, WHY ???

Thanks,
   Gilad.
0
Comment
Question by:gshriki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 3

Expert Comment

by:kulina
ID: 6470589
Did you initialize winsock (using WSAStartup())?
0
 
LVL 10

Expert Comment

by:makerp
ID: 6470844
11004, the requested name is valid and was found in the database, but it does not have the correct data resolved for.

what this mneans i do not know.

is 1.2.3.4 a real ip address of a machine on your network?, what is it you are trying to do
0
 
LVL 3

Expert Comment

by:kulina
ID: 6470944
Use struct in_addr addr instead of unsigned int, i.e:

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

#pragma comment( lib, "ws2_32.lib" )
int initWinSock(void);

int main( int argc, char *argv[] )
{
    struct hostent *host;
    struct in_addr addr;

    if( argc != 2 ){
        printf( "Usage: host <hostname>\n" );
        return 1;
    }

    if( initWinSock() ){
        printf( "Error! Can't initialize winsock\n" );
        return 1;
    }

    // Check whether argument is an IP address or a DNS
    addr.s_addr = inet_addr( argv[1] );
    if( addr.s_addr == INADDR_NONE ){
        // Not an IP address string, maybe a DNS
        host = gethostbyname( argv[1] );
    }
    else{
        // A valid IP address string
        host = gethostbyaddr( (const char *)&addr,
                        sizeof(struct in_addr), AF_INET );
    }

    // Check return value
    if( host == NULL ){
        printf( "Cannot resolve address %s. Error code: %d\n",
                argv[1], WSAGetLastError() );
        return 1;
    }

    // Print host info
    printf( "\nHost Name........: %s", host->h_name );
   
     WSACleanup();
     return 0;
}

/* initiates the use of winsock by a process */
int initWinSock()
{
   WORD wVersionRequested;
   WSADATA wsaData;
   int err;
   
   wVersionRequested = MAKEWORD( 1, 1 );
   err = WSAStartup( wVersionRequested, &wsaData );

   if ( err != 0 ){
       /* no usable winsock found (for requested version) */
       return -1;
   }

   return 0;
}

Try using it this way:
host 64.58.76.22

Result:
Host Name........: w4.dcx.yahoo.com

Hope this helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:kulina
ID: 6470951
By the way, 1.2.3.4 is indeed unresolved IP address. If you ping it you get:
ping 1.2.3.4

Pinging 1.2.3.4 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

0
 
LVL 2

Expert Comment

by:sateesh_babu
ID: 6471144
I get from your code that you are trying to use gethostbyname. If you want to get a correct result, then just give the IP address of your machine and test the program.
In Unix you can get the ip address of your machine, by
ping -s <hostname>
If you want your hostname, type hostname.

In windows you can get the IP address using ipconfig command.

Hope this helps!!!
0
 
LVL 3

Expert Comment

by:kulina
ID: 6471785
>>Why is that?
"gethostbyaddr()" needs "in_addr" structure as a first parameter, cast into char *. You passed unsigned int which is wrong.

>>why does it try to resolve the address?
By resolve it means that "gethostbyaddr()" will try to get information about the host whose IP address is stored in "in_addr" member "s_addr" (remember, "in_addr" is passed as the first parameter). If this IP address exists "gethostbyaddr()" will 'fill out' a hostent structure with the information about that host (i.e. host name, alias, other ip addresses etc). "gethostbyaddr()" will then return a pointer to that hostent structure

>>Do I need DNS here ? and is so, WHY ???
You do not need to pass domain (or host) name to "gethostbyaddr()" since it wouldn't know what to do with it. At some point, probably a memory access violation would happen. On the other hand, as you can see from my code example above, you may pass the DNS name to "gethostbyname()" function. Both functions are used to retrieve information about IP hosts, which is stored in hostent structure. They just do it differently, i.e.
gethostbyname( char *hostname )
retrieves information about host based on the host name i.e.
struct hostent *he = gethostbyname( "www.yahoo.com" );

gethostbyaddr( (char *)struct in_addr addr, sizeof(struct in_addr), AF_INET )
retrieves information based on the IP address that is converted to unsigned long format using inet_addr. See http://www.pc-help.org/obscure.htm for information on different representations of IP address.

If you have more questions let me know.
0
 

Author Comment

by:gshriki
ID: 6472080
Thanks everyone, but i managed to take the gethostbyaddr out of the code, and i'm not using it at all.

Thanks again...
0
 
LVL 3

Accepted Solution

by:
kulina earned 400 total points
ID: 6472313
Good news! Would you mind elaborating on details of your workaround?
Thanks.
0
 

Author Comment

by:gshriki
ID: 6473689
Yes,
Because I already have the IP adderss, I didn't need the hostent structure and the gethostbyaddr command...


0
 

Expert Comment

by:peter_sheynkman
ID: 6475773

I would say that "kulina" deservse the points on that one.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question