472,993 Members | 3,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

'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 6776
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: yawnmoth | last post by:
using the gethostbyname function seems to noticeably slow down pages. some of the comments in php.net's gethostbyname entry suggest using a version that caches the result, but those versions also...
0
by: Eric Brunel | last post by:
Hi all, I just compiled Python 2.3.2 on Linux Mandrake 8.0, upgrading from Python 2.1. I have one problem that I can't figure out: when I wanted to get "the" IP address of the current host with...
1
by: Fortepianissimo | last post by:
I've tried using socket.setdefaulttimeout(timeout) to set the default timeout to 'timeout' for all sockets. The part that's not clear to me, is that if this will affect...
5
by: ruroma | last post by:
Hello, I have one problem, and can think of two possible solutions but I can't manage to make them work. I'm open to other suggestions if you thik is better. The main function calls...
1
by: Glen Conway | last post by:
Hi, I'm trying to use the gethostbyname function from wsock32.dll and failing dismally Has anyone got a successful implementation of this in VB.NET? My ulitimate goal is to resolve NetBIOS names...
1
by: Quentin Carbonneaux | last post by:
Hello, I'm writing a little program which has to get a FQDN on his stdin and return the IP on stdout, we can do it easily with gethostbyname call but I would like to set up a timeout to this...
1
by: yawnmoth | last post by:
I seem to be getting conflicting gethostbyname behavior on different servers. Before going into detail, here's the script I'm using: <? $address = $HTTP_SERVER_VARS; $rev =...
2
by: Andrew DeFaria | last post by:
I've been having problems with my ISP. One way it seems to manifest itself is that I can not reach or contact my ISP's DNS servers. IOW a simply nslookup google.com will fail. So I tried writing...
18
by: aj | last post by:
I have the following snippet of code. On some platforms, the delete calls works, on Linux, it core dumps (memory dump) at the delete call. Am I responsible for deleting the memory that...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.