Click here to Skip to main content
11,706,142 members (56,930 online)
Click here to Skip to main content

Tagged as

Getting the External IP Address

, 3 Sep 2012 CPOL 33.8K 14
Rate this:
Please Sign up or sign in to vote.
A very simple and short way to get the external IP address

Introduction

We connect to servers that give us our external IP address and try to parse the IP from returning HTML pages. But when servers make small changes on these pages or remove them, these methods stop working properly.

Here is a method that takes the external IP address using a server which has been alive for years and returns a simple response rapidly...

C#

private string getExternalIp()
{
    try
    {
        string externalIP;
        externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
        externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                     .Matches(externalIP)[0].ToString();
        return externalIP;
    }
    catch { return null; }
}

VB.NET

Private Function GetExternalIp() As String
    Try
        Dim ExternalIP As String
        ExternalIP = (New WebClient()).DownloadString("http://checkip.dyndns.org/")
        ExternalIP = (New Regex("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) _
                     .Matches(ExternalIP)(0).ToString()
        Return ExternalIP
    Catch
        Return Nothing
    End Try
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Huseyin Atasoy
Engineer
Turkey Turkey
I am interested in artificial intelligence, signal processing, electronics, robotics and internet/network technologies.

A few of my works:
KANO (Real-time Optical Mark Recognition)
PAKEV
Real-time object recognition
Real-time facial expression recognition

My blog

You may also be interested in...

Comments and Discussions

 
SuggestionWay without REGEX Pin
Rhodderz2-Mar-15 5:00
memberRhodderz2-Mar-15 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 3 Sep 2012
Article Copyright 2012 by Huseyin Atasoy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid