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

newbie: getaddrinfo

38 views
Skip to first unread message

Thomas

unread,
Apr 9, 2013, 9:28:26 AM4/9/13
to
Hi there,

I write a network prog (it's in c++ but I use the socket api). Here is my func/method:

struct addrinfo* Net::Init(const char *serv) const {

struct addrinfo hints;
bzero(&hints, sizeof(struct addrinfo) );

hints.ai_flags = AI_PASSIVE; // for passive open
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

struct addrinfo *res;
int r = 0;
r = getaddrinfo(NULL, serv, &hints, &res);
if (r != 0) {
cout << "Line: " << __LINE__ << endl;
cout << '\n' << gai_strerror(r) << endl;
}

return res;
}

It works but not correct. Here is the call in main:

struct addrinfo *res = cli.Init("http");
listenfd = cli.Socket(res->ai_family, res->ai_socktype, res->ai_protocol);

The problem is: when I pass "http" in cli.Init("http") and call it I get every time another port/service when I view socklist (lists all listening sockets on my ubuntu linux).

Whats wrong here?

Greetings Thomas

Rainer Weikusat

unread,
Apr 9, 2013, 9:41:46 AM4/9/13
to
Likely guess: You didn't bind the socket to any address.

Thomas

unread,
Apr 9, 2013, 9:49:21 AM4/9/13
to
bind(listenfd, (SA *) &res->ai_addr, res->ai_addrlen);
listen(listenfd, 5);

Markus Schaaf

unread,
Apr 9, 2013, 9:54:01 AM4/9/13
to
Am 09.04.2013 15:28, schrieb Thomas:

> struct addrinfo* Net::Init(const char *serv) const {
>
> struct addrinfo hints;
> bzero(&hints, sizeof(struct addrinfo) );
>
> hints.ai_flags = AI_PASSIVE; // for passive open
> hints.ai_family = AF_UNSPEC;
> hints.ai_socktype = SOCK_STREAM;
>
> struct addrinfo *res;
> int r = 0;
> r = getaddrinfo(NULL, serv, &hints, &res);
> if (r != 0) {
> cout << "Line: " << __LINE__ << endl;
> cout << '\n' << gai_strerror(r) << endl;
> }
>
> return res;
> }
>
> It works but not correct. Here is the call in main:
>
> struct addrinfo *res = cli.Init("http");
> listenfd = cli.Socket(res->ai_family, res->ai_socktype, res->ai_protocol);
>
> The problem is: when I pass "http" in cli.Init("http") and call it I get every time another port/service when I view socklist (lists all listening sockets on my ubuntu linux).

Nobody can tell for sure what's going on, because you didn't post the
relevant code. Assuming `cli.Socket` calls `socket` and `listen`, then
the arguments to `listen` might give an answer. As it seems, these won't
be influenced by the argument to `cli.Init`.

Thomas

unread,
Apr 9, 2013, 10:00:49 AM4/9/13
to
Am Dienstag, 9. April 2013 15:28:26 UTC+2 schrieb Thomas:
Here is the code from main():

struct sockaddr_storage cliaddr;

struct addrinfo *res = cli.Init("http");
listenfd = cli.Socket(res->ai_family, res->ai_socktype, res->ai_protocol);

bind(listenfd, (SA *) &res->ai_addr, res->ai_addrlen);
listen(listenfd, 5);

signal(SIGCHLD, sig_chld);
for ( ; ; ) {

clilen = sizeof(cliaddr);
if ( (connfd = accept(listenfd, (SA *) &cliaddr, &clilen)) < 0) {
// handle interrupted system call
if (errno == EINTR)
continue;
else
err_msg("accept-error");
}

...
....

Thomas

unread,
Apr 9, 2013, 10:02:34 AM4/9/13
to

Richard Kettlewell

unread,
Apr 9, 2013, 10:03:50 AM4/9/13
to
Thomas <tok...@gmail.com> writes:
> Here is the code from main():
>
> struct sockaddr_storage cliaddr;
>
> struct addrinfo *res = cli.Init("http");
> listenfd = cli.Socket(res->ai_family, res->ai_socktype, res->ai_protocol);
>
> bind(listenfd, (SA *) &res->ai_addr, res->ai_addrlen);
> listen(listenfd, 5);

You need to check all these calls for errors. You can’t just call them
and blindly assume they worked.

--
http://www.greenend.org.uk/rjk/

Markus Schaaf

unread,
Apr 9, 2013, 10:21:44 AM4/9/13
to
Am 09.04.2013 15:54, schrieb Markus Schaaf:

> Nobody can tell for sure what's going on, because you didn't post the
> relevant code. Assuming `cli.Socket` calls `socket` and `listen`, then
> the arguments to `listen` might give an answer.

Read `listen` as `bind`. :-)

Since you didn't pass AI_ADDRCONFIG as a flag and then blindly bind to
the first address, that call probably fails.

Richard Kettlewell

unread,
Apr 9, 2013, 11:12:44 AM4/9/13
to
...I’m not sure it was necessary to send me seven email replies to that.

--
http://www.greenend.org.uk/rjk/

Thomas

unread,
Apr 10, 2013, 7:35:05 AM4/10/13
to
Am Dienstag, 9. April 2013 15:28:26 UTC+2 schrieb Thomas:
There was a bind-error. I found it. Thanks for help.
0 new messages