View Full Version : how to use DNS of server to connect over Internet
hello all,
i want client application to connect to server using the servers name(not ip address), how can this be done.
if i use ip address, i think that it may not be able to locate server or may connect to someother machine , am i right( i am talking bout server and client using Internet/WWW , not in a LAN.),
if so, how can the client use the DNS name of server in the application?,
as in struct sockaddr_in we have to use the decimal dotted address, not name.
dev
RobSeace
04-09-2003, 01:18 PM
You need to use a function like gethostbyname() or getaddrinfo() to
perform the DNS lookup for you... That'll get you the raw IP address
you need to use... Check your local man pages for more info.... Or,
do a search here for "gethostbyname", which should get you some
example usage, too... Or, just see question 3.1 (http://www.developerweb.net/forum/viewtopic.php?t=46) in the main FAQ...
hi Rob,
i got gethostbyname(),
but can u tell me will it work in my case:-
i have the webserver(linux), i will open a port and listen on it(i am trying to code so,), i want my client to be albe to connect to the server's listing port , meidum will be Internet/WWW,?
how does gethostbyname work?,
i hope it may look into its hosts first, but if it doesnot , will it try to get it from routers above it?
dev
RobSeace
04-09-2003, 07:17 PM
Well, gethostbyname() just uses the local resolver to lookup the
address... Typically, the resolver can be configured to look in
"/etc/hosts" first by either some setting in "/etc/resolv.conf" or
something like "/etc/nsswitch.conf"... It depends on your specific
system... Then, if it doesn't find the host there, it'll contact the
DNS servers listed in "/etc/resolv.conf" to perform a DNS lookup...
Uzume
04-14-2003, 06:28 AM
If you have it available, I would like to suggest you try to use getaddrinfo() or getnameinfo() instead of other calls that translate names (numeric or otherwise) to socket addresses and back.
Using such as well as using struct sockaddr_storage instead of struct sockaddr_in or struct sockaddr for allocating socket addresses on the stack, then you will in essence have a address family independent application. This could allow your application to work for IPv6 and other similar protocols.
RobSeace
04-14-2003, 01:04 PM
I agree and really like getaddrinfo(), myself... However, I find some of
the current implementations of it rather lacking... Eg: as far as I'm aware,
even in the newest glibc, getaddrinfo() is still broken... (I reported it and
demonstrated it on RH's bugzilla page, LONG ago, but received only silence
in response... ;-/ Except for a few other users who also discovered the
same problem and chimed in alongside me, also to no avail...) Namely,
it refuses to honor "/etc/nsswitch.conf", so that it ALWAYS tries to perform
a DNS lookup first, even if looking up something in "/etc/hosts"... This is
just horribly broken behavior, and renders the function useless to me,
sadly... I'd really love to be able to use it... ;-/ So, for now, I'm forced
instead to use gethostbyname_r() (the thread-safe version, whose usage
is pretty much completely undocumented, and which I had to basically
figure out on my own)... It works well enough for my needs... (And, it'll
actually handle IPv6 too, just not as smoothly as getaddrinfo()...) And,
at least it obeys the local configuration, and checks "/etc/hosts" before
trying to do a DNS lookup, anyway... ;-/
mlampkin
04-15-2003, 12:54 AM
Think I will pull down the latest sound and see if I can come up with a patch...
Those guys seem a bit cranky unless you post a patch when you post the bug... but if you do give them a patch... the natives turn friendly... lol.
Btw... there is work that needs to be done on the internationalization stuff in libc too... anyone wanna help me (already gave them part of it last month)...
Michael
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.