i have an ip, in network order format, and i want to get its hostname, like if i had 216.92.131.172 i want to get its hostname which would be "pb9.pair.com",,, via winsock i dont know what api i should use.
Posted on 2003-06-18 14:46:01 by Qages
I think the api you need is: gethostbyaddr() .
Remember is wants network byte order, not dotted decimal.

RobotBob
Posted on 2003-06-18 15:00:45 by RobotBob
yea thats what i tried. it wants a char* in its definition not a network order.
Posted on 2003-06-18 18:58:47 by Qages
you could use inet_ntoa first to convert the ip to a string
Posted on 2003-06-18 19:24:33 by david
I usually use gethostbyname. EDIT: meant addr
Its funny the docs for that function say net byte order.
But it really wants a char * to a dotted decimal string.

I looked and could not find a function that goes from

For instance 127.0.0.1 converted to (16777343)
directly to host.

My docs show nothng else?

I guess you must inet_ntoa() first.

Thomas? anyone else know an answer?

EDIT: top post had not displayed for me when I posted.
RobotBob
Posted on 2003-06-18 19:29:16 by RobotBob
RobotBob: Gethostbyname is not the function you want, but I assume that was a typo since you already mentioned gethostbyaddr. It wants a pointer to a network byte order address, not to a dotted string. The docs clearly say so:

addr
Pointer to an address in network byte order.

This is to support other types of addresses that aren't 4 bytes in length.
I don't have an asm example available but I do have a C++ one:


const char *ipNumber = "216.239.51.100";
unsigned long ipValue = inet_addr(ipNumber);

HOSTENT *pHostent =
gethostbyaddr(reinterpret_cast<const char*>(&ipValue),
sizeof(ipValue),
AF_INET);
if (pHostent!=NULL)
{
cout << "IP number " << ipNumber << " has hostname " << pHostent->h_name << "." << endl;
}
else
{
cerr << "No hostname found" << endl;
}


Thomas
Posted on 2003-06-19 04:20:34 by Thomas
I mentioned this to qages, but he experienced problems. So I quickly tested and it failed.
The docs do state that, I was confused
when my results didn't match.

I look at my old code and called gethostbyaddr
this morning. I passed it 16777343 and it worked.

Previously it died when I tried this.

When I looked at my old code this is what I have.

Scary thing is I have written
alot of winsock apps, I feel so ashamed :grin: .

RobotBob
Posted on 2003-06-19 06:06:14 by RobotBob

Scary thing is I have written
alot of winsock apps, I feel so ashamed .


Me too!!! :grin:
Posted on 2003-06-19 06:17:10 by david
it does work, hmmmmmmm. thx!

IP number 216.239.51.100 has hostname www.google.com.
Posted on 2003-06-19 12:18:00 by Qages