Unix Programming - How to reliably get local host IP address?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > December 2007 > How to reliably get local host IP address?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to reliably get local host IP address?
D'artagnan

2007-12-14, 1:35 am

I used to have this:

struct addrinfo *infop = 0, hint;
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
getaddrinfo(hostname, 0, &hint, &infop);
struct sockaddr_in *sa = (struct sockaddr_in *)infop->ai_addr;
struct in_addr addr = sa->sin_addr;
......

However, sometimes the address returned was 127.0.0.1. I figured out
this is because in /etc/hosts the local host name (say, snowstorm) is
mapped to 127.0.0.1, not to a real IP:
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com