• 0

External IP address


Question

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: 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 :) I really appreciate this.

Link to comment
Share on other sites

Recommended Posts

  • 0

I've found this code, but I can't make it work :( Can someone tell me how to use it with c#?

Here it is:

// 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.

Link to comment
Share on other sites

  • 0
u could make a HTTP file downloaded from www.whatismyip.com!!!

Then u can check the HTML code for the IP address...

Simple :p 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.

Edited by yyy
Link to comment
Share on other sites

  • 0

Well, I've figured out how to do it in C# :laugh: (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:

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.

Link to comment
Share on other sites

  • 0
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.

Link to comment
Share on other sites

  • 0
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.

Link to comment
Share on other sites

  • 0

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 ;)

Link to comment
Share on other sites

  • 0
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 ;)

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 :) 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

Edited by yyy
Link to comment
Share on other sites

  • 0
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 :( But I've been told elsewhere that it gives the wrong address :( :( :( Anyone has a better way ?

Link to comment
Share on other sites

  • 0

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. :)

Link to comment
Share on other sites

  • 0

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 :)

Link to comment
Share on other sites

  • 0
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

Link to comment
Share on other sites

  • 0
How can you have an external IP address if you're not connected to the Internet?
Yes I know about this now.
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? :( 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.

Link to comment
Share on other sites

  • 0
<%@ 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>

Link to comment
Share on other sites

  • 0
<%@ 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:):) But I don't have a site. I need it for my application. Thanks anyway.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.