• 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
https://www.neowin.net/forum/topic/225737-external-ip-address/
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.

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

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

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

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

  • 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

[email protected]

http://www.msjogren.net

Edited by yyy
  • 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

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

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

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

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

    • No registered users viewing this page.
  • Posts

    • Euro-Office must default to ODF to be considered "genuinely European", LibreOffice argues by David Uzondu Euro-Office is a web-based collaborative office suite that positions itself as a "European sovereign alternative" to American tech companies, backed by a coalition of developers including Nextcloud, IONOS, Abilian, BTactic, OpenProject, and, more recently, Tuta. The project officially went live a couple of days ago, but not before drawing heavy fire from LibreOffice developers, who called the marketing claim that Euro-Office represents the "first open-source office suite developed in Europe" a deceptive historical inaccuracy because projects like OpenOffice and LibreOffice existed decades earlier. Now that the project has launched, LibreOffice is back with another complaint, arguing that Euro-Office cannot consider itself "genuinely European" while it pushes proprietary Microsoft defaults on users. Euro-Office had promised to improve the OpenDocument Format (ODF) back in April, but the current release still plagues users with several technical failures. For instance, the suite lacks an admin setting to enforce ODF, and mobile editors completely block ODF saves, forcing files into Microsoft's OOXML formats. Some configurations force files into read-only mode, while editing frequently corrupts document formatting or erases data. LibreOffice thinks that merely supporting a format as an afterthought does not make you a sovereign alternative, as file formats are the battleground where" digital sovereignty is won or lost." The road to the first stable release of Euro-Office has been quite bumpy due to an aggressive public fallout with OnlyOffice, from which the coalition originally forked the project. OnlyOffice struck back by accusing the coalition of violating copyright terms under its AGPLv3 branding requirements by stripping the original branding anyway and forking the code. Getting Euro-Office up and running is a bit wonky (at least for non-technical users), as there is no direct installer to grab off the web. The easiest way we learnt is by using Docker. First, pull the official Euro-Office image from the GitHub Container Registry: docker pull ghcr.io/euro-office/documentserver:latest Then, run the container with active ports and a secure JWT token, enabling the test environment: docker run -i -t -d -p 8080:80 --restart=always -e EXAMPLE_ENABLED=true -e JWT_SECRET=my_secure_jwt_secret ghcr.io/euro-office/documentserver:latest And finally, open a web browser and go to the following address: http://localhost:8080 If you are running this on a remote server, replace localhost with your server's IP address. You will see the Euro-Office test page, where you can create new text documents, spreadsheets, or presentations directly in the browser. Image via Euro-Office Nextcloud promises that proper standalone desktop versions and mobile apps will arrive in a future release.
    • It’s any of their products not just windows.
    • Google Gemini has been failing for users across the United States, Europe, and Asia since early Wednesday morning, June 10, 2026, and more than six hours into the incident Google has yet to declare a fix............. https://www.techtimes.com/articles/318152/20260610/google-gemini-outage-tops-six-hours-errors-1076-1099-worldwideflash-lite-still-answers.htm
    • Fun fact: There are more Warhammer 40k games than there are stars in the universe.
  • Recent Achievements

    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      486
    2. 2
      PsYcHoKiLLa
      197
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!