728x90

IP Time 공유기를 사용하여 접속하면 보통 사설 IP 주소를 사용하게 된다.

내 PC의 공인 IP주소를 알아내는 코드는 Web 파싱을 이용하면 쉽다.


public static string getMyPublicIP()
{
    WebRequest request = WebRequest.Create("http://www.findip.kr");
    WebResponse response = request.GetResponse();
    StreamReader stream = new StreamReader(response.GetResponseStream());

    // 주소에 있는 텍스트 모두를 긁어 저장
    string firstStr = stream.ReadToEnd();
    // 파싱할 부분의 시작부분 검색
    int index1 = firstStr.IndexOf("<h1> 내 아이피 주소(My IP Address) :") + 31;
    // 끝부분 검색
    int index2 = firstStr.IndexOf("</h1>");
    //다시 담기
    string str = firstStr.Substring(index1, index2 - index1);
    return str;
}



블로그 이미지

Link2Me

,