'gethostbyname' fails Please Help

Discussion in 'C Programming' started by cpptutor2000@yahoo.com, Jul 24, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. "" <> writes:
    > 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) <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"
     
    Keith Thompson, Jul 25, 2008
    #2
    1. Advertising

  3. soscpd Guest

    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
     
    soscpd, Jul 25, 2008
    #3
  4. Kaz Kylheku Guest

    On 2008-07-24, <> wrote:
    > 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.
     
    Kaz Kylheku, Jul 25, 2008
    #4
  5. Guest

    On Jul 25, 2:58 am, Kaz Kylheku <> wrote:
    <snip off-topic posix reply>

    Why not set up follow-ups to comp.unix.programmer instead of here?
     
    , Jul 25, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill

    GetHostByName in IIS

    Bill, Aug 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    453
  2. KK
    Replies:
    2
    Views:
    597
    Big Brian
    Oct 14, 2003
  3. Eric Brunel
    Replies:
    0
    Views:
    1,461
    Eric Brunel
    Oct 27, 2003
  4. Andrew DeFaria

    If gethostbyname fails it never recovers

    Andrew DeFaria, Oct 5, 2006, in forum: Perl
    Replies:
    2
    Views:
    2,322
    Andrew DeFaria
    Oct 6, 2006
  5. Andrew DeFaria

    If gethostbyname fails it never recovers

    Andrew DeFaria, Oct 6, 2006, in forum: Perl Misc
    Replies:
    4
    Views:
    188
    Peter J. Holzer
    Oct 7, 2006
Loading...

Share This Page