'gethostbyname' fails Please Help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cpptutor2000@yahoo.com

    '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(h ost)) == 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.


  • Mark McIntyre

    #2
    Re: 'gethostbyname' fails Please Help

    cpptutor2000@ya hoo.com wrote:
    ............... ...............
    if(gethostname( host, sizeof(host)) < 0)
    {
    /* Print error message and return -1 */
    }
    >
    if((hp = gethostbyname(h ost)) == 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.progr ammer is probably a better place as networking is topical
    there.

    Comment

    • Keith Thompson

      #3
      Re: 'gethostbyname' fails Please Help

      "cpptutor2000@y ahoo.com" <cpptutor2000@y ahoo.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.progr ammer.

      --
      Keith Thompson (The_Other_Keit h) kst-u@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"

      Comment

      • soscpd

        #4
        Re: 'gethostbyname' fails Please Help

        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(a rgv[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

        Comment

        • Kaz Kylheku

          #5
          Re: 'gethostbyname' fails Please Help

          On 2008-07-24, cpptutor2000@ya hoo.com <cpptutor2000@y ahoo.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(h ost)) == 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.co m". 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.co m" 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.co m/index.html" and "www.bar.co m/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.

          Comment

          • vippstar@gmail.com

            #6
            Re: 'gethostbyname' fails Please Help

            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.progr ammer instead of here?

            Comment

            • Richard Heathfield

              #7
              Re: 'gethostbyname' fails Please Help

              vippstar@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.progr ammer 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

              Comment

              Working...