473,659 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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.
Jul 24 '08 #1
6 6841
cp**********@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.
Jul 24 '08 #2
"cp**********@y ahoo.com" <cp**********@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) 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(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
Jul 24 '08 #4
On 2008-07-24, cp**********@ya hoo.com <cp**********@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.
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.progr ammer 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.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
Jul 25 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6625
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 only speedup subsequent calls to gethostbyname - the first call is still as slow as ever. so is there anything i can do to speed this up? perhapes i can just implement a function equivalent to gethostbyname using fsockopen, or something?
0
6019
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 Python 2.1, I did: Python 2.1.1 (#12, Apr 10 2002, 17:52:08) on linux2 Type "copyright", "credits" or "license" for more information. >>> import socket
1
7126
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 socket.gethostbyname(). In my observation the timeout setting did not affect the DNS lookup. It might be my misunderstanding of the semantics here: would
5
2734
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 sendSocket, but I don't know how to tell it where to send the socket. ( I think I'm messing it up with pointers)
1
3808
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 to IP Addresses. I can use the framework DNS features if a DNS server is present on the network but this cannot use WINS if no DNS server is available. Any help or pointers are apprecatiated. Here is the code I've managed to do so far. I've...
1
7205
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 blocking system call. So I read texts about this and I planed to use a hack with alarm and setjmp/longjmp. My program works until there is a timeout, when it occurs, all the gethostbyname calls which follow fail, backtrace in gdb give me this : #0 ...
1
1718
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 = implode('.',array_reverse(explode('.', $address))); $lookup = "$rev.l1.spews.dnsbl.sorbs.net"; echo gethostbyname($lookup); echo '<br />';
2
5573
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 a script that would monitor this. The script calls gethostbyname for google.com every 15 minutes and logs the status. When gethostbyname fails however it never comes back. My ISP and internet connection may come back and nslookup at the command...
18
3653
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 gethostbyname allocated? struct hostent *lHostInfo; lHostInfo = gethostbyname(ipHost.c_str()); memcpy(&(lDestAddr.sin_addr), lHostInfo->h_addr_list, lHostInfo- delete lHostInfo;
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.