Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

connect to hostname in /etc/hosts

27 views
Skip to first unread message

cerr

unread,
Nov 5, 2009, 6:35:41 PM11/5/09
to
Hi There,

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

David Schwartz

unread,
Nov 5, 2009, 7:46:03 PM11/5/09
to


Use 'getaddrinfo' or 'gethostbyname' or whatever function does what
you want.

DS

Rainer Weikusat

unread,
Nov 6, 2009, 7:49:50 AM11/6/09
to

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.

frank

unread,
Nov 6, 2009, 12:00:43 PM11/6/09
to

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?"

cerr

unread,
Nov 26, 2009, 4:50:12 PM11/26/09
to

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

Alan Curry

unread,
Nov 26, 2009, 5:13:54 PM11/26/09
to
In article <af2553d5-15c2-4c10...@b25g2000prb.googlegroups.com>,

cerr <ron.e...@gmail.com> wrote:
>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!

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

cerr

unread,
Nov 26, 2009, 5:49:14 PM11/26/09
to
On Nov 26, 2:13 pm, pac...@kosh.dhis.org (Alan Curry) wrote:
> In article <af2553d5-15c2-4c10-8ab8-b4e233748...@b25g2000prb.googlegroups.com>,

>
> cerr  <ron.egg...@gmail.com> wrote:
> >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!
>
> 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.

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

0 new messages