|
-
October 7th, 2005, 08:30 AM
#1
gethostbyaddr
Hello, when I try to connect with gethostbyname, all works well, but I can't get my program to work with gethostbyaddr.
This is (among others) the code I'm trying now, but it only returns NULL's...
Code:
char *tempHost = strstr(thisHst,".");
LPHOSTENT he;
in_addr iaHost;
iaHost.s_addr = inet_addr("177.16.3.77"); //thisHst
he = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr), AF_INET);
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?
-
October 7th, 2005, 08:35 AM
#2
Re: gethostbyaddr
did you call WSAStartup(..) before your code?
anyway look here at a sample 
Cheers
-
October 7th, 2005, 08:38 AM
#3
Re: gethostbyaddr
When the function fails (returns null) you can (and you should) use WSAGetLastError() to determine what went wrong.
- petter
-
October 7th, 2005, 08:42 AM
#4
-
October 7th, 2005, 08:54 AM
#5
Re: gethostbyaddr
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)
-
October 7th, 2005, 09:09 AM
#6
Re: gethostbyaddr
You should use it something more like this:
Code:
iaHost.s_addr = inet_addr("177.16.3.77");
he = gethostbyaddr((const char *)&iaHost.s_addr, sizeof(iaHost.s_addr), AF_INET);
- petter
-
October 7th, 2005, 09:10 AM
#7
Re: gethostbyaddr
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.
-
October 7th, 2005, 09:30 AM
#8
Re: gethostbyaddr
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;
Code:
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;
Code:
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
he = gethostbyaddr((const char *) &addr, sizeof(addr), AF_INET); // = NULL
Last edited by Beekman; October 7th, 2005 at 09:44 AM.
-
October 7th, 2005, 09:42 AM
#9
Re: gethostbyaddr
Well, your target machine probably don't have a (official) host name then.
- petter
-
October 7th, 2005, 03:31 PM
#10
Re: gethostbyaddr
 Originally Posted by Beekman
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:
Code:
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
-
October 10th, 2005, 04:39 AM
#11
Re: gethostbyaddr
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;
Code:
int port=80;
WSADATA info;
int err = WSAStartup(MAKEWORD(2,2), &info);
SOCKET s_ = socket(AF_INET,SOCK_STREAM,0);
//in_addr iaHost;
//iaHost.s_addr = inet_addr("172.16.3.77");
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr = *((in_addr *) "172.16.3.77");
memset(&(addr.sin_zero), 0, 8);
int con = connect(s_, (sockaddr *) &addr, sizeof(sockaddr)); //(con == 0)
int sendLen = 4 + strlen(thisMon) + 14;
char *sendLine = new char[sendLen];
int qLength = sizeof(sendLine);
int bufLen = 8192;
char *HtmlData = new char[bufLen];
send(s_, "GET /HTTP/1.0\r\n\r\n", qLength, 0);
int some = WSAGetLastError(); //(some == 183)
int recResult = recv (s_, HtmlData, bufLen, 0);
some = WSAGetLastError(); //(some == 10054)
But the WSA error = 183 (which is, according to other threads, a file-write error, and doesn't make any sense here) and the result = -1.
Last edited by Beekman; October 10th, 2005 at 05:05 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|