I follow Bay's NP manual.
I made some changes and am trying to get the IP address of my server program through getaddrinfo ().
(the original can be found here http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver )
Below are the parts that I changed / added.
if ((rv = getaddrinfo(NULL, "0", &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
printf("ip: %s\nport: %d\n",
inet_ntop(AF_INET, &((struct sockaddr_in *)p->ai_addr)->sin_addr, ip4, INET_ADDRSTRLEN),
ntohs(((struct sockaddr_in *)p->ai_addr)->sin_port)
);
The problem is that I get results
ip: 0.0.0.0
port: 0
Q1: I read from several websites saying that setting “0” for the port tells the OS that you want the next available port, and not really 0. Is that true?
Q2: I also read that gethostbyname (gethostname (...)) can give you an ip machine, but Beej said that they are replaced by getaddrinfo (). So should I use getaddrinfo? Or gethostbyname?
Q3: - , ?