newbie: getaddrinfo problem

T

Thomas

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
 
T

Thomas

Am Dienstag, 9. April 2013 14:46:32 UTC+2 schrieb Thomas:
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


Oh thanx.
 
J

James Kuyper

Hi there,

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

You make use of several features of C++; in principle, misuse of one of
those features might have been the reason why your code isn't doing what
you expect it to do. You shouldn't expect people at comp.lang.c to be
sure about whether or not those features have been used correctly - we
have C experts here, but not so many C++ experts. Because your question
is primarily about Unix, the most appropriate place to ask you question
is comp.unix.programmer. However, if it had been a programming question
rather than a Unix question, comp.lang.c++ or comp.lang.c++.moderated
would have been more appropriate places to post it - they have lots of
C++ experts there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top