I would like to establish a socket connection to a hostname whose ip
address is defined in /etc/hosts.
inet_addr says
"function converts the Internet host address cp from numbers-and-dots
notation" but i wanna have it converting from a hostname.
Any clues are appreciated!
Thanks,
Ron
Use 'getaddrinfo' or 'gethostbyname' or whatever function does what
you want.
DS
A bit more information: gethostbyname is the traditional, simple
interface, suitable for single-threaded programs which just want to
turn hostnames in IPv4 addresses. getaddrinfo is more complete and in
particular, it can be used to write programs which are (almost) completely
indepedent of the actually used transport- and internet-layer
protocols, the downside being that using it requires much more effort.
Hey Ron,
What do you have for source on this? I'm not new to C but am new to the
*nix platform, so I'm curious what all a person can do.
--
frank
"Rape: is it too much to ask for corporations who bid on federal
contracts to refrain from?"
Hi Rainer,
I've tried to use gethost by name as follows:
memset(host,0x00,sizeof(host));
sprintf(host,"nems");
host_info = NULL;
host_info = gethostbyname(host);
if (host_info) {
address = (in_addr*)host_info->h_addr_list[0] ;
memset(host,NULL,sizeof(host)) ;
strcpy(host,inet_ntoa(*address)) ;
}
sa_server.sin_family = AF_INET;
sa_server.sin_port = htons(Port);
sa_server.sin_addr.s_addr = inet_addr(host);
The declarations look like this:
char host[255];
int Port=1514;
hostent *host_info;
in_addr *address;
and i have inluded
#include <netdb.h>
and
#include <sys/socket.h>
but i get following compiler errors:
`hostent' undeclared (first use in this function)
and
`in_addr' undeclared (first use in this function)
which tells me that my compiler (gcc 3.4.4) does not recognize hostent
nor in_addr.
Why not? Does anyone have any clues? Comments are appreciated!
Thanks,
Ron
Because the types are called "struct hostent" and "struct in_addr".
C doesn't automatically typedef every struct like C++ does, so you actually
do need the "struct" keyword.
--
Alan Curry
Huh, yes I figured that out, it's been a while since i wrote C99
code :) Excuse me for that stupid-ish question :) I get a connection
established succesfully now! :)
Sweet and thanks for everyone's help!
--
Ron