468,766 Members | 1,293 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

'gethostbyname' fails Please Help

Could some C guru please help ? I am writing a Web server on RH 9.0
box. In the section of the code where the Web server has to be
initiialized, I have:

char host[128];
struct hostent *hp = NULL;
..............................
...............................
if(gethostname(host, sizeof(host)) < 0)
{
/* Print error message and return -1 */
}

if((hp = gethostbyname(host)) == NULL)
{
/* Print another message and return -1 */
}

The second guard is always failing. I have tried 'nslookup' from the
command prompt and it works fine, by returning the name of the DNS
server.
Any hints or suggestions would be greatly appreciated.
Jul 24 '08 #1
6 6445
cp**********@yahoo.com wrote:
..............................
if(gethostname(host, sizeof(host)) < 0)
{
/* Print error message and return -1 */
}

if((hp = gethostbyname(host)) == NULL)
{
/* Print another message and return -1 */
}

The second guard is always failing. I have tried 'nslookup' from the
command prompt and it works fine, by returning the name of the DNS
server.
Not really a C question - its hard to say why your implementation or
environment might cause gethostbyname() to fail. I assume that the
argument types are correct?
Any hints or suggestions would be greatly appreciated.
comp.unix.programmer is probably a better place as networking is topical
there.
Jul 24 '08 #2
"cp**********@yahoo.com" <cp**********@yahoo.comwrites:
Could some C guru please help ? I am writing a Web server on RH 9.0
box. In the section of the code where the Web server has to be
initiialized, I have:

char host[128];
struct hostent *hp = NULL;
.............................
..............................
if(gethostname(host, sizeof(host)) < 0)
[...]

Try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 24 '08 #3
That fails too?

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
struct hostent *server;
if (argc < 2)
{
fprintf(stderr,"usage %s hostname\n", argv[0]);
exit(0);
}

server = gethostbyname(argv[1]);

if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}

/*You can put something here to print positive results, just in
case.*/

return 0;
}

Regards
Rafael
Jul 24 '08 #4
On 2008-07-24, cp**********@yahoo.com <cp**********@yahoo.comwrote:
Could some C guru please help ? I am writing a Web server on RH 9.0
box. In the section of the code where the Web server has to be
initiialized, I have:

char host[128];
struct hostent *hp = NULL;
.............................
..............................
if(gethostname(host, sizeof(host)) < 0)
{
/* Print error message and return -1 */
}
This just retrieves the machine's own name, as it is known within
your machine. It's largely a useless piece of information.
if((hp = gethostbyname(host)) == NULL)
{
/* Print another message and return -1 */
}
DNS might know nothing about your machine.

If you set up a new Linux box, and call it ``mybox'', do you think
that your DNS servers automatically knows about the name ``mybox''?

The gethostbyname function might resolve it properly through /etc/hosts,
rather than through DNS, if your /etc/nsswitch.conf is set up to look in
/etc/hosts, and if the contents of your /etc/hosts are sane. Otherwise,
all bets are off.

What do you want to achieve by discovering the host's name and address?

What if the host has more than one address?

As a client, to talk to some service on the local machine, you can always use
INADDR_LOOPBACK, without any name resolution.

To discover all of the external-facing IP addresses of your machine,
well, that is more complicated. You have to do something like
enumerate all of the interfaces (in a very system specific way)
and query all of their configured addresses.

But you don't need to do this in a simple Web server. Here is a simpler
solution. Part of an HTTP request is the domain name. So multi-hosting can be
handled entirely within your Web server.

Just bind your socket to INADDR_ANY, so that you catch connection
requests from all attached networks. Then match the domain names
in the requests against supported domains.

You tell your web server (e.g. through a configuration file or whatever) to
serve some pages for "www.foo.com". Clients will request the pages that way,
and you simply have to parse the name out of the request and compare strings;
if a request is for other than "www.foo.com" you reject it with a 404.
As a bonus, you can handle multiple domains at the same time, so you can serve
up a different file for "www.foo.com/index.html" and "www.bar.com/index.html".
The second guard is always failing. I have tried 'nslookup' from the
command prompt and it works fine, by returning the name of the DNS
server.
nslookup (or at least some of its implementations) prints the name of the DNS
server that it's using, whether or not the lookup succeeds or fails. The one I
have here also looks up a successful termination status if the lookup.
Jul 25 '08 #5
On Jul 25, 2:58 am, Kaz Kylheku <kkylh...@gmail.comwrote:
<snip off-topic posix reply>

Why not set up follow-ups to comp.unix.programmer instead of here?
Jul 25 '08 #6
vi******@gmail.com said:
On Jul 25, 2:58 am, Kaz Kylheku <kkylh...@gmail.comwrote:
<snip off-topic posix reply>

Why not set up follow-ups to comp.unix.programmer instead of here?
When a mouse talks to a lion, "please" is always a good move.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 25 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by yawnmoth | last post: by
1 post views Thread by Fortepianissimo | last post: by
5 posts views Thread by ruroma | last post: by
1 post views Thread by Glen Conway | last post: by
1 post views Thread by Quentin Carbonneaux | last post: by
1 post views Thread by yawnmoth | last post: by
2 posts views Thread by Andrew DeFaria | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.