Help - Search - Members - Calendar
Full Version: External IP address
Neowin Forums > Help & Discussion Center > Programming (C#, C++, JAVA, VB, .NET etc.)
yyy
Hello,
I'm using c# .net and I would like to know how can I get my external IP. I was looking over the Internet and couldn't find a solution wacko.gif Maybe somebody here knows how to do it? I can also try to convert a VB .NET code so if you know how to do it with VB that's OK.
Thanks in advance for your answers smile.gif I really appreciate this.
Andareed
You could use the win32 api GetIpAddrTable. Check MSDN for docs and an example.
yyy
Andareed - your'e great smile.gif biggrin.gif laugh.gif I hope I can understand the information I've found.
yyy
I've found this code, but I can't make it work sad.gif Can someone tell me how to use it with c#?

Here it is:
CODE
// Before calling AddIPAddress we use GetIpAddrTable to get
// an adapter to which we can add the IP.
PMIB_IPADDRTABLE pIPAddrTable;
DWORD dwSize = 0;

pIPAddrTable = (MIB_IPADDRTABLE*) malloc( sizeof( MIB_IPADDRTABLE) );

// Make an initial call to GetIpAddrTable to get the
// necessary size into the dwSize variable
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
 GlobalFree( pIPAddrTable );
 pIPAddrTable = (MIB_IPADDRTABLE *) malloc ( dwSize );
}

// Make a second call to GetIpAddrTable to get the
// actual data we want
if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) == NO_ERROR ) {
 printf("\tAddress: %ld\n", pIPAddrTable->table[0].dwAddr);
}


It is from MSDN site.
Rupert
That looks like C++ although the languages are quite similar.
Rupert
You need to import a DLL...
yyy
How? Can you please show me how to do it in c#?
yyy
Anyone ?
AlienWorkShop
u could make a HTTP file downloaded from www.whatismyip.com!!!

Then u can check the HTML code for the IP address...
Simple tongue.gif lol
yyy
Quote - (AlienWorkShop @ Oct 5 2004, 08:50)
u could make a HTTP file downloaded from www.whatismyip.com!!!

Then u can check the HTML code for the IP address...
Simple tongue.gif lol

Is it legal ? And even if it is - it will take a long time for dial up modem users to load it. I want a faster way to get the IP address - even if your'e not connected or if you have a dial up modem.

BTW - check this: http://dotnet247.com/247reference/msgs/10/53244.aspx. I think I need something similar.
yyy
Well, I've figured out how to do it in C# laugh.gif (I use the code that I've found in the site)

HOWEVER, I know only how to use it as a console application. I want to use it inside a form. Can you tell me how I can use this code inside a form so it will show the text in a label ?

Here is the code as a console application:

CODE
using System;
using System.Net;
using System.Runtime.InteropServices;

struct MIB_IPADDRROW
{
public int dwAddr;
public int dwIndex;
public int dwMask;
public int dwBCastAddr;
public int dwReasmSize;
public short unused1;
public short unused2;
}

class C
{
[DllImport("iphlpapi.dll")]
static extern int GetIpAddrTable(
IntPtr pIpAddrTable,
ref int pdwSize,
bool bOrder);

static string IPToString(int ipaddr)
{
return String.Format( "{0}.{1}.{2}.{3}",
(ipaddr >> 24) & 0xFF, (ipaddr >> 16) & 0xFF,
(ipaddr >> 8) & 0xFF, ipaddr & 0xFF);
}

static void Main()
{
IntPtr pBuf = IntPtr.Zero;
int nBufSize = 0;

GetIpAddrTable( IntPtr.Zero, ref nBufSize, false );
try {
pBuf = Marshal.AllocHGlobal( nBufSize );
int r = GetIpAddrTable( pBuf, ref nBufSize, false );
if ( r != 0 )
throw new System.ComponentModel.Win32Exception( r );

#if USE_UNSAFE_CODE

unsafe {
int nNumRows = *((int*)(void*)pBuf);

MIB_IPADDRROW* pRow =
(MIB_IPADDRROW*)(void*)(IntPtr)((int)pBuf + sizeof(int));
while ( nNumRows-- > 0 ) {
Console.WriteLine( IPToString( IPAddress.NetworkToHostOrder(
pRow->dwAddr ) ) );
pRow++;
}
}


}
catch (Exception ex) {
Console.WriteLine( ex.Message );
}
finally {
if ( pBuf != IntPtr.Zero )
Marshal.FreeHGlobal( pBuf );
}
}

}


Please tell me how to use it in a form.
Garry
Quote - (yyy @ Oct 5 2004, 10:19)
Is it legal ? And even if it is - it will take a long time for dial up modem users to load it. I want a faster way to get the IP address - even if your'e not connected or if you have a dial up modem.

BTW - check this: http://dotnet247.com/247reference/msgs/10/53244.aspx. I think I need something similar.

Legal depends on whether whatismyip.com allow you to do it. It wouldn't take very long for dial-up users anyway.

