I would like to know how to get the public ip of my server with C programming language.
I already know how to do this with libcurl but now i want to understand how to get this info with the socket programming (if it is possible).
I've already tried with struct hostent *hp
but i get only the local address 127.0.0.1
This is the code i've used:
int main(int argc, char *argv[]){
struct hostent *hp;
int i=0;
if((hp=gethostbyname(argv[1])) == NULL){
herror("gethostbyname()");
exit(1);
}
fprintf(stdout, "Hostname: %s\n", hp->h_name);
/* fprintf (stdout,"IP server: %s\n",inet_ntoa(*((struct in_addr *)hp->h_addr))); con questa printo solo 1 ip */
while (hp->h_addr_list[i] != NULL) { /* mentre così printo tutti gli eventuali ip */
printf("IP: %s\n", inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i])));
i++;
}
return 0;
}