|
Problem with getaddrinfo() function
I am new to network programming.
I just copied the code from Beej's Guide, to fill in the addrinfo structure using getaddrinfo.
It said that if I have hints.ai_flags-AI_PASSIVE and NULL in the first argument of getaddrinfo, i'll get a linked list of structures suitable for binding some port, i.e. my own ip address will be filled.
So 127.0.0.1 (loopback address) should be filled.
But when I see the value, it is giving 0.0.0.0
<code>
char address[100];
struct addrinfo hints, *res,*first;
int sockfd;
// first, load up address structs with getaddrinfo():
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
// fill in my IP for me
getaddrinfo(NULL, "3490", &hints, &res);
first=res;
// make a socket:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
inet_ntop(first->ai_family,&(((struct sockaddr_in *)(first->ai_addr))->sin_addr),address,100);
perror("inet_ntop");
printf("%s\n",address);
printf("%d\n",first->ai_protocol);
printf("%hu\n",ntohs(((struct sockaddr_in *)(first->ai_addr))->sin_port));
</code>
|