TechLog

자신의 IP 주소 가져오기

Kenial 2008. 8. 6. 13:26

자신의 IP 주소 가져오기

닷넷 프레임워크에서 System.Net.Dns 어셈블리를 사용해 자신의 IP 주소를 가져오는 방법을 설명합니다.

 

 

자신의 IP 주소 가져오기

 

자신의 IP 주소를 가져오는 코드는 System.Net.Dns 어셈블리를 사용해서 만들 수 있다. 코드는 다음과 같다 :

 

string hostname = System.Net.Dns.GetHostName();

System.Net.IPAddress[] ipaddresses = System.Net.Dns.Resolve(hostname).AddressList;

foreach (System.Net.IPAddress ipaddress in ipaddresses)

{

    MessageBox.Show(ipaddress.ToString());

}

 

hostname 에는 자신의 컴퓨터 이름(네트워크상에 netbios 이름으로 나타나는)이 리턴되며, Dns.Resolve로 해당 hostname IPAddress 배열을 얻어오게 된다. IP가 여러개 존재할 경우에도 모두 얻어올 수 있다.