When I use the PInternetExplorer I can go to the page 'http://177.16.3.77', so the IP-nr. is reachable.
Can anybody help me solve this problem?
golanshahar
October 7th, 2005, 08:35 AM
did you call WSAStartup(..) before your code?
anyway look here at a sample ;)
function needed to convert hostname to ipaddress (http://www.codeguru.com/forum/showthread.php?t=351855)
Cheers
wildfrog
October 7th, 2005, 08:38 AM
When the function fails (returns null) you can (and you should) use WSAGetLastError() to determine what went wrong.
- petter
Marc G
October 7th, 2005, 08:42 AM
[ moved thread ]
Beekman
October 7th, 2005, 08:54 AM
Thnx for your responses, but the example is an gethostbyname-example (which as I said works fine) so I don't think I can use that.
I did use WSAStartup.
The WSAGetLastError returns an 11001, that means Host not found (?), which doesn't really help me...
(And sorry for posting in the wrong topic-category)
wildfrog
October 7th, 2005, 09:09 AM
You should use it something more like this:
iaHost.s_addr = inet_addr("177.16.3.77");
he = gethostbyaddr((const char *)&iaHost.s_addr, sizeof(iaHost.s_addr), AF_INET);
- petter
Marc G
October 7th, 2005, 09:10 AM
The first parameter to gethostbyaddr should not be a in_addr but simply your ip address in char format (= inet_addr("1.2.3.4")).
Also, see the example code in the MSDN.
Beekman
October 7th, 2005, 09:30 AM
I don't fully understand the last reply, and I started from the MSDN example.
It still keeps returning NULL's (and 11001's) at these tries;
he = gethostbyaddr(thisHst, 4, AF_INET);
he = gethostbyaddr((const char *)&iaHost, 4, AF_INET);
he = gethostbyaddr((const char *)&iaHost.s_addr, 4, AF_INET);
he = gethostbyaddr(thisHst, sizeof(iaHost.s_addr), AF_INET);
he = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
he = gethostbyaddr((const char *)&iaHost.s_addr, sizeof(iaHost.s_addr), AF_INET);
he = gethostbyaddr((const char *)&iaHost.s_addr, sizeof(iaHost.s_addr), AF_INET);
In some examples I found that some people put in the '4' hard-coded, I don't really understand why, but also that didn't work...
Edit;
I also included the example;
he = gethostbyname("www.codeguru.com");
ppaddr = (int**)he->h_addr_list;
sockAddr.sin_addr.s_addr = **ppaddr;
addr = inet_ntoa(sockAddr.sin_addr); // -> returns the correct IP
Well, your target machine probably don't have a (official) host name then.
- petter
MikeAThon
October 7th, 2005, 03:31 PM
Hello, when I try to connect with gethostbyname, all works well, but I can't get my program to work with gethostbyaddr.
....
When I use the PInternetExplorer I can go to the page 'http://177.16.3.77', so the IP-nr. is reachable.
gethostbyaddr() involves a reverse DNS lookup, so it's irrelevant that you can reach the host by IP number. The question is whether your DNS server is able to make the reverse resolution, from IP number back to host name.
Try using the nslookup utility program (which should be available in some flavor on all OSs, including linux and windows). From a command promt, type:
c:\>nslookup 177.16.3.77
What do you see? I just tried it from my machine, and my DNS server reports " can't find 177.16.3.77: Non-existent domain"
There are also web sites that provide reverse DNS lookup capabilites, like http://www.kloth.net/services/nslookup.php . Again, I tried your address there and it too was unable to resolve your given IP address back into a host name.
Just to be clear, this does not mean that your IP does not have a host name. It probably does. It simply means that the DNS server does not have an entry for the reverse of the host name. In DNS terminology, in addition to the forward entry (which, given a host name returns an IP number), there can also be a reverse entry normally called a "PTR" entry which, for an IP address of the form aaa.bbb.ccc.ddd looks something like ddd.ccc.bbb.aaa.in-addr.arpa. Without this "reverse" entry, it is not possible for the DNS server to make the reverse resolution from IP address back into host name.
Mike
Beekman
October 10th, 2005, 04:39 AM
I think I'm doing something totally wrong then... I don't really want to get a hostname, I just want to connect to a host, using an IP-adres.
But I just read in another thread, you don't even need gethostbyaddr. But when I try it without this function, it also doesn't work.
This is what I have now;
int port=80;
WSADATA info;
int err = WSAStartup(MAKEWORD(2,2), &info);
SOCKET s_ = socket(AF_INET,SOCK_STREAM,0);