If you're not connected to the Internet then you do not have an external IP address (or you'll have one in the 169 range which is useless anyway.) Getting the IP address from the computer itself isn't a good idea anyway since a lot of people use devices like routers where the computer is not assigned the external IP address as it is assigned to the router which creates a network between itself and the PC on an internal range of IPs.
yyy
Quote - (Garry @ Oct 5 2004, 23:54)
Legal depends on whether whatismyip.com allow you to do it. It wouldn't take very long for dial-up users anyway.

If you're not connected to the Internet then you do not have an external IP address (or you'll have one in the 169 range which is useless anyway.) Getting the IP address from the computer itself isn't a good idea anyway since a lot of people use devices like routers where the computer is not assigned the external IP address as it is assigned to the router which creates a network between itself and the PC on an internal range of IPs.

I once used http://checkip.dyndns.org/ to do this (I thought it is legal) and a dial up user of my program told me that it take him about a minute (which is a lot) to load the program. Finally I asked the site's webmaster if it is legal and he said it isn't.

But look at the code above - can't this code get the external IP always ? I think it does.
Andareed
Requirements (from MSDN):
Client: Requires Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0 SP4 and later, Windows Me, or Windows 98.
Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0 SP4 and later.

So it won't work on win95. What a shame wink.gif
yyy
Quote - (Andareed @ Oct 6 2004, 01:23)
Requirements (from MSDN):
Client: Requires Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0 SP4 and later, Windows Me, or Windows 98.
Server: Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 4.0 SP4 and later.

So it won't work on win95. What a shame wink.gif

What won't work ? The code above? I don't mind. It is being done as a plug in for a program that doesn't work on Win 95 anyway smile.gif So I don't mind.


But plese - I want to show the information inside a label - how can I do it?

BTW, the code was written by:
Mattias Sjögren
mattias@mvps.org
http://www.msjogren.net
yyy
Can someone test the code above and tell me if it shows the real external IP address? I'm not sure if it does sad.gif
Andareed
You can test it yourself and compare its output to ipconfig.exe.
yyy
Quote - (Andareed @ Oct 6 2004, 23:20)
You can test it yourself and compare its output to ipconfig.exe.

I don't have a routher or something so my external and internal IP's are the same sad.gif But I've been told elsewhere that it gives the wrong address sad.gif sad.gif sad.gif Anyone has a better way ?
hava333
Do you just want a script in C# that gets the IP address of a person viewing your site? Cuz i have something that does jsut that, but i cant find it at the moment, ill keep lookin. smile.gif
yyy
I need a code in c# or VB that shows the external IP address of the user of my application. That's all. Do you have something like this ? I will really appreciate it if you tell me smile.gif
jafoman
Quote - (yyy @ Oct 5 2004, 09:19)
Is it legal ? And even if it is - it will take a long time for dial up modem users to load it. I want a faster way to get the IP address - even if your'e not connected or if you have a dial up modem.

BTW - check this: http://dotnet247.com/247reference/msgs/10/53244.aspx. I think I need something similar.

How can you have an external IP address if you're not connected to the Internet? The only way that I know of to get your public IP address is to ask another machine on the Internet for your IP address. You won't be able to obtain it from some C++ or C# function that I'm aware of!

jafo
yyy
Quote -
How can you have an external IP address if you're not connected to the Internet?
Yes I know about this now.

Quote -
The only way that I know of to get your public IP address is to ask another machine on the Internet for your IP address.  You won't be able to obtain it from some C++ or C# function that I'm aware of!


Oh really? sad.gif Too bad you didn't tell me sooner. So I'll probably have to use a site. I saw once that there's a way to get the external IP but I didn't understand it.
Thanks anyway to everyone. If someone still finds a code soon I'll like to see it.
hava333
CODE
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<%

string ClientIP;
StreamWriter objStreamWriter;


ClientIP = Request.UserHostAddress.ToString();

IP.Text = ClientIP;


objStreamWriter = File.AppendText( MapPath( "beta_ip.txt" ) );
objStreamWriter.WriteLine( (ClientIP) );
objStreamWriter.Close();


%>

<html>
<body>


<form runat="server">
 <p>
<asp:Label id="IP" runat="server" />
 </p>
</form>
</body>
</html>
yyy
Quote - (hava333 @ Oct 7 2004, 02:39)
CODE
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<%

string ClientIP;
StreamWriter objStreamWriter;


ClientIP = Request.UserHostAddress.ToString();

IP.Text = ClientIP;


objStreamWriter = File.AppendText( MapPath( "beta_ip.txt" ) );
objStreamWriter.WriteLine( (ClientIP) );
objStreamWriter.Close();


%>

<html>
<body>


<form runat="server">
 <p>
<asp:Label id="IP" runat="server" />
 </p>
</form>
</body>
</html>

Thanks for the code smile.gif But I don't have a site. I need it for my application. Thanks anyway.
hava333
Is your application windows based?
yyy
Quote - (hava333 @ Oct 8 2004, 00:14)
Is your application windows based?

Yes. I use c# .net.
itsnotabigtruck
It's a much better idea to put a script on a web site and read it in code than use the code suggested above, because some people might be on NATs or on proxies.

For example, your code could download the content of whatismyip.com and parse the IP out of it.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.