자신의 IP를 받아오는 gethostbyname()이 127.0.0.1 만 반환합니다

안녕하세요.^^

네트워크 관련 프로그래밍을 하다가 의문점이 있어서 이렇게 질문을 올립니다.

공유기에 연결하는 장치여서 IP주소가 바뀔 수가 있습니다.(전제 조건)

gethostbyname( )함수를 이용하여 IP 주소를 출력하려고 하는데,

계속 루프백 주소(127. 0. 0. 1)만 출력합니다. 실제 공유기와 연결된 IP 주소를 출력하려면 어떻게 해야할까요??

소스코드는 아래와 같습니다.
–> 소스코드는 다음의 책의 예제를 약간 수정한 것 입니다. "컴퓨터 네트워크 프로그래밍", 김화종 저, 홍릉과학출판사

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>

int main(int argc, char* argv[])
{
struct hostent *hp;
struct in_addr in;
int i;
char buf[20];

char localHostName[256];
memset(localHostName, 0, 256);
gethostname(localHostName, 256);

hp=gethostbyname(localHostName);
if(hp==NULL)
{
    printf(&quot;gethostbyname failed\n&quot;);
    exit(0);
}
printf(&quot;호스트 이름          : %s\n&quot;, hp-&gt;h_name);
printf(&quot;호스트 주소타입 번호 : %d\n&quot;, hp-&gt;h_addrtype);
printf(&quot;호스트 주소 길이     : %d\n&quot;, hp-&gt;h_length);
for(i=0; hp-&gt;h_addr_list[i]; i++)
{
    memcpy(&amp;in.s_addr, hp-&gt;h_addr_list[i], sizeof(in.s_addr));
    inet_ntop(AF_INET, &amp;in, buf, sizeof(buf));
    printf(&quot;IP address(%d) : %s\n&quot;, i+1, buf);
}
for(i=0; hp-&gt;h_aliases[i]; i++)
{
    printf(&quot;Host alias(%d) : %s &quot;, i+1, hp-&gt;h_aliases[i]);
}
puts(&quot;&quot;);
return 0;

}

[url:216rkn63]http://pastebin.com/1Hp8Waju[/url:216rkn63]

이 코드로 한번 테스트 해보세요